Disable cache in Laravel

Published on November 2th, 2019

When debugging and you are not sure whether the cache is part of the issue it's always nice to temporarily disable it to get to the bottom of what's going on. What many people do not know and what isn't documented in the Laravel documentation is the option to disable the cache by using the Illuminate\Cache\NullStore. There is not much to do to enable the null or none cache driver. You only have to tweak the config/cache.php and modify your .env file.

I will first show you how to disable the cache and then explain why it is working the way it is.

Disabling the Cache

To disable the cache you have to add the following to your config/cache.php file.

  'stores' => [
    //...
    'none' => [
        'driver' => 'null',
    ],
  ],

Now you have to change your CACHE_DRIVER value to none in your .env file and your cache is disabled.

How does it work?

Let's take a look at the Illuminate\Cache\CacheManager@resolve() method, which is responsible for returning the right store for the given driver.

protected function resolve($name)
{
  $config = $this->getConfig($name);

  if (is_null($config)) {
    throw new InvalidArgumentException("Cache store [{$name}] is not defined.");
  }

  if (isset($this->customCreators[$config['driver']])) {
    return $this->callCustomCreator($config);
  } else {
    $driverMethod = 'create'.ucfirst($config['driver']).'Driver';

    if (method_exists($this, $driverMethod)) {
        return $this->{$driverMethod}($config);
    } else {
        throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported.");
    }
  }
}

We get the cache connection configs from $config = $this->getConfig($name);, which pulls the configuration for the specific store return $this->app['config']["cache.stores.{$name}"];. The if statement checks if we have a store with the given name defined and throws an error if not. Now the $this->customCreators array is checked if it has a key with the given name. $this->customCreators can be filled by calling the extend() method on the Cache facade. So, if there was no extension found it is assumed that we want to create one of the standard drivers. In our case the name of the driver is null that's why it's checked whether a method with the name createNullDriver() exists in the CacheManager.... surprise... there is such a method.

protected function createNullDriver()
{
  return $this->repository(new NullStore);
}

The method returns the cache Repository and passes the NullStore to it. You might be wondering what the NullStore is doing. The class just doesn't do anything. It only returns the return values it's expected to return by the Illuminate\Contracts\Cache\Store interface.

class NullStore extends TaggableStore
{
  //..

  public function get($key)
  {
      //
  }

  public function put($key, $value, $seconds)
  {
    return false;
  }

  public function increment($key, $value = 1)
  {
    return false;
  }

  public function decrement($key, $value = 1)
  {
    return false;
  }

  public function forever($key, $value)
  {
    return false;
  }

  public function forget($key)
  {
    return true;
  }

  public function flush()
  {
    return true;
  }

  public function getPrefix()
  {
    return '';
  }
}

Thanks for making it so far! I appreciate you taking the time to read through the article. To provide you with higher quality articles I need your feedback on how to write better articles. Did you enjoy the read? Do you have tips on what I could make better? Please tag me on twitter @krishankoenig or write me a mail to krishan.koenig@gmail.com.

Sign up for my newsletter

If you enjoyed this article, sign up for my newsletter to be the first to know when a new article is published.