Reputation:
I have a hash in redis. It has 20 key/value pairs. I want to update it hourly and remove old elements. What is the best way to remove HASH key's content and fill with with fresh values. Since HASH is part of an API, I don't want to cause any race condition when I am removing all elements from the HASH. I don't want to use EXPIRE
because I want to have a control over schedule.
I created following mock up. Assume that Faker()
is my API that gets fresh values. I want to add new values to HASH and get rid of the old ones.
import redis
import json
from faker import Faker
try:
r = redis.StrictRedis(host='localhost', port=6379, db=0)
fake = Faker()
for _ in range(20):
userId = fake.uuid4()
user = {"name":fake.name(),"address":fake.address(),"country":fake.country()}
r.hset('users',userId,json.dumps(user))
except Exception as e:
print ('Error',e)
Upvotes: 2
Views: 2067
Reputation: 49942
How about deleting the Hash with a DEL
command? You can wrap it in a transaction that also sets (HSET
) the new values in it.
Upvotes: 4
Reputation: 911
"What is the best way to remove HASH key's content and fill with with fresh values." just update it, at least if you're updating... so hset(hash, key, newvalue) or hmset(hash, newvalues_dictionary) - whats bad with this approach?
Upvotes: 0