64

I am using redis as an in-memory database backend for django cache.

In particular, I use django-redis configured as follows:

CACHES = {
    'default': {
        'BACKEND': 'redis_cache.cache.RedisCache',
        'KEY_PREFIX':   DOMAIN_NAME,
        'LOCATION': 'unix:/tmp/redis_6379.sock:1',
        'OPTIONS': {
            'PICKLE_VERSION': -1,   # default
            'PARSER_CLASS': 'redis.connection.HiredisParser',
            'CLIENT_CLASS': 'redis_cache.client.DefaultClient',
        },
    },
}

My django cache seem to work correctly.

The weird thing is that I cannot see django cache keys using the redis-cli command line.

[edit] Please notice in the following that I tried both with

$ redis-cli

and

$ redis-cli -s /tmp/redis_6379.sock

[endedit]

with no difference.

In particular, using the KEYS * command:

$ redis-cli
redis 127.0.0.1:6379> keys *
(empty list or set)

but

redis 127.0.0.1:6379> set stefano test
OK
redis 127.0.0.1:6379> keys *
1) "stefano"

while from django shell:

In [1]: from django.core.cache import cache

In [2]: cache.keys('*')
Out[2]:
[u'django.contrib.sessions.cachebblhwb3chd6ev2bd85bawuz7g6pgaij8',
 u'django.contrib.sessions.cachewpxiheosc8qv5w4v6k3ml8cslcahiwna']

If I'm using MONITOR on the cli:

redis 127.0.0.1:6379> monitor
OK
1373372711.017761 [1 unix:/tmp/redis_6379.sock] "KEYS" "project_prefix:1:*"

I can see a request, using the django cache prefix; which should prove the redis-cli is connected to the same service. But even searching for that prefix in the redis-cli returns an (empty list or set)

Why is that?

What is the mechanisms that compartmentalize the different caches on the same redis instance?

Stefano
  • 18,083
  • 13
  • 64
  • 79

2 Answers2

83

I would say there are two possibilities:

1/ The django app may not connect to the Redis instance you think it is connected to, or the redis-cli client you launch does not connect to the same Redis instance.

Please note you do not use the same exact connection mechanism in both cases. Django uses a Unix Domain Socket, while redis-cli uses TCP loopback (by default). You may want to launch redis-cli using the same socket path, to be sure:

$ redis-cli -s /tmp/redis_6379.sock

Now since you have verified with a MONITOR command that you see the commands sent by Django, we can assume you are connected to the right instance.

2/ There is a database concept in Redis. By default, you have 16 distinct databases, and the current default database is 0. The SELECT command can be used to switch a session to another database. There is one keyspace per database.

The INFO KEYSPACE command can be used to check whether some keys are defined in several databases.

redis 127.0.0.1:6379[1]> info keyspace
# Keyspace
db0:keys=1,expires=0
db1:keys=1,expires=0

Here I have two databases, let's check the keys defined in the db0 database:

redis 127.0.0.1:6379> keys *
1) "foo"

and now in the db1 database:

redis 127.0.0.1:6379> select 1
OK
redis 127.0.0.1:6379[1]> keys *
1) "bar"

My suggestion would be also to check whether the Django application sends any SELECT command at connection time to the Redis instance (with MONITOR).

I'm not familiar with Django, but the way you have defined the LOCATION parameter makes me think your data could be in database 1 (due to the suffix).

Stefano
  • 18,083
  • 13
  • 64
  • 79
Didier Spezia
  • 70,911
  • 12
  • 189
  • 154
  • 2
    Thanks Didier. I had ruled out 1) because I also tried connecting to the socket directly and I saw no difference. But I did not know about the "SELECT" command! That's totally it! Working now... I must say that Redis documentation on this point wasn't very clear..Even checking out the [Redis SELECT](http://redis.io/commands/select) command doc shows a bunch of confused comments! – Stefano Jul 10 '13 at 07:52
  • Added a link to the "SELECT" command documentation to your answer, but I COULD NOT FIND any official reference to the multiple databases. Maybe it's just me, if you know it could you please add the link? Thanks! – Stefano Jul 10 '13 at 07:59
  • 1
    I'm afraid there is no dedicated page describing databases. There is one useful comment in the configuration file though: https://github.com/antirez/redis/blob/unstable/redis.conf#L86 – Didier Spezia Jul 10 '13 at 12:25
  • Crazy this lack of documentation! Thanks a lot Didier. I wonder if I should file an issue, but there are 400 outstanding ones and I would not be capable of providing a documentation pull proposal :( – Stefano Jul 10 '13 at 12:41
  • 1
    Probably because Salvatore consideres it the worst design decision in Redis and wants to get rid of it. I would definitely change the Django location to use db0, btw. – The Real Bill Jul 10 '13 at 14:47
  • Thanks @TheRealBill - that's probably a very good idea to avoid future confusions! – Stefano Jul 12 '13 at 14:19
4

Do this:

redis-cli -h <host> KEYS "trendingKey*"

OUTPUT

  1. "trendingKey:2:1"
  2. "trendingKey:trending102:1"
  3. "trendingKey:trending101:1"
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Victor
  • 761
  • 8
  • 7