Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 基于模式匹配从redis中删除对象_Python_Redis - Fatal编程技术网

Python 基于模式匹配从redis中删除对象

Python 基于模式匹配从redis中删除对象,python,redis,Python,Redis,我使用Redis作为应用程序的数据存储/缓存。在将数据酸洗成字符串后,我将数据推送到Redis实例。我的数据是一个Python类对象(即,键值对,但被pickle成字符串)。我正在Python中使用Redis库 我的数据会定期被推送,并且可能由于主机停机等原因,来自某个主机的数据会停止被推送。我希望在主机停机后能够从该主机清除数据。我有一个触发器,可以通知我的应用程序主机停机等情况 但是,我不确定如何通过取消数据酸洗并检查数据中的某个键值对来高效地从Redis中清除数据。如果可能的话,我想在适当

我使用Redis作为应用程序的数据存储/缓存。在将数据酸洗成字符串后,我将数据推送到Redis实例。我的数据是一个Python类对象(即,键值对,但被pickle成字符串)。我正在Python中使用Redis库

我的数据会定期被推送,并且可能由于主机停机等原因,来自某个主机的数据会停止被推送。我希望在主机停机后能够从该主机清除数据。我有一个触发器,可以通知我的应用程序主机停机等情况

但是,我不确定如何通过取消数据酸洗并检查数据中的某个键值对来高效地从Redis中清除数据。如果可能的话,我想在适当的地方这样做。在此方面的任何帮助都将不胜感激

编辑:

这就是我用来将数据推送到redis的方法:

self.redis.zadd("mymsgs", pickle.dumps(msg), int(time.time()+360))
消息本身不符合格式:

{'hostname': 'abc1', 'version': 'foo', 'uptime': 'bar'}

如果我理解正确,我建议(当然,如果可能的话)您稍微更改一下键的格式。我建议不要使用通用的
mymsgs
作为密钥,而是以某种方式将主机名添加到密钥本身。例如,它可以是来自主机名的mysgs

由于您可以使用通配符获取密钥,因此当您想要获取所有消息时,您可以从*中列出与
mysgs\u匹配的密钥,然后获取这些密钥的值。这样,当您知道名为
hostname
的主机名已关闭时,可以通过执行
delete(“mysgs\u from\u 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'}]

你不能在Redis密钥中添加主机名吗?这样,您就可以使用类似
hostname foo*
?或者你可以研究一段时间后自动删除钥匙的计时器是否有帮助?(我指的是)?@BorrajaX所以事情是这样的。我存储在Redis中的数据是dict的一个pickle版本,其中包含主机名及其作为kvp的值。我对Redis是个新手。没有其他方法可以从数据存储中删除吗?当您说
kvp
。。。那是Redis的钥匙吗?因为,好吧,您只能在键中使用通配符,而不能在值中使用通配符。你的钥匙看起来怎么样?你能编辑这个问题来提供一个例子吗?请看我上面的编辑<代码>kvp
此处不引用Redis键。我的理解是,在我的情况下,
mymsgs
将是Redis的钥匙?如果我错了,请纠正我。。。。另外,在一个键中,有没有一种方法可以根据字符串模式匹配来删除数据?谢谢!我将尝试一下,并将问题标记为已回答,如果我能让它工作:)我希望它有帮助:)