REDIS Durability ? how to auto expire data?
Asked Answered
S

4

9

I use REDIS to store data (string) . ex: key "s1" store value "hello world". key "s2" store value "bye bye". I want s1 auto expire (free memory) after 5 minutes but s2 never expire. I use C#, .net 4.0 >> how to code ?. thanks

Summitry answered 30/6, 2014 at 4:17 Comment(0)
K
7

Documentation regarding EXPIRE allows you to set an EXPIRE value per key, in seconds.

EXPIRE s1 300

will expire the key s1 in 5 minutes.

See the documentation here: REDIS EXPIRE

If you are looking for C# code, I think it would depend on what library you are using to access REDIS. There are some other SO questions that may help, but also discuss the problem where expire did not work: Redis Expire does not work

Kaltman answered 30/6, 2014 at 4:26 Comment(1)
Even better - use SET's built in EX directive, i.e.: SET s1 "hello world" EX 300Imperceptive
T
11

If you plan to use Redis just as a cache where every key will have an expire set, you may consider using the following configuration instead (assuming a max memory limit of 2 megabytes as an example):

maxmemory 2mb
maxmemory-policy allkeys-lru

In this configuration there is no need for the application to set a time to live for keys using the EXPIRE command (or equivalent) since all the keys will be evicted using an approximated LRU algorithm as long as we hit the 2 megabyte memory limit.

Basically in this configuration Redis acts in a similar way to memcached. We have more extensive documentation about using Redis as an LRU cache.

Taiga answered 24/11, 2015 at 12:12 Comment(1)
This is a copied text from the redis doc without the link for the LRU redis cache config: redis.io/docs/manual/config/#configuring-redis-as-a-cacheAles
K
7

Documentation regarding EXPIRE allows you to set an EXPIRE value per key, in seconds.

EXPIRE s1 300

will expire the key s1 in 5 minutes.

See the documentation here: REDIS EXPIRE

If you are looking for C# code, I think it would depend on what library you are using to access REDIS. There are some other SO questions that may help, but also discuss the problem where expire did not work: Redis Expire does not work

Kaltman answered 30/6, 2014 at 4:26 Comment(1)
Even better - use SET's built in EX directive, i.e.: SET s1 "hello world" EX 300Imperceptive
H
2

For my case with REDIS Azure, I was looking to just assure that I didn't have to keep scaling my service for useless data. I found that the maxmemory LRU/FIFO seems to serve this purpose:

http://redis.io/topics/lru-cache

In REDIS Azure, one can set the Maxmemory Policy to any of the permitted LRU schemes in the management portal.

Holily answered 8/11, 2015 at 15:42 Comment(0)
C
1

By default redis don't expire keys, it sets expire time to -1, "s2" time is unilimited unless you set it.

Normally Redis keys are created without an associated time to live. The key will simply live forever, unless it is removed by the user in an explicit way, for instance using the DEL command.
The EXPIRE family of commands is able to associate an expire to a given key, at the cost of some additional memory used by the key. When a key has an expire set, Redis will make sure to remove the key when the specified amount of time elapsed.

http://redis.io/commands/expire (again)

Caesarea answered 30/6, 2014 at 15:18 Comment(2)
Wow, forever is a long time :-) There must be a way to clean the cache that is full of "old" keys (but also active ones)? i.e. retroactively set an default expiration time span?Holily
Sure, it'd be an easy loop. Iterate through all keys, check the TTL on each one, and set your EXPIRE for each one where the ttl is -1. Done.Mellott

© 2022 - 2024 — McMap. All rights reserved.