1

I am hashing a value to a key in my Redis.

_redis.GetDatabase(0).HashSet("db", "key1" , "value");

I also want to set the expiration time of 30 days to this key. I did not find any way to do so. Are there any possible ways to set an expiration time right after setting the value?

Prabhas Kumra
  • 541
  • 7
  • 13
  • Does this answer your question? [How to "EXPIRE" the "HSET" child key in redis?](https://stackoverflow.com/questions/16545321/how-to-expire-the-hset-child-key-in-redis) – for_stack Feb 11 '22 at 00:57

2 Answers2

1

In example you provided "db" is a hash-key, and "key1" is in fact a sub-key (see: https://redis.com/ebook/part-1-getting-started/chapter-1-getting-to-know-redis/1-2-what-redis-data-structures-look-like/1-2-4-hashes-in-redis/)

You cannot expire sub-keys (see: Redis: To set timeout for a key value pair in Set)

In order to expire hash-key you can use:

_redis.GetDatabase(0).KeyExpire("db", TimeSpan.FromDays(30));
0
// using the async api  
await db.KeyExpireAsync("db", TimeSpan.FromDays(30));
Bnaya
  • 715
  • 5
  • 7