Redis values set from Python are not reflecting in redis client

4.4k Views Asked by At

I have installed redis on my OS X, and trying to set and get some values in Redis from Python 3.5 client. I have the Redis server on (through command redis-server) and the redis-client on as well (opened through the command redis-cli). This is what I am trying on Python:

import redis
r = redis.StrictRedis()
r.set("foo", "bar")
r.get("foo")

This prints bar as expected. However, if I go to my redis-client prompt and try get foo, it returns nil. In the same way, if I set a value in the redis-client itself, like set boo too, calling get boo in the client returns too as expected, however, in my Python client if I run r.get(boo), I get None.

Funny thing is, they are synced between their own instances. So if I open another Python command prompt and type r.get("foo"), it returns bar. In the same way, if I open another redis-cli instance and type get boo, I get too. It is only that the values between Python client and Redis client are not syncing. I even tried to enforce a bgsave from Python client after setting the value there, and it did trigger a save in the window where redis-server is running, but the values do not reflect in the redis-cli window even after that.

They were working perfectly fine until some days back, this has started happening sometime recently.

Any idea how to fix this?

1

There are 1 best solutions below

6
On

It seems to me that you are accessing (setting/getting values) through different dbs in your redis-cli and your redis-py instance. redis servers can have multiple key-value stores (or "tables") completely isolated from one another. These are referred throughout the documentation as db, and are uniquely identified by integers.

If you are instancing redis-py exactly as in your code snippet, with:

import redis
r = redis.StrictRedis()

Then you are using the constructor's default value for db, which is 0. (See documentation)

If the values differ between the Python implementation and redis-cli, then most likely your command line client is running on a different dbthan 0. You can check which db you are working on with the command CLIENT LIST, which will return your current database id.

To switch to the same database as the Python implementation, just run on your redis-cli:

SELECT 0

(See documentation)

Or, alternatively, you can call your redis-py constructor with the desired db parameter:

r = redis.StrictRedis(db=3) # 3 is an example value for your redis-cli's working db