spring-data-redis
/spring-data-redis
Wspiera w Spring Boot cache, przechowywanie sesji i limity zapytań w Redis, ze wzorcem cache-aside i strategią TTL.
- Autor
- rrezartprebreza ↗
- Repozytorium
- spring-boot-skills ↗
- Licencja
- MIT
- Ostatnia zmiana repozytorium
- 22.10.2026
Ocena i bezpieczeństwo
- Pliki w paczce
- 5
- Skrypty
- 3
- Komentarze HTML
- 0
- Adresy URL w SKILL.md
- 0
Brak sygnałów. Repozytorium zmienione 22.10.2026, bez odwołań do wycofanych modeli i API.
MIT, otwarta: można używać i zmieniać, z zachowaniem informacji o autorze.
- 3 skrypty w paczce. Przejrzyj je przed instalacją, bo Claude może je uruchomić.
Skrypty: examples/bad-cache-service.java, examples/good-cache-service.java, templates/RedisConfig.java
Co robi i kiedy
Wspiera w Spring Boot cache, przechowywanie sesji i limity zapytań w Redis, ze wzorcem cache-aside i strategią TTL.
Oryginalny opis (EN)
Use when implementing caching, session storage, rate limiting, or any Redis integration. Covers cache-aside pattern, key naming, TTL strategy, and serialization config.
Jak działa
- Dependencies
- Configuration
- Key Naming Convention
- @Cacheable — Declarative Caching
- Manual Cache-Aside Pattern
- Rate Limiting with Redis
- application.yml
- Cache Stampede
- Gotchas
Podgląd SKILL.md
---
name: spring-data-redis
description: >
Use when implementing caching, session storage, rate limiting, or any Redis integration.
Covers cache-aside pattern, key naming, TTL strategy, and serialization config.
---
# Spring Data Redis
## Dependencies
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
```
## Configuration
```java
@Configuration
@EnableCaching
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(jsonSerializer()); // JSON, not Java serialize
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(jsonSerializer());
return template;
}
@Bean
public RedisCacheManager cacheManager(RedisConnectionFactory factory) {
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(10))
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jsonSerializer()))
.disableCachingNullValues();
return RedisCacheManager.builder(factory)
.cacheDefaults(config)
.transactionAware() // put/evict only after the surrounding transaction commits
.withCacheConfiguration("orders", config.entryTtl(Duration.ofMinutes(5)))
.withCacheConfiguration("products", config.entryTtl(Duration.ofHours(1)))
.build();
}
// Jackson 3 (tools.jackson) serializer. Default typing is OFF by default —
// enable it (scoped to trusted packages) so cached Objects round-trip to their
// real type instead of coming back as LinkedHashMap.
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();
}
}
```
## Key Naming Convention
```
{app}:{domain}:{id} → orders:order:uuid-here
{app}:{domain}:list:{filter} → orders:order:list:status:PENDING
{app}:session:{userId} → orders:session:uuid-here
{app}:ratelimit:{ip} → orders:ratelimit:192.168.1.1
```
## @Cacheable — Declarative Caching
```java
@Service
@RequiredArgsConstructor
public class ProductService {
@Cacheable(value = "products", key = "#id")
public ProductResponse findById(UUID id) {
return productRepository.findById(id)
.map(ProductResponse::from)
.orElseThrow(() -> new EntityNotFoundException("Product not found: " + id));
}
@CachePut(value = "products", key = "#result.id") // update cache after write
@Transactional
public ProductResponse update(UUID id, UpdateProductRequest request) {
Product product = productRepository.findById(id).orElseThrow();
product.update(request);
return ProductResponse.from(productRepository.save(product));
}
@CacheEvict(value = "products", key = "#id") // invalidate on delete
@Transactional
public void delete(UUID id) {
productRepository.deleteById(id);
}
@CacheEvict(value = "products", allEntries = true) // clear all
public void clearCache() {}
}
```
## Manual Cache-Aside Pattern
```java
@Service
@RequiredArgsConstructor
public class OrderCacheService {
private final RedisTemplate<String, Object> redisTemplate;
private final JsonMapper jsonMapper; // Jackson 3 — Boot auto-configures a JsonMapper bean
private static final Duration TTL = Duration.ofMinutes(5);
public Optional<OrderResponse> get(UUID orderId) {
String key = "orders:order:" + orderId;
Object cached = redisTemplate.opsForValue().get(key);
if (cached == null) return Optional.empty();
return Optional.of(jsonMapper.convertValue(cached, OrderResponse.class));
}
public void put(OrderResponse order) {
String key = "orders:order:" + order.id();
redisTemplate.opsForValue().set(key, order, TTL);
}
public void evict(UUID orderId) {
redisTemplate.delete("orders:order:" + orderId);
}
}
```
## Rate Limiting with Redis
```java
@Component
@RequiredArgsConstructor
public class RateLimiter {
private final RedisTemplate<String, String> redisTemplate;
// INCR and PEXPIRE in ONE atomic script: as two separate commands, a crash or
// timeout between them leaves a key with no TTL and locks the caller out forever.
private static final RedisScript<Long> INCR_WITH_TTL = RedisScript.of(
"local c = redis.call('INCR', KEYS[1]) " +
"if c == 1 then redis.call('PEXPIRE', KEYS[1], ARGV[1]) end " +
"return c", Long.class);
public boolean isAllowed(String identifier, int maxRequests, Duration window) {
String key = "ratelimit:" + identifier;
Long count = redisTemplate.execute(INCR_WITH_TTL, List.of(key), String.valueOf(window.toMillis()));
return count != null && count <= maxRequests;
}
}
```
## application.yml
```yaml
spring:
data:
redis:
host: ${REDIS_HOST:localhost}
port: ${REDIS_PORT:6379}
password: ${REDIS_PASSWORD:}
timeout: 2000ms
lettuce:
pool:
max-active: 10
max-idle: 5
min-idle: 2
cache:
type: redis
```
## Cache Stampede
When a hot key expires, every concurrent request misses at once and they all hammer the DB to recompute
the same value (the "thundering herd"). For expensive, high-traffic loads, let one caller compute while
the rest wait:
```java
// sync = true — only one thread computes the value; others block on it
@Cacheable(value = "products", key = "#id", sync = true)
public ProductResponse findById(UUID id) { ... }
```
`sync = true` serializes recomputation per key within a single instance. For a fleet-wide guarantee,
add a short Redis lock (`SETNX` with a TTL) around the recompute. Pair with jittered TTLs so a batch of
keys written together doesn't all expire on the same second.
## Gotchas
- Agent combines `@Transactional` with `@CachePut`/`@CacheEvict` on a non-transaction-aware cache manager — the cache can be updated before commit (or evicted then re-filled with stale data); build it with `.transactionAware()`
- Agent implements a rate limiter as separate `INCR` then `EXPIRE` calls — a failure between them leaves a TTL-less key that blocks the caller forever; use one Lua script
- Agent allow-lists only `java.util.` for default typing — `BigDecimal` is non-final and carries a type id, so cached prices/totals fail to deserialize; allow `java.math.` (and `java.time.`) too
- Agent uses Java serialization for values — always use JSON (`GenericJacksonJsonRedisSerializer`)
- Agent caches entities with JPA lazy fields — cache DTOs/response objects, not entities
- Agent uses no TTL — always set expiry, memory is not infinite
- Agent forgets `@EnableCaching` — `@Cacheable` silently does nothing without it
- Agent caches `null` values — use `.disableCachingNullValues()` to avoid storing misses
- Agent leaves hot keys unprotected — use `@Cacheable(sync = true)` to prevent stampede on expiry
- Agent gives every entry the same TTL — add jitter so keys don't expire in a synchronized wave
- Agent uses `GenericJackson2JsonRedisSerializer` — deprecated Jackson 2 API; use `GenericJacksonJsonRedisSerializer` (Jackson 3, `tools.jackson`)
- Agent expects default typing out of the box — the Jackson 3 serializer ships with it OFF; call `.enableDefaultTyping(validator)` or `@Cacheable` hits come back as `LinkedHashMap` and throw `ClassCastException`
- Agent registers `JavaTimeModule` on the mapper — Jackson 3 handles `java.time` natively; no module needed
- Agent declares a generic `ObjectMapper` bean to customize JSON — declare a `JsonMapper` bean or a `JsonMapperBuilderCustomizer` instead
- Migrating from Boot 3: Spring Session keys moved `spring.session.redis.*` → `spring.session.data.redis.*`
Pliki w paczce (5)
- SKILL.md 9 KB
- agents/openai.yaml 1 KB
- examples/bad-cache-service.java 1 KB
- examples/good-cache-service.java 1 KB
- templates/RedisConfig.java 3 KB
Wersje
| Repozytorium | Wynik | Licencja |
|---|---|---|
| Ta strona · rrezartprebreza/spring-boot-skills | 100 | MIT |
| rrezartprebreza/spring-boot-skills | 100 | MIT |
| claude-dev-suite/claude-dev-suite | 100 | MIT |
Migawka bazy z 13.11.2026 · wersja paczki fa5ee6dd23c9 (skrót SHA-256 całej paczki)
Pobrania w ostatnich 30 dniach: 0 · ranking
Zgłoś problem z tym skillem: szkodliwa treść, błędny opis albo prośba autora o usunięcie.
Podobne skille
- 100redis-data-structures-caching
Pomaga wdrożyć cache-aside i write-through w Redis: ciągi, hashe, zbiory posortowane i polityki wygasania TTL. - 100caching-strategies
Opisuje wzorce cache w aplikacjach: Redis, cache w pamięci, cache HTTP, CDN, unieważnianie, cache-aside, write-through i stale-while-revalidate. - 100cache-strategy
Projektuje i wdraża warstwy cache dla API i aplikacji webowych w Redis lub Memcached: cache-aside, write-through, write-behind, TTL, unieważnianie i ochrona przed stampede. - 100caching-strategies
Projektuje wielowarstwowe architektury cache dla aplikacji webowych: wzorce cache-aside i write-through, TTL, unieważnianie, Redis, CDN, nagłówki Cache-Control i ochrona przed stampede. - 100spring-cache
Wspiera abstrakcję Spring Cache w Spring Boot 3.x: @Cacheable, @CacheEvict, @CachePut, menedżery cache (Caffeine, Redis, EhCache), TTL, klucze i cache warunkowy. - 100redis-caching-strategy-and-invalidation-patterns
Projektuje strategię cache na Redis: cache-aside, write-through, write-behind, strategie TTL i unieważniania oraz ich awarie, jak thundering herd i stale reads.