7

I've got Redis set up as my cache in django, with the following setting:

CACHES = {
    'default': {
        'BACKEND': 'redis_cache.RedisCache',
        'LOCATION': 'localhost:6379',
        'OPTIONS': {
            'PICKLE_VERSION': 1,
        },
    },
}

And I'm experimenting with it (new to Redis, want to understand it better). So, I go into my Django shell, and I do:

from django.core.cache import cache
cache.set('asdf', 2)
cache.get('asdf')  # Returns 2

And then I go into redis-cli, where I expect to see the value, but none of these show any values:

KEYS *
GET *
GET 'asdf'

What's up with that?

mlissner
  • 17,359
  • 18
  • 106
  • 169

1 Answers1

10

Redis has 16 databases by default. As @Bernhard says in his comment, you can see how many keys each has with:

INFO KEYSPACE

Which in my case returned:

# Keyspace
db0:keys=1,expires=0,avg_ttl=0
db1:keys=2,expires=2,avg_ttl=504748260

And you can SELECT the database you want to inspect with:

SELECT 1

At which point, sure enough, I can see the keys I expected:

KEYS *
1) ":1:asdf"
2) ":1:django.contrib.sessions.cacheg2l0bo9z88z8bn4q2ep0andjgo8zrzzk"
mlissner
  • 17,359
  • 18
  • 106
  • 169
  • 1
    I had the same issue, but no matter which database I selected I still got nothing when using django-redis==5.2.0. When I explicitly specified the db: `"OPTIONS": {"DB": 1}` things worked as expected / mentioned in this answer. Also, thank you @mlissner! – ianarko Sep 26 '22 at 18:19