I need to expire all keys in redis hash, which are older then 1 month.
相关问题
- facebook error invalid key hash for some devices
- Getting Redis Master address from Sentinel C#
- Change first key of multi-dimensional Hash in perl
- Configuring Redis to play nice with AppHarbor
- Bool.hashValue valid to convert to Int?
This is not possible, for the sake of keeping Redis simple.
Quoth Antirez, creator of Redis:
You can. Here is an example.
Use EXPIRE or EXPIREAT command.
If you want to expire specific keys in the hash older then 1 month. This is not possible. Redis expire command is for all keys in the hash. If you set daily hash key, you can set a keys time to live.
You could store key/values in Redis differently to achieve this, by just adding a prefix or namespace to your keys when you store them e.g. "hset_"
Get a key/value
GET hset_key
equals toHGET hset key
Add a key/value
SET hset_key value
equals toHSET hset key
Get all keys
KEYS hset_*
equals toHGETALL hset
Get all vals should be done in 2 ops, first get all keys
KEYS hset_*
then get the value for each keyAdd a key/value with TTL or expire which is the topic of question:
SET hset_key value EXPIRE hset_key
Regarding a NodeJS implementation, I have added a custom
expiryTime
field in the object I save in the HASH. Then after a specific period time, I clear the expired HASH entries by using the following code:There is a Redisson java framework which implements hash
Map
object with entry TTL support. It useshmap
andzset
Redis objects under the hood. Usage example:This approach is quite useful.
You can expire Redis hashes in ease, Eg using python
This will expire all child keys in hash hashed_user after 10 seconds
same from redis-cli,
after 10 seconds