Cached values not showing in Redis

Posted on: 02/11/2018. Last updated at: 04/11/2018


So as the title mentions, I wasn't able to see any of my cached values in Redis. Normally everytime i've stashed a value in cache, i've been able to fetch it by its key or list all of the saved keys.

GET key
KEYS *

At first I wasn't sure that anything was actually being saved. There is a way to check this, you can issue the following command in your terminal

redis-cli monitor

If your application is successfully saving items in Redis, you should see something similar to this image:

Example output from redis-cli monitor command

The answer was right infront of my eyes, but I didn't know Redis well enough to know what i was looking for!

The Solution

Redis has 16 databases. Whenever i've used Redis, it just so happens that it used database 0 for everything. As 0 is the default database. Looking at the output from the monitor command, you can see the first command it issues is "SELECT" "1". This is Redis changing the database.

To prove this, you can issue another command from withing the Redis CLI:

redis-cli
INFO KEYSPACE
# Keyspace
db1:keys=2,expires=2,avg_ttl=189046333

As you can see, db1 has 2 keys. You can change database by using the SELECT command followed by the database number. In my case this was SELECT 1.

Issue the KEYS * command once more and voilia, there are your cached values!

KEYS *
1) "cache:f1c4532dc98dd1b4bcdbe9f94d1e"
2) "cache:4dff7ca60b157dae0720a9ed321bd6"



Picture of Daniel Ord
Daniel Ord

Building things for the Web. Follow me on Twitter or checkout my GitHub.

More Posts

How to sanitize user input with Laravel Livewire
3 easy ways to sanitize user input with Laravel Livewire
Upload and fetch images using Digital Ocean Spaces and Laravel 5
I needed a place to securely store my files offsite and Amazon S3 was a obvious choice at time (2016)....
Can't write image data to path - Intervention Image
I recently came across the following error when using Intervention Image with Laravel 5.6: "Can't write image data to path...
Backup MySQL database to Digital Ocean Spaces
Creating backups of your database is a no-brainer. There are lots of guides on how to do this and upload...

Other Categories