我正在编写一个django管理命令来处理我们的一些redis缓存.基本上,我需要选择所有键,确认某种模式(例如:"prefix:*")并删除它们.
我知道我可以使用cli来做到这一点:
redis-cli KEYS "prefix:*" | xargs redis-cli DEL
Run Code Online (Sandbox Code Playgroud)
但我需要在应用程序中执行此操作.所以我需要使用python绑定(我使用的是py-redis).我已经尝试将列表输入到删除中,但它失败了:
from common.redis_client import get_redis_client
cache = get_redis_client()
x = cache.keys('prefix:*')
x == ['prefix:key1','prefix:key2'] # True
Run Code Online (Sandbox Code Playgroud)
# 现在
cache.delete(x)
Run Code Online (Sandbox Code Playgroud)
#返回0.没有删除
我知道我可以迭代x:
for key in x:
cache.delete(key)
Run Code Online (Sandbox Code Playgroud)
但这将失去redis的速度和滥用其功能.是否有py-redis的pythonic解决方案,没有迭代和/或cli?
谢谢!
我想使用redis'pubsub来传输一些消息,但不希望被阻止使用listen,如下面的代码:
import redis
rc = redis.Redis()
ps = rc.pubsub()
ps.subscribe(['foo', 'bar'])
rc.publish('foo', 'hello world')
for item in ps.listen():
if item['type'] == 'message':
print item['channel']
print item['data']
Run Code Online (Sandbox Code Playgroud)
最后一for节将阻止.我只是想检查一个给定的通道是否有数据,我该如何做到这一点?有check类似的方法吗?
我已经阅读了redis-python文档并在线搜索,我找不到任何关于db参数的信息Redis().有什么用?
我在redis-cli中测试了所有事务命令(MULTI,EXEC,WATCH,DISCARD).但是,当我尝试使用redis-py时,发生以下错误:
AttributeError:'Redis'对象没有属性'multi'
我试过以下代码片段:
import redis,time
r = redis.Redis()
try:
r.set("transError",10)
r.watch("transError")
var = r.get("transError")
var = int(var) + 1
print "Run other client to simulate an error without transaction"
time.sleep(4)
r.multi()
r.set("transError",var)
r.execute()
print "Value in first client",r.get("transError")
except redis.WatchError:
print "Value Altered"
Run Code Online (Sandbox Code Playgroud)
我见过使用multi()和execute()的代码示例,但它们对我不起作用.有帮助吗?
我玩烧瓶微框架,并希望在redis中缓存一些统计数据.假设我有这个词:
mydict = {}
mydict["test"] = "test11"
Run Code Online (Sandbox Code Playgroud)
我把它保存到了redis
redis.hmset("test:key", mydict)
Run Code Online (Sandbox Code Playgroud)
但恢复后
stored = redis.hgetall("test:key")
print(str(stored))
Run Code Online (Sandbox Code Playgroud)
我觉得很奇怪{b'test': b'test11'}所以stored.get("test")给我没有
mydictstr方法结果看起来很好{'test': 'test11'}.那么,为什么这个二进制标记添加到恢复数据?我还检查了redis-cli并且没有看到明确的b标记.hgetall出了什么问题?
请考虑以下示例
>>import redis
>>redis_db_url = '127.0.0.1'
>>r = redis.StrictRedis(host = redis_db_url,port = 6379,db = 0)
>>r.sadd('a',1)
>>r.sadd('a',2)
>>r.sadd('a',3)
>>r.smembers('a')
Run Code Online (Sandbox Code Playgroud)
[+]输出:set(['1','3','2'])
>>r.sadd('a',set([3,4]))
>>r.smembers('a')
Run Code Online (Sandbox Code Playgroud)
[+]输出:set(['1','3','2','set([3,4])'])
>>r.sadd('a',[3,4])
>>r.smember('a')
Run Code Online (Sandbox Code Playgroud)
[+] set(['1','[3,4]','3','2','set([3,4])'])
根据https://redis-py.readthedocs.org/en/latest/ sadd(name,*values)中的官方文档添加值来设置名称
那么它是一个错误还是我错过了什么?
我正在尝试使用 python 在 redis 中订阅 keyspace 事件。我希望.listen()在调用之后不要使用 for 循环.psubscribe()。这可能吗?
我已经启用了所有键空间事件KEA。
def subscribe(self, key, handler):
# this function never gets called, if I don't add the for-loop with listen() below
def event_handler(msg):
print('Handler', msg)
redis_server = StrictRedis(host='localhost', port=6379, db=0)
pubsub = redis_server.pubsub()
subscribe_key = '*'
pubsub.psubscribe(**{subscribe_key: event_handler})
# without the following for-loop with listen, the callback never fires. I hope to get rid of this.
for item in pubsub.listen():
pass
Run Code Online (Sandbox Code Playgroud) 在下面的代码中,管道超时是2秒?
client = redis.StrictRedis(host=host, port=port, db=0, socket_timeout=2)
pipe = client.pipeline(transaction=False)
for name in namelist:
key = "%s-%s-%s-%s" % (key_sub1, key_sub2, name, key_sub3)
pipe.smembers(key)
pipe.execute()
Run Code Online (Sandbox Code Playgroud)
在redis中,set"key"中有很多成员.它始终返回错误,如下所示:
error Error while reading from socket: ('timed out',)
Run Code Online (Sandbox Code Playgroud)
如果我将socket_timeout值修改为10,则返回ok.
参数"socket_timeout"不是指连接超时吗?但它看起来像响应超时.
redis-py版本是2.6.7.
我正在尝试在集群模式下将redis-py与 redis一起使用,但我无法让它工作。我看到redis-py-cluster可以工作,但是我更喜欢 redis-py,因为我一直在使用它并且它是推荐的 client。
除了python-redis-lock模块为lock对象提供contextmanager之外 - 与redispy模块获得的lock对象相比有什么不同?python-redis-lock有什么特别之处?
rc = Redis.from_url(settings.BROKER_URL)
lock_str = "bld-%s-lock" % bld_id
Run Code Online (Sandbox Code Playgroud)
使用redispy:
lock = rc.lock(lock_str)
Run Code Online (Sandbox Code Playgroud)
使用python-redis-lock:
lock = redis_lock.Lock(rc, lock_str)
Run Code Online (Sandbox Code Playgroud)