Search code examples
spring-bootcachingredisjedisspring-data-redis

Does Redis Support setting Expiration at the Field (hashKey) level in a HASH?


Implementation Context

I have an token based API Implementation. The logic at a high level is token is issued by a API, on subsequent calls to other API this token must be used. One API fetches huge data from DB and stores it in cache (I am using Redis here). The cache has the key as Session:Functionality, field (hashKey) as the token and value in Json format as the data fetched up from the DB. In subsequent calls to API , token is validated at the gateway level, API checks if the data is already present in REDIS.

Here it queries the HASH in Redis with the Key as Session:Functionality & the hashkey as token. If the data is in the Redis the API sends a quick response, otherwise it fetches the data from the downstream systems.

So in Hash in Redis looks like,

enter image description here

Question

Now my question is , is is possible to set a unique timeout at haskkey level for every token as the underlying data may change and I want to keep a timeout of 5 mins at this level.

I am using Spring Spring Boot Jedis , so if you can post the specific method for setting the timeout with this framework it will be great.


Solution

  • No.

    Hi, it is not possible, either use a different top-level key for that specific field, or store along with the filed another field with an expire time, fetch both, and let the application understand if it is still valid or not based on current time.

    From antirez - author of redis https://github.com/antirez/redis/issues/167#issuecomment-2559040