JetCache Overview: Core Concepts, API Usage, and SpringBoot Integration
This article introduces JetCache, an open‑source distributed caching framework built on Spring and Redis, outlines its key features and typical scenarios, and provides step‑by‑step guidance for integrating JetCache into a SpringBoot application with configuration examples and sample code.
JetCache is an open‑source distributed cache framework from Alibaba Group, built on Spring and Redis, offering annotation‑based configuration, multi‑level caching, support for various cache protocols (Redis, Tair, Memcached), high performance, and easy‑to‑use APIs suitable for medium‑sized applications.
Annotation‑based: Simplifies cache configuration and usage through annotations.
Multi‑level cache: Supports local memory, Redis, Tair, Memcached, improving hit rates and query efficiency.
Multiple cache protocols: Flexible switching among Redis, Tair, Memcached.
High performance: Optimized data structures and algorithms for high concurrency.
Ease of use: Simple API, Spring injection, rich cache operations.
Typical application scenarios include hotspot data caching, multi‑level cache fusion, high‑concurrency read/write, and fast access queries.
JetCache API and Usage
Cache management: Use CacheBuilder to create caches and Cache objects for operations such as get , put , remove . The @Cache annotation configures TTL and cache name.
Annotation support: Annotations like @Cached , @CacheUpdate , @CacheInvalidate enable convenient cache operations, pre‑warming, updating, and invalidation.
Cache types: Supports local memory, Redis, Tair, Memcached, selectable per business need.
Spring integration: JetCache integrates with Spring, allowing annotation‑driven injection and configuration.
SpringBoot Integration Steps
1. Add Maven dependency:
<dependency>
<groupId>com.alicp.jetcache</groupId>
<artifactId>jetcache-starter-redis</artifactId>
<version>3.0.1</version>
</dependency>2. Configure application.yml :
jetcache:
remote:
type: redis
default:
servers: 127.0.0.1:6379
database: 0
connectTimeout: 5000
timeout: 5000
maxTotal: 100
cluster:
servers: "127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381"
maxTotal: 100
local:
default:
limit: 10000
myCache:
limit: 1003. Enable method caching in the main class:
@EnableMethodCache(basePackages = "")4. Example service implementation:
@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 offers cache types LOCAL, REMOTE, and BOTH, allowing developers to balance local memory usage and remote cache load for optimal performance.
Source: juejin.cn/post/7244734132323221563
Top Architecture Tech Stack
Sharing Java and Python tech insights, with occasional practical development tool tips.
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.