Master JetCache: Simplify Java Caching with Annotations and Two‑Level Support
This article introduces JetCache, a Java caching library that offers powerful annotations, two‑level (local and remote) caching, TTL support, and easy configuration, and demonstrates its quick start, usage in Spring Boot, unit testing, and cache statistics.
What is JetCache
JetCache is a Java‑based cache system wrapper that provides a unified API and annotations to simplify cache usage. It offers stronger annotations than SpringCache, natively supporting TTL, two‑level cache, distributed auto‑refresh, and a Cache interface for manual operations. Currently it includes RedisCache, TairCache (not open‑source), CaffeineCache (in‑memory) and a simple LinkedHashMapCache (in‑memory), and adding new implementations is straightforward.
Quick Start
Maven Dependency
<dependency>
<groupId>com.alicp.jetcache</groupId>
<artifactId>jetcache-starter-redis-lettuce</artifactId>
<version>2.6.0</version>
</dependency>Cache Configuration
jetcache:
statIntervalMinutes: 1
areaInCacheName: false
local:
default:
type: linkedhashmap
keyConvertor: fastjson
remote:
default:
type: redis.lettuce
keyConvertor: fastjson
uri: redis://127.0.0.1:6379/ # specify redis addressEnable in Main Class
@EnableMethodCache(basePackages = "com.pig4cloud.jetcache")
@EnableCreateCacheAnnotation
@SpringBootApplication
public class JetcacheDemoApplication {
public static void main(String[] args) {
SpringApplication.run(JetcacheDemoApplication.class, args);
}
}Business Usage
Use @Cached to add cache to a method, @CacheUpdate to update cache, and @CacheInvalidate to remove cache entries.
public class DemoService {
@Cached(name = "DemoCache", key = "#username", expire = 3600, cacheType = CacheType.BOTH)
public Demo findByUserName(String username) {
log.warn("Support DB query logic {}", username);
return new Demo(username, "123456");
}
@CacheInvalidate(name = "DemoCache", key = "#username")
public void deleteByUserName(String username) {}
}Unit Test
@SpringBootTest
class JetcacheDemoApplicationTests {
@Autowired
private DemoService demoService;
@Test
void findByUserNameTest() {
demoService.findByUserName("a");
demoService.findByUserName("a");
demoService.findByUserName("a");
}
}Because the findByUserName method is annotated for caching, the underlying DB query runs only once, as shown by a single warning log.
2021-03-07 22:09:11.828 WARN 53501 --- [main] c.p.jetcache.service.DemoService : Support DB query logic aWith a two‑level (local & remote) cache, Redis is accessed only once.
Cache Statistics
The statIntervalMinutes parameter defines the interval for cache statistics; setting it to 0 disables statistics.
jetcache:
statIntervalMinutes: 1Statistics are automatically logged, e.g.:
2021-03-07 22:13:00.032 INFO 53564 --- [DefaultExecutor] c.alicp.jetcache.support.StatInfoLogger : jetcache stat from 2021-03-07 22:12:13,773 to 2021-03-07 22:13:00,024
cache| qps| rate| get| hit| fail| expire|avgLoadTime|maxLoadTime
-----+----------+-------+--------------+--------------+--------------+--------------+-----------+-----------Summary
Source code: https://github.com/lltx/jetcache-demo
Reference
[1] https://github.com/lltx/jetcache-demo
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Architecture Diary
Committed to sharing original, high‑quality technical articles; no fluff or promotional content.
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.
