Repository navigation
Expand file tree
/
Copy pathExpSpringCacheRedis.java
More file actions
263 lines (211 loc) · 11.4 KB
/
Copy pathExpSpringCacheRedis.java
File metadata and controls
263 lines (211 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
/**
* Simulación de Spring Cache con Redis como backend.
*
* Conceptos demostrados:
* - @Cacheable: cache-aside transparente (miss → ejecuta método → guarda en Redis)
* - @CacheEvict: invalida la entry al actualizar el dato en DB
* - @CachePut: siempre ejecuta el método Y actualiza Redis (sin consultar caché)
* - Cache key generation: método + parámetros como key por defecto
* - Contadores de "database calls" para demostrar el ahorro
*/
public class ExpSpringCacheRedis {
// ─────────────────────────────────────────────
// CACHE: simula Redis como backend de Spring Cache
// ─────────────────────────────────────────────
static class Cache {
private final String nombre;
private final Map<String, Object> store = new HashMap<>();
private int hits = 0;
private int misses = 0;
Cache(String nombre) {
this.nombre = nombre;
}
Optional<Object> get(String key) {
if (store.containsKey(key)) {
hits++;
System.out.printf(" [CACHE HIT] %s::%s%n", nombre, key);
return Optional.of(store.get(key));
}
misses++;
System.out.printf(" [CACHE MISS] %s::%s → ejecutando método...%n", nombre, key);
return Optional.empty();
}
void put(String key, Object value) {
store.put(key, value);
System.out.printf(" [CACHE PUT] %s::%s = %s%n", nombre, key, value);
}
void evict(String key) {
if (store.remove(key) != null) {
System.out.printf(" [CACHE EVICT] %s::%s eliminada%n", nombre, key);
}
}
void evictAll() {
store.clear();
System.out.printf(" [CACHE CLEAR] %s — todas las entries eliminadas%n", nombre);
}
boolean contains(String key) { return store.containsKey(key); }
int hits() { return hits; }
int misses() { return misses; }
void printStats() {
System.out.printf(" Cache '%s': hits=%d, misses=%d, entries=%d%n",
nombre, hits, misses, store.size());
}
}
// ─────────────────────────────────────────────
// CACHE MANAGER: gestiona múltiples caches nombrados
// En Spring: RedisCacheManager, con TTL y serialización configurables
// ─────────────────────────────────────────────
static class CacheManager {
private final Map<String, Cache> caches = new HashMap<>();
Cache getCache(String nombre) {
return caches.computeIfAbsent(nombre, Cache::new);
}
}
// ─────────────────────────────────────────────
// ENTIDAD Y REPOSITORIO (simulan DB)
// ─────────────────────────────────────────────
record Producto(String id, String nombre, double precio) {}
static class ProductoRepository {
private final Map<String, Producto> db = new HashMap<>();
private final AtomicInteger dbCalls = new AtomicInteger(0);
ProductoRepository() {
db.put("P01", new Producto("P01", "Laptop", 1200.0));
db.put("P02", new Producto("P02", "Monitor", 350.0));
db.put("P03", new Producto("P03", "Teclado", 80.0));
}
Producto findById(String id) {
int call = dbCalls.incrementAndGet();
System.out.printf(" [DB CALL #%d] SELECT * FROM productos WHERE id='%s'%n", call, id);
return db.get(id);
}
Producto save(Producto p) {
int call = dbCalls.incrementAndGet();
System.out.printf(" [DB CALL #%d] UPDATE productos SET nombre='%s', precio=%.2f WHERE id='%s'%n",
call, p.nombre(), p.precio(), p.id());
db.put(p.id(), p);
return p;
}
int getDbCalls() { return dbCalls.get(); }
}
// ─────────────────────────────────────────────
// PRODUCTO SERVICE: simula @Cacheable, @CacheEvict, @CachePut
// ─────────────────────────────────────────────
static class ProductoService {
private final ProductoRepository repository;
private final Cache cache;
ProductoService(ProductoRepository repository, CacheManager cacheManager) {
this.repository = repository;
this.cache = cacheManager.getCache("productos");
}
// @Cacheable(value = "productos", key = "#id")
// Spring AOP intercepta la llamada, genera key, busca en Redis.
// Si hit → devuelve sin ejecutar el método.
// Si miss → ejecuta, guarda en Redis, devuelve.
Producto findById(String id) {
// Spring genera la key: "productos::" + id
String cacheKey = id;
return (Producto) cache.get(cacheKey).orElseGet(() -> {
Producto p = repository.findById(id);
if (p != null) cache.put(cacheKey, p);
return p;
});
}
// @CacheEvict(value = "productos", key = "#id")
// Invalida la entry en Redis. El siguiente findById hará miss y recargará de DB.
void evictFromCache(String id) {
System.out.printf(" [@CacheEvict] invalidando key '%s'%n", id);
cache.evict(id);
}
// @CachePut(value = "productos", key = "#result.id")
// Siempre ejecuta el método (guarda en DB) Y actualiza Redis.
// Sin consultar caché: garantiza que Redis queda actualizado tras cada escritura.
Producto actualizar(Producto producto) {
Producto saved = repository.save(producto);
// @CachePut: actualiza independientemente de si había hit o miss
cache.put(saved.id(), saved);
System.out.printf(" [@CachePut] Redis actualizado con nueva versión de '%s'%n", saved.id());
return saved;
}
// @CacheEvict(value = "productos", allEntries = true)
// Limpia todo el caché de "productos". Útil tras migraciones masivas.
void evictAll() {
cache.evictAll();
}
}
// ─────────────────────────────────────────────
// DEMO: @Cacheable vs @CachePut vs @CacheEvict
// ─────────────────────────────────────────────
static void demoCacheable() {
System.out.println("══ Demo @Cacheable ══");
CacheManager cacheManager = new CacheManager();
ProductoRepository repo = new ProductoRepository();
ProductoService service = new ProductoService(repo, cacheManager);
System.out.println("\n Llamada 1 → MISS (primera vez):");
Producto p1 = service.findById("P01");
System.out.printf(" Resultado: %s%n", p1);
System.out.println("\n Llamada 2 → HIT (mismo id, ya en caché):");
Producto p1cached = service.findById("P01");
System.out.printf(" Resultado: %s%n", p1cached);
System.out.println("\n Llamada 3 → HIT (tercera llamada, sigue en caché):");
service.findById("P01");
System.out.println("\n Llamada 4 → MISS (primer acceso a P02):");
service.findById("P02");
System.out.printf("%n DB calls reales: %d (de 4 llamadas al servicio)%n", repo.getDbCalls());
cacheManager.getCache("productos").printStats();
}
static void demoCacheEvict() {
System.out.println("\n══ Demo @CacheEvict ══");
CacheManager cacheManager = new CacheManager();
ProductoRepository repo = new ProductoRepository();
ProductoService service = new ProductoService(repo, cacheManager);
System.out.println("\n Cargando P01 en caché (MISS):");
service.findById("P01");
System.out.println("\n Cargando P01 de nuevo (HIT):");
service.findById("P01");
System.out.println("\n Simulando actualización → @CacheEvict:");
service.evictFromCache("P01");
System.out.println("\n Siguiente acceso → MISS (caché invalidado, recarga de DB):");
service.findById("P01");
System.out.printf("%n DB calls totales: %d%n", repo.getDbCalls());
}
static void demoCachePut() {
System.out.println("\n══ Demo @CachePut ══");
CacheManager cacheManager = new CacheManager();
ProductoRepository repo = new ProductoRepository();
ProductoService service = new ProductoService(repo, cacheManager);
System.out.println("\n Actualizando P02 con @CachePut:");
Producto actualizado = service.actualizar(new Producto("P02", "Monitor 4K", 420.0));
System.out.printf(" Producto actualizado: %s%n", actualizado);
System.out.println("\n Leyendo P02 → HIT (caché caliente gracias a @CachePut):");
Producto leído = service.findById("P02");
System.out.printf(" Leído desde caché: %s%n", leído);
System.out.printf("%n Nota: @CachePut evita que el siguiente caller haga miss.%n");
System.out.printf(" DB calls totales: %d (1 update + 0 selects)%n", repo.getDbCalls());
}
// ─────────────────────────────────────────────
// MAIN
// ─────────────────────────────────────────────
public static void main(String[] args) {
System.out.println("=== ExpSpringCacheRedis: Spring Cache + Redis simulado ===\n");
demoCacheable();
demoCacheEvict();
demoCachePut();
System.out.println("""
══ Resumen de anotaciones Spring Cache ══
@Cacheable → Consulta caché. Miss → ejecuta + guarda. Hit → devuelve sin ejecutar.
Ideal para: findById, getProducto, getUserProfile (operaciones de lectura).
@CacheEvict → Invalida una o varias keys. No ejecuta la query de caché.
Ideal para: delete, cuando los datos cambian desde fuera del servicio.
allEntries=true para limpiar todo el caché de ese nombre.
@CachePut → Siempre ejecuta el método Y actualiza Redis. Sin hit/miss check.
Ideal para: update (garantiza que Redis queda con el valor nuevo).
Sin @CachePut en el update, el caché quedaría obsoleto hasta que expire.
Key generation por defecto: cacheName + "::" + parámetros del método.
Custom key: @Cacheable(key = "#usuario.id + ':' + #tipo")
Condition: @Cacheable(condition = "#id.length() > 3") — solo cachea si se cumple
Unless: @Cacheable(unless = "#result == null") — no cachea resultados nulos""");
}
}