Alibaba's open source caching framework JetCache

Alibaba's open source caching framework JetCache

I have been using Spring Cache to cache interface data before. The main reason is that Spring Cache is not very good in setting the specific key cache invalidation time. I have to expand it myself, and I accidentally discovered Ali's JetCache. Most of the needs can be met, and there are some very practical functions, I will introduce you to them today.

JetCache is a Java-based cache system package that provides unified APIs and annotations to simplify the use of cache. JetCache provides more powerful annotations than SpringCache. It can natively support TTL, two-level caching, and distributed automatic refresh. It also provides a Cache interface for manual caching operations. There are currently four implementations, RedisCache, TairCache (this part is not open source on github), CaffeineCache (in memory) and a simple LinkedHashMapCache (in memory). It is also very simple to add a new implementation.

GitHub: github.com/alibaba/jet...

All features:

  • Access the Cache system through a unified API
  • Implement declarative method caching through annotations, support TTL and two-level caching
  • Create and configure Cache instance through annotations
  • Automatic statistics for all Cache instances and methods cache
  • Key generation strategy and Value serialization strategy are configurable
  • Distributed cache automatic refresh, distributed lock (2.2+)
  • Asynchronous Cache API (2.2+, when using Redis' lettuce client)
  • Spring Boot support

Experience it

Add Maven configuration:

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

Configuration content:

#  Java 
jetcache.remote.default.valueDecoder = java
# Key 
jetcache.remote.default.keyConvertor = fastjson
#  key 
jetcache.areaInCacheName = false
jetcache.remote.default.valueEncoder = java
#  tair redis linkedhashmap caffeine 
jetcache.local.default.type = linkedhashmap
#  0 
jetcache.statIntervalMinutes = 15
jetcache.local.default.keyConvertor = fastjson
jetcache.remote.default.uri = redis://192.168.0.210:6379/
 
  • remote means remote cache
  • local means local cache

Start the class to open the cache:

@SpringBootApplication
@EnableMethodCache(basePackages = "com.cxytiandi.jetcache")
@EnableCreateCacheAnnotation
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class);
    }
}
 
  • @EnableMethodCache is used to activate the use of @Cached annotations
  • @EnableCreateCacheAnnotation is used to activate the use of @CreateCache annotation

To define a simple entity class as a data cache, you must implement the Serializable interface.

@Data
public class User implements Serializable {

	private Long id;
	
	private String name;

}
 

@CreateCache use

@CreateCache(expire = 100)
private Cache<Long, User> userCache;

User user = new User();
user.setId(1L);
user.setName("yinjihuan");
// 
userCache.put(1L, user);

// 
userCache.remove(1L);
 

It's very simple to use, just like operating a local Map. There are many configurations in @CreateCache that we need to specify by ourselves. If we don't specify them, we will use the default ones. For the configuration, please check the document: github.com/alibaba/jet...

@Cached use

@Cached(name="getUser.", key="#id", expire = 8, cacheType=CacheType.BOTH)
@Override
public User getUser(Long id) {
	User user = new User();
	user.setId(1L);
	user.setName("yinjihuan");
	return user;
}
 
  • name Cache name
  • key Cache key, appended to name to form a unique cache key, using SpEL specify the key. If not specified, it will be automatically generated based on all parameters.
  • expire cache expiration time
  • cacheType The type of cache, including CacheType.REMOTE, CacheType.LOCAL, CacheType.BOTH. If defined as BOTH, LOCAL and REMOTE will be used to combine into a two-level cache

For more configuration introduction, please check the document: github.com/alibaba/jet...

That's it for today's introduction. It is still very convenient to use. Let's try more functions, such as cache timing refresh, cache hit rate statistics, custom serialization methods, and so on.

The pit of integrating Apollo Here is a point that is easy to be pitted. If your configuration is managed in Apollo, then there will be a problem during integration. You need to add the following configuration to the project. , The rest of the configuration can be put in Apollo.

spring.application.name=district-service
app.id=${spring.application.name}
apollo.bootstrap.enabled=true
// lettuce
jetcache.remote.default.type=redis.lettuce
 

@Cached is defined on the interface. Another pit is that if @Cached is defined on the interface, you cannot specify the key attribute. The framework will automatically generate the key according to the parameter. If you have to specify the key with a SPEL expression, the project compiles and sets the target It must be in the 1.8 format, and specify the -parameters parameter of javac, otherwise it is necessary to use key="args[0]" to access the form by indexing. I suggest to put the definition of @Cached on the implementation class, which is also convenient for modification.

I have also recorded a set of videos here. If you are interested, you can check it out: cxytiandi.com/course/24