Using JetCache for Multi‑Level Caching in Spring Boot Applications
This article introduces Alibaba's JetCache Java caching framework, explains how to combine local and Redis remote caches for multi‑level caching, provides Maven dependencies, configuration examples, three usage patterns (AOP, API, advanced API), testing steps, and common troubleshooting tips for Spring Boot projects.
In real‑world scenarios, developers often combine local caches with Redis remote caches to meet different business requirements; JetCache, an open‑source Java caching framework from Alibaba, offers a seamless way to implement such multi‑level caching.
1. JetCache Overview
JetCache supports multiple cache types—local, distributed, and multi‑level—providing high performance, easy integration with Spring Cache, and features such as cache warming and key prefixes. Official repository: https://github.com/alibaba/jetcache and documentation: https://github.com/alibaba/jetcache/tree/master/docs/CN .
2. Using JetCache
2.1 Add Maven dependencies
<dependency>
<groupId>com.alicp.jetcache</groupId>
<artifactId>jetcache-starter-redis</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.3.1</version>
</dependency>Configure application.yml (or application.properties ) to set Redis address, thread pool, and cache types:
jetcache:
statIntervalMinutes: 15
areaInCacheName: false
local:
default:
type: linkedhashmap
keyConvertor: fastjson
remote:
default:
type: redis
keyConvertor: fastjson
broadcastChannel: projectA
valueEncoder: java
valueDecoder: java
poolConfig:
minIdle: 5
maxIdle: 20
maxTotal: 50
host: 127.0.0.1
port: 63792.2 Enable caching annotations
@EnableCreateCacheAnnotation
@EnableMethodCache(basePackages = "com.example.jetcachedemo")2.3 Define cacheable methods (AOP mode)
@RestController
@RequestMapping("user")
public class UserController {
@GetMapping("getRemote")
@Cached(name="userCache:", key="#id", expire=3600, timeUnit=TimeUnit.SECONDS, cacheType=CacheType.REMOTE)
public User getRemote(Long id) { /* ... */ }
@GetMapping("getLocal")
@Cached(name="userCache:", key="#id", expire=3600, timeUnit=TimeUnit.SECONDS, cacheType=CacheType.LOCAL)
public User getLocal(Long id) { /* ... */ }
@GetMapping("getBoth")
@Cached(name="userCache:", key="#id", expire=3600, timeUnit=TimeUnit.SECONDS, cacheType=CacheType.BOTH)
public User getBoth(Long id) { /* ... */ }
@PostMapping("updateUser")
@CacheUpdate(name="userCache:", key="#user.id", value="#user")
public Boolean updateUser(@RequestBody User user) { return true; }
@PostMapping("deleteUser")
@CacheInvalidate(name="userCache:", key="#id")
public Boolean deleteUser(Long id) { return true; }
}Entity User must implement Serializable :
@Data
public class User implements Serializable {
private Long id;
private String name;
private Integer age;
private Integer sex;
}2.4 Alternative API mode (using @CreateCache ) and 2.5 Advanced API mode (using CacheManager ) are also demonstrated with corresponding code snippets.
3. Testing
Three testing methods are shown: accessing /user/getRemote?id=1 , /user/getLocal?id=1 , and similar endpoints for the API and advanced API modes, verifying that data is stored in Redis, local cache, or both as configured.
4. Common Errors
Typical class‑not‑found problems such as ClassNotFoundException: com.alibaba.fastjson.JSON and NoClassDefFoundError: redis/clients/jedis/UnifiedJedis are resolved by adding the appropriate fastjson and jedis dependencies or downgrading JetCache version.
Source: Juejin article
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.