Multilevel Cache Spring Boot Starter

repository·master·Indexed 42 days ago

https://github.com/pig-mesh/multilevel-cache-spring-boot-starter

A Spring Boot starter providing a two-layer caching strategy combining Caffeine (L1 local memory) and Redis (L2 distributed memory). It reduces network I/O and improves performance in microservice architectures by using Redis Pub/Sub to maintain data consistency across instances. Supports Spring Boot 3.x (version 3.0.0) and Spring Boot 2.x (version 1.0.1) via standard Spring Cache annotations.

Tokens
690
Snippets
2
Records
3
Agent score
5%

What's inside Multilevel Cache Spring Boot Starter

  1. How multilevel cache works

    master

    The multilevel-cache-spring-boot-starter implements a two-layer caching strategy to optimize performance and reduce network I/O overhead:

    1. L1 Cache (Local): Uses Caffeine (JVM-level memory cache) for extremely high-speed access.
    2. L2 Cache (Distributed): Uses Redis to provide shared state across microservices.

    Data Flow

    • Read Operation: The system first checks the Caffeine L1 cache. If the key is missing (cache miss), it queries Redis (L2). If found in Redis, the value is automatically promoted to the Caffeine L1 cache for subsequent requests.
    • Write/Evict Operation: To maintain data consistency across multiple application instances, all write operations (put, evict, clear) use Redis Pub/Sub. When one instance modifies the cache, it publishes a CacheMessage via Redis. All other instances listening to the topic receive this message and invalidate the corresponding key in their local Caffeine L1 cache.
  2. Configure and use multilevel cache with Spring Cache annotations

    master

    To use the multilevel cache, follow these three steps:

    1. Enable Caching: Add the @EnableCaching annotation to your Spring Boot application configuration class.
    2. Declare Cacheable Annotations: Use standard Spring Cache annotations like @Cacheable on your service or controller methods. The starter will automatically use the RedisCaffeineCacheManager to handle the multi-layer logic.
    3. Define Keys: Use SpEL (Spring Expression Language) to define cache keys.
    // 1. Enable caching in your application
    @EnableCaching
    public class App {
        public static void main(String[] args) {
            SpringApplication.run(App.class, args);
        }
    }
    
    // 2. Use annotations on target methods
    @Cacheable(value = "get", key = "#key")
    @GetMapping("/get")
    public String get(String key) {
        return "success";
    }
  3. Install the multilevel-cache-spring-boot-starter

    master

    Add the following dependency to your Maven pom.xml. Ensure you replace ${lastVersion} with the appropriate version for your Spring Boot environment:

    • Use version 3.0.0 for SpringBoot 3.x.
    • Use version 1.0.1 for SpringBoot 2.x.
    <dependency>
        <groupId>com.pig4cloud.plugin</groupId>
        <artifactId>multilevel-cache-spring-boot-starter</artifactId>
        <version>${lastVersion}</version>
    </dependency>