I have a One Spring Hibernate Application. In my application, Recently i am implemented Spring data Redis.
spring-servlet.xml
<!-- redis connection factory -->
<bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:use-pool="true"/>
<!-- redis template definition -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnFactory"/>
And this redisTemplate
use in my ServiceImpl class.
RedisServiceImpl
@Autowired
private RedisTemplate<String, T> redisTemplate;
public RedisTemplate<String, T> getRedisTemplate() {
return redisTemplate;
}
public void setRedisTemplate(RedisTemplate<String, T> redisTemplate) {
this.redisTemplate = redisTemplate;
}
Now I added data in redisServer like this
public void putData(String uniqueKey, String key, Object results) {
redisTemplate.opsForHash().put(uniqueKey, key, results);
}
Now i want to remove Expire key.
I search in Google, But in google all are saying like this
redisTemplate.expire(key, timeout, TimeUnit);
In this expire method, We need to provide uniqueKey
instead of key
.
But I need to Expire key
instead of uniqueKey
.
So Please help me what can i do for expire Key
?
I am using Redis Version 3.2.100.
Instead of redis template ,Use Redis Cache Manager, pass redistemplate to cacheManager and use its set expires property to which is basically map of String & Long , you can add cache name and set its expiry time i.e time to live (TTL).
You can use setDefaultExpiration method of cacheManager to set same expiry time to all the cache.
To set TTL for keys, you may create multiple beans of cacheManager and set TTL for individual bean. Then as per your use, you can use required cachemanager. Here is what I have implemented.
To use above created cachemanager beans,
when we define
cacheManager ="cacheManager2"
in@Cacheable
, It will use TTL set forcacheManager2
defined in configuration. Same goes forcacheManager1
Actually You cannot expire or set the TTL for individual keys inside the Redis Hash. You can only expire or set TTL the complete hash. if you want to support this you have to change your data structure.
Here is the link for why it is not possible; and below are some excerpts from Redis expire
Also this link Allow to set an expiration on hash field might help you to change your data structure to handle expiry
Actually you can do it with Redisson framework (Redis based Data Grid for Java) using
RMapCache
object. It provides ability to setttl
andmaxIdle
per map entry. Example:You can adopt Quartz for this purpose (implementing ttl for a Redis record). If you use Spring Boot, it autoconfigures Scheduler for you. Thus you can autowire it directly to your service layer.
Then you need to implement a job like this (in this example I am using Spring Redis Data):
}
Then you can encapsulate some logic for creating JobDetail and Trigger
And finally, right after you saved you record in Redis repository, you need to schedule a job for removal this record (uniqueKey) from it like this: