rrezartprebreza/spring-boot-skills / skills/spring-boot-4/spring-data-redis
spring-data-redis
VerifiedUse when implementing caching, session storage, rate limiting, or any Redis integration. Covers cache-aside pattern, key naming, TTL strategy, and serialization config.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 13.5 KB |
| agents/openai.yaml | config | 192 B |
| examples/bad-cache-service.java | other | 1.3 KB |
| examples/good-cache-service.java | other | 1.1 KB |
| templates/AtomicWindowLimiter.java | other | 1.6 KB |
| templates/CoalescingRedisCacheManager.java | other | 1.2 KB |
| templates/OptionalCacheLookup.java | other | 1010 B |
| templates/RedisConfig.java | other | 3.6 KB |
templates/RedisConfig.java
View on GitHubpackage com.example.config;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.cache.BatchStrategies;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJacksonJsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import tools.jackson.databind.jsontype.BasicPolymorphicTypeValidator;
import java.time.Duration;
import java.util.Map;
@Configuration
@EnableCaching
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(jsonSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(jsonSerializer());
return template;
}
@Bean
public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {
RedisCacheConfiguration defaultConfig = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(30))
.disableCachingNullValues()
.serializeValuesWith(
RedisSerializationContext.SerializationPair
.fromSerializer(jsonSerializer()));
// Per-cache TTL overrides
Map<String, RedisCacheConfiguration> cacheConfigs = Map.of(
"orders", defaultConfig.entryTtl(Duration.ofMinutes(15)),
"products", defaultConfig.entryTtl(Duration.ofHours(1)),
"users", defaultConfig.entryTtl(Duration.ofMinutes(5))
);
// Redis 4 can otherwise schedule writes asynchronously with Lettuce.
var writer = RedisCacheWriter.create(connectionFactory,
config -> config.immediateWrites().batchStrategy(BatchStrategies.scan(1000)));
return RedisCacheManager.builder(writer)
.cacheDefaults(defaultConfig)
.transactionAware() // put/evict only after the surrounding transaction commits
.withInitialCacheConfigurations(cacheConfigs)
.build();
}
/**
* Jackson 3 (tools.jackson) JSON serializer.
* - java.time works out of the box — no JavaTimeModule to register.
* - Default typing is OFF by default: enable it with a validator scoped to
* trusted packages so cached Objects deserialize to their real type instead
* of LinkedHashMap. Never use enableUnsafeDefaultTyping() on data an
* attacker could write.
*/
private GenericJacksonJsonRedisSerializer jsonSerializer() {
return GenericJacksonJsonRedisSerializer.builder()
.enableDefaultTyping(BasicPolymorphicTypeValidator.builder()
.allowIfSubType("com.example.") // your DTO packages
.allowIfSubType("java.util.") // collections
.allowIfSubType("java.math.") // BigDecimal is non-final, so it carries a type id
.allowIfSubType("java.time.")
.build())
.build();
}
}
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack." | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |