Mastering JetCache: A SpringBoot Guide to Distributed Caching

This article introduces JetCache, an open‑source distributed cache built on Spring and Redis, explains its core features and typical use cases, details the main API and annotation support, and provides a step‑by‑step SpringBoot integration guide with full code examples.

Architect's Guide
Architect's Guide
Architect's Guide
Mastering JetCache: A SpringBoot Guide to Distributed Caching

1. JetCache Basic Concepts

JetCache is an open‑source distributed caching framework from Alibaba Group that works with Spring and Redis. Its key characteristics include annotation‑driven configuration, multi‑level caching (local memory, Redis, Tair, Memcached, etc.), support for multiple cache protocols, high performance under high concurrency, and a simple API that fits small to medium‑size applications.

Annotation‑based : Use annotations to configure and use caches, keeping code concise and maintainable.

Multi‑level cache : Combine local memory, Redis, Tair, Memcached, etc., to improve hit rates and query speed.

Multiple protocols : Compatible with Redis, Tair, Memcached, offering flexibility.

High performance : Optimized data structures and query algorithms for high‑concurrency environments.

Ease of use : Simple API, Spring injection, rich cache operations, suitable for many projects.

Typical scenarios include hotspot data caching, multi‑level cache fusion, high‑concurrency read/write, and fast query acceleration.

2. JetCache API and Usage

Cache management : Use CacheBuilder to create a cache and Cache objects to perform get, put, remove, etc. The @Cache annotation can set TTL and name when calling put.

Annotation support : Annotations such as @Cached, @CacheUpdate, @CacheInvalidate simplify cache operations, including pre‑warming, updating, and invalidation.

Cache types : Supports local memory, Redis, Tair, Memcached, allowing selection based on business needs.

Spring integration : Provides seamless Spring support; caches can be injected and configured via annotations.

3. SpringBoot Integration with JetCache

Step 1 – Add Maven dependency

<dependency>
    <groupId>com.alicp.jetcache</groupId>
    <artifactId>jetcache-starter-redis</artifactId>
    <version>3.0.1</version>
</dependency>

Step 2 – Configure application.yml

jetcache:
  remote:
    type: redis
    default:
      # Redis address
      servers: 127.0.0.1:6379
      # DB index, default 0
      database: 0
      # Password (optional)
      password:
      # Connection timeout (ms)
      connectTimeout: 5000
      # Operation timeout (ms)
      timeout: 5000
      # Max pool size
      maxTotal: 100
    cluster:
      servers: "127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381"
      password:
      maxTotal: 100
  local:
    default:
      limit: 10000
    myCache:
      limit: 100

Step 3 – Enable cache annotations @EnableMethodCache(basePackages = "") Step 4 – Example service code

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Cache(name = "userCache", key = "#userId", expire = 300)
    @Override
    public User getUserById(Long userId) {
        return userRepository.getUserById(userId);
    }

    @CacheUpdate(name = "userCache", key = "#user.id")
    @Override
    public void updateUser(User user) {
        userRepository.update(user);
    }

    @CacheInvalidate(name = "userCache", key = "#userId")
    @Override
    public void deleteUser(Long userId) {
        userRepository.deleteUser(userId);
    }
}

The @Cached annotation natively supports TTL, and the cacheType attribute can be set to LOCAL, REMOTE, or BOTH to choose between local memory, a remote cache server (e.g., Redis), or a two‑level cache, helping to reduce server load and improve response time.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaRedisJetCache
Architect's Guide
Written by

Architect's Guide

Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.

0 followers
Reader feedback

How this landed with the community

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.