### Declare Cacheable Interface Method Source: https://github.com/pig-mesh/multilevel-cache-spring-boot-starter/blob/master/README.md This Java code snippet illustrates how to declare a method that should be cached using Spring's @Cacheable annotation. It specifies the cache name ('get') and a cache key based on the method's parameter. ```java @Cacheable(value = "get",key = "#key") @GetMapping("/get") public String get(String key){ return "success"; } ``` -------------------------------- ### Add Multilevel Cache Dependency (Maven) Source: https://github.com/pig-mesh/multilevel-cache-spring-boot-starter/blob/master/README.md This snippet shows how to include the multilevel cache starter dependency in your Maven project's pom.xml file. It specifies the group ID, artifact ID, and version for the starter. ```xml com.pig4cloud.plugin multilevel-cache-spring-boot-starter ${lastVersion} ``` -------------------------------- ### Cache Eviction and Update via Redis Pub/Sub Source: https://github.com/pig-mesh/multilevel-cache-spring-boot-starter/blob/master/README.md This Java code demonstrates how cache eviction and update operations are handled using Redis Pub/Sub. When a cache is updated or evicted, a message is published to a Redis topic, which is then used to invalidate the corresponding entry in the local Caffeine cache. ```java @Override public void put(Object key, Object value) { push(new CacheMessage(this.name, key)); } @Override public ValueWrapper putIfAbsent(Object key, Object value) { push(new CacheMessage(this.name, key)); } @Override public void evict(Object key) { push(new CacheMessage(this.name, key)); } @Override public void clear() { push(new CacheMessage(this.name, null)); } private void push(CacheMessage message) { stringKeyRedisTemplate.convertAndSend(topic, message); } ``` -------------------------------- ### Multilevel Cache Lookup Logic Source: https://github.com/pig-mesh/multilevel-cache-spring-boot-starter/blob/master/README.md This Java code outlines the lookup logic for the RedisCaffeineCache. It first attempts to retrieve the value from Caffeine (L1 cache) and if not found, it retrieves it from Redis (L2 cache) and then populates the Caffeine cache. ```java protected Object lookup(Object key) { Object cacheKey = getKey(key); // 1. 先调用 caffeine 查询是否存在指定的值 Object value = caffeineCache.getIfPresent(key); if (value != null) { log.debug("get cache from caffeine, the key is : {}", cacheKey); return value; } // 2. 调用 redis 查询在指定的值 value = stringKeyRedisTemplate.opsForValue().get(cacheKey); if (value != null) { log.debug("get cache from redis and put in caffeine, the key is : {}", cacheKey); caffeineCache.put(key, value); } return value; } ``` -------------------------------- ### Custom CacheManager Implementation Source: https://github.com/pig-mesh/multilevel-cache-spring-boot-starter/blob/master/README.md This Java code shows a custom CacheManager implementation, RedisCaffeineCacheManager, which is responsible for creating and managing RedisCaffeineCache instances. It defines how new caches are created and retrieved. ```java public class RedisCaffeineCacheManager implements CacheManager { @Override public Cache getCache(String name) { Cache cache = cacheMap.get(name); if (cache != null) { return cache; } cache = new RedisCaffeineCache(name, stringKeyRedisTemplate, caffeineCache(), cacheConfigProperties); Cache oldCache = cacheMap.putIfAbsent(name, cache); log.debug("create cache instance, the cache name is : {}", name); return oldCache == null ? cache : oldCache; } } ``` -------------------------------- ### Enable Caching in Spring Boot Application Source: https://github.com/pig-mesh/multilevel-cache-spring-boot-starter/blob/master/README.md This Java code demonstrates how to enable Spring's caching annotations in your Spring Boot application by adding the @EnableCaching annotation to your main application class. ```java @EnableCaching public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } } ``` -------------------------------- ### Cache Message Listener for Local Cache Invalidation Source: https://github.com/pig-mesh/multilevel-cache-spring-boot-starter/blob/master/README.md This Java code defines a MessageListener that listens to Redis pub/sub messages. Upon receiving a message, it deserializes the CacheMessage and calls the clearLocal method on the RedisCaffeineCacheManager to invalidate the corresponding entry in the local Caffeine cache. ```java public class CacheMessageListener implements MessageListener { private final RedisTemplate redisTemplate; private final RedisCaffeineCacheManager redisCaffeineCacheManager; @Override public void onMessage(Message message, byte[] pattern) { CacheMessage cacheMessage = (CacheMessage) redisTemplate.getValueSerializer().deserialize(message.getBody()); cacheMessage.getCacheName(), cacheMessage.getKey()); redisCaffeineCacheManager.clearLocal(cacheMessage.getCacheName(), cacheMessage.getKey()); } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.