How to set the expiration policy for Redis cache?

In Redis, setting a cache expiration strategy is typically achieved by using expiration time. The EXPIRE command can be used to set the expiration time for the cache, so that when the expiration time is reached, the cache will automatically become invalid and be deleted.

For example, you can use the following command to set the cache for key1 to expire after 60 seconds:

EXPIRE key1 60

Additionally, you can use the TTL command to check the remaining expiration time of a key, as shown below:

TTL key1

Additionally, you can set a global default expiration time by modifying the Redis configuration file to ensure all cached keys have the same expiry strategy. This can be accomplished by adjusting the timeout parameter in the redis.conf file.

Leave a Reply 0

Your email address will not be published. Required fields are marked *