写入Redis中的几个数据库用于python

wyp*_*wyp 6 python database redis key-value-store

我想将一些值写入不同的数据库,这是我的代码:

import redis

r1 = redis.Redis(host='127.0.0.1', port=6379, db = 'db1')
r2 = redis.Redis(host='127.0.0.1', port=6379, db = 'db2')

numList = ['4', '3', '2', '1']

for num in numList:
   r1.lpush('a', num)
   r2.lpush('a', 'test')

print r1.lrange('a',start=0,end=-1)
print r2.lrange('a',start=0,end=-1)
Run Code Online (Sandbox Code Playgroud)

然后我得到了这个结果:

['test', '1', 'test', '2', 'test', '3', 'test', '4']
['test', '1', 'test', '2', 'test', '3', 'test', '4']
Run Code Online (Sandbox Code Playgroud)

虽然我使用不同的数据库,但对于相同的密钥,所有的值都被放入.谢谢.

fre*_*ish 18

DB应该是从零开始的数字索引 (显然限制为15).尝试使用

r1 = redis.Redis(host='127.0.0.1', port=6379, db = 0)
r2 = redis.Redis(host='127.0.0.1', port=6379, db = 1)
Run Code Online (Sandbox Code Playgroud)

  • 没有 15 个数据库的限制。您可以在 redis.config 文件中将数据库数量设置为您想要的数量,然后选择 0 到 'databases'-1 之间的 dbid。 (2认同)
  • @MikeL 我大约 6 年前写了这个答案。我不记得为什么要写这个限制了。也许当时有这样的限制,但现在已经不复存在了。谁知道?我不记得了。 (2认同)