我正在使用Redis作为我的应用程序的数据存储/缓存。我将数据腌制成字符串后将数据推送到Redis实例。我的数据是一个Python类对象(即键值对,但是被腌成一个字符串)。我在Python中使用Redis lib。
我的数据会被定期推送,并且由于主机发生故障,来自某个主机的数据可能会停止被推送等等。我希望能够在主机发生故障后从该主机中清除数据。我有一个触发器,通知我的应用程序关于主机关闭等等。
但是,我不确定如何通过取消数据并检查数据中的某个键值对,以有效的方式从Redis中清除数据。如果可能的话,我想这样做。任何有关这方面的帮助都将非常感激!
修改
这是我用来将数据推送到redis的方法:
self.redis.zadd("mymsgs", pickle.dumps(msg), int(time.time()+360))
邮件本身不符合以下格式:
{'hostname': 'abc1', 'version': 'foo', 'uptime': 'bar'}
答案 0 :(得分:1)
如果我理解正确的话,我建议(如果可能的话)是你改变了一下键的格式。我建议不要使用通用mymsgs
作为密钥,而是以某种方式将主机名添加到密钥本身。例如,它可以是mysgs_from_HOSTNAME
。
由于您可以使用通配符来获取密钥,因此当您想要所有消息时,您只需列出匹配mysgs_from_*
的密钥,然后获取这些密钥的值。这样,当您知道名为HOSTNAME
的主机名已关闭时,您可以通过执行delete("mysgs_from_HOSTNAME"
)来快速清除其所有条目。
见这个例子:
import redis
import time
import pickle
redis_connection = redis.Redis(host='localhost', port=6379, db=0)
# This "for" loop is just a simple populator, to put a bunch of key/values in Redis
for hostname in ['abc1', 'foo2', 'foo3']:
msg = {'hostname': hostname, 'version': 'foo', 'uptime': 'bar'}
# Step 1, store the data using a key that contains the hostname:
redis_key = "messages_from_host_%s" % hostname
redis_connection.zadd(redis_key, pickle.dumps(msg), int(time.time() + 360))
# Ok... I have some sample data in Redis now...
# Shall we begin?...
# Let's say I wanna get all the messages from all the hosts:
# First, I find all the keys that can contain messages from hosts
matching_keys = redis_connection.keys("messages_from_host_*")
print "Got these keys that match what I wanna get: %s" % matching_keys
# Then I iterate through the keys and get the actual zrange (~value) of each
print "These are the messages from all those hosts:"
for matching_key in matching_keys:
messages = [pickle.loads(s) for s in redis_connection.zrange(matching_key, 0, -1)]
print messages
# Let's say that now, I discover that host called `foo2` is down, and I want
# to remove all its information:
redis_connection.delete("messages_from_host_foo2")
# All the entries referred to the host `foo2` should be gone:
print "Now, I shouldn't bee seing information from `foo2`"
matching_keys = redis_connection.keys("messages_from_host_*")
for matching_key in matching_keys:
messages = [pickle.loads(s) for s in redis_connection.zrange(matching_key, 0, -1)]
print messages
哪个输出:
Got these keys that match what I wanna get: ['messages_from_host_foo2', 'messages_from_host_foo3', 'messages_from_host_abc1']
These are the messages from all those hosts:
[{'uptime': 'bar', 'hostname': 'foo2', 'version': 'foo'}]
[{'uptime': 'bar', 'hostname': 'foo3', 'version': 'foo'}]
[{'uptime': 'bar', 'hostname': 'abc1', 'version': 'foo'}]
Now, I shouldn't bee seing information from `foo2`
[{'uptime': 'bar', 'hostname': 'foo3', 'version': 'foo'}]
[{'uptime': 'bar', 'hostname': 'abc1', 'version': 'foo'}]