Skip to content

Commit 6749fba

Browse files
committed
Added TTL support for Redis backend
1 parent 4ae9910 commit 6749fba

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,3 +292,6 @@ This project is licensed under the MIT License. See the [LICENSE](LICENSE) file
292292
- In-Memory and Redis storage backends
293293
- Full Spring Boot auto-configuration
294294
- Complete JavaDoc documentation
295+
296+
### Version 0.2.0
297+
- Added TTL support for Redis backend

spring-boot-starter-cache-tags/src/main/java/io/github/intellinside/cache/tags/CacheTagsAutoConfiguration.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
1414
import org.springframework.cache.CacheManager;
1515
import org.springframework.context.annotation.Bean;
16+
import org.springframework.data.redis.cache.RedisCacheConfiguration;
1617
import org.springframework.data.redis.core.StringRedisTemplate;
1718

1819
/**
@@ -78,8 +79,9 @@ public TaggingCacheManagerBeanPostProcessor taggingCacheManagerBeanPostProcessor
7879
@Bean
7980
@ConditionalOnProperty(value = "spring.cache.type", havingValue = "redis")
8081
@ConditionalOnBean(StringRedisTemplate.class)
81-
public CacheTagsStore redisCacheTagsStore(StringRedisTemplate redisTemplate) {
82-
return new RedisCacheTagStore(redisTemplate);
82+
public CacheTagsStore redisCacheTagsStore(StringRedisTemplate redisTemplate,
83+
RedisCacheConfiguration redisCacheConfiguration) {
84+
return new RedisCacheTagStore(redisTemplate, redisCacheConfiguration);
8385
}
8486

8587
/**

spring-cache-tags-redis/src/main/java/io/github/intellinside/cache/tags/store/RedisCacheTagStore.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package io.github.intellinside.cache.tags.store;
22

33
import lombok.RequiredArgsConstructor;
4+
import org.springframework.data.redis.cache.RedisCacheConfiguration;
45
import org.springframework.data.redis.core.RedisTemplate;
56

7+
import java.time.Duration;
68
import java.util.HashMap;
79
import java.util.HashSet;
810
import java.util.Map;
@@ -35,7 +37,7 @@
3537
*
3638
* <p>
3739
* <b>Example:</b>
38-
*
40+
*
3941
* <pre>
4042
* SET tag:user:123 user-cache:123:details
4143
* SET tag:user:123 user-cache:123:permissions
@@ -49,6 +51,7 @@
4951
@RequiredArgsConstructor
5052
public class RedisCacheTagStore implements CacheTagsStore {
5153
private final RedisTemplate<String, String> redisTemplate;
54+
private final RedisCacheConfiguration redisCacheConfiguration;
5255

5356
/**
5457
* Associates multiple tags with a cache key by storing them in Redis Sets.
@@ -64,7 +67,15 @@ public class RedisCacheTagStore implements CacheTagsStore {
6467
@Override
6568
public void addMappings(Set<String> tags, String cacheName, Object key) {
6669
String serializedKey = cacheName + ":" + key.toString();
67-
tags.forEach(tag -> redisTemplate.opsForSet().add("tag:" + tag, serializedKey));
70+
tags.stream()
71+
.map(tag -> "tag:" + tag)
72+
.forEach(tag -> {
73+
redisTemplate.opsForSet().add(tag, serializedKey);
74+
Duration timeToLive = redisCacheConfiguration.getTtlFunction().getTimeToLive(tag, null);
75+
if (timeToLive.isPositive()) {
76+
redisTemplate.expire(tag, timeToLive);
77+
}
78+
});
6879
}
6980

7081
/**

0 commit comments

Comments
 (0)