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
<code><dependency>
<groupId>com.alicp.jetcache</groupId>
<artifactId>jetcache-starter-redis-lettuce</artifactId>
<version>2.6.0</version>
</dependency></code>Cache Configuration
<code>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 address
</code>Enable in Main Class
<code>@EnableMethodCache(basePackages = "com.pig4cloud.jetcache")
@EnableCreateCacheAnnotation
@SpringBootApplication
public class JetcacheDemoApplication {
public static void main(String[] args) {
SpringApplication.run(JetcacheDemoApplication.class, args);
}
}
</code>Business Usage
Use
@Cachedto add cache to a method,
@CacheUpdateto update cache, and
@CacheInvalidateto remove cache entries.
<code>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) {}
}
</code>Unit Test
<code>@SpringBootTest
class JetcacheDemoApplicationTests {
@Autowired
private DemoService demoService;
@Test
void findByUserNameTest() {
demoService.findByUserName("a");
demoService.findByUserName("a");
demoService.findByUserName("a");
}
}
</code>Because the
findByUserNamemethod is annotated for caching, the underlying DB query runs only once, as shown by a single warning log.
<code>2021-03-07 22:09:11.828 WARN 53501 --- [main] c.p.jetcache.service.DemoService : Support DB query logic a
</code>With a two‑level (local & remote) cache, Redis is accessed only once.
Cache Statistics
The
statIntervalMinutesparameter defines the interval for cache statistics; setting it to 0 disables statistics.
<code>jetcache:
statIntervalMinutes: 1
</code>Statistics are automatically logged, e.g.:
<code>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
-----+----------+-------+--------------+--------------+--------------+--------------+-----------+-----------
</code>Summary
Source code: https://github.com/lltx/jetcache-demo
Reference
[1] https://github.com/lltx/jetcache-demo
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.