Caching with Redis

If you find yourself in a situation with a huge site, small amount of memory, and static caching isn’t an option, using a Redis cache may be a great way to increase site performance.


What is Redis?

Redis is a NoSQL key/value store, much like our flat file system based on YAML, that is held in memory. This results in very fast I/O response times and lets you scale to much larger and handle heavier traffic. It’s a perfect fit for taking Statamic to the next-level.

If you’re not sure if you have Redis installed, you can run the following command from the command line.

$ redis-cli

If you don’t get an error and see something similar to this, you’ve got yourself some Redis.

$ 127.0.0.1:6379>

Installing Redis

If Redis isn’t already installed on your server, or you want to get it running locally for testing, here are a few articles that can help:

Enabling Statamic’s Redis Cache driver

In your .env file set the following:

CACHE_DRIVER=redis

That’s it. If you need to change the host, port, or database (not common, but useful if you have multiple sites on the same server that should not share a cache), you have access to the following options as well, shown with their defaults.

REDIS_HOST=127.0.0.1
REDIS_PORT=6379
REDIS_DATABASE=0

You can also set a prefix by adding:

CACHE_KEY_PREFIX=my_awesome_site

This is vital if you are running more than one site on a server.

How to confirm Redis is actually working

Run the monitor command and visit a page of the site in your browser. If it’s working you’ll see a number of events trigger as Redis does its thing.

$ redis-cli monitor
Last modified on July 8, 2019