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
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
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.
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
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.
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)
TTL
on each one, and set your EXPIRE
for each one where the ttl is -1. Done. –
Mellott © 2022 - 2024 — McMap. All rights reserved.