Backend Development 5 min read

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.

Java Architecture Diary
Java Architecture Diary
Java Architecture Diary
Master JetCache: Simplify Java Caching with Annotations and Two‑Level Support

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>&lt;dependency&gt;
    &lt;groupId&gt;com.alicp.jetcache&lt;/groupId&gt;
    &lt;artifactId&gt;jetcache-starter-redis-lettuce&lt;/artifactId&gt;
    &lt;version&gt;2.6.0&lt;/version&gt;
&lt;/dependency&gt;</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

@Cached

to add cache to a method,

@CacheUpdate

to update cache, and

@CacheInvalidate

to 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

findByUserName

method 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

statIntervalMinutes

parameter 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

JavaRediscachingAnnotationsSpringBootJetCache
Java Architecture Diary
Written by

Java Architecture Diary

Committed to sharing original, high‑quality technical articles; no fluff or promotional content.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.