I had been using Spring Cache to Cache interface data, mainly because Spring Cache was not very effective in setting the expiration time of specific key Cache, and I had to expand it by myself, so I found Ali’s JetCache by accident. Most of the requirements can be met, and there are some very practical functions, today to introduce you.

JetCache is a Java-based caching system package that provides a unified API and annotations to simplify the use of caches. JetCache provides more powerful annotations than SpringCache, with native support for TTL, two-level caching, distributed auto-refresh, and a Cache interface for manual caching. Currently there are four implementations, RedisCache, TairCache (not available on Github), CaffeineCache(in Memory) and a simple LinkedHashMapCache(in memory). It is also very easy to add new implementations.

GitHub:github.com/alibaba/jet…

All features:

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

Experience the

Add Maven configuration:

<dependency> <groupId>com.alicp.jetcache</groupId> <artifactId>jetcache-starter-redis</artifactId> The < version > 2.5.11 < / version > < / dependency >Copy the code

Configuration contents:

# Use Java serialization storage
jetcache.remote.default.valueDecoder = java
# Key converter
jetcache.remote.default.keyConvertor = fastjson
Whether to add the cache key prefix
jetcache.areaInCacheName = false
jetcache.remote.default.valueEncoder = java
# Cache type. Tair and Redis are currently supported remote caches. Linkedhashmap and Caffeine are currently supported local cache types
jetcache.local.default.type = linkedhashmap
Console output statistics, statistics interval, 0 indicates no statisticsjetcache.statIntervalMinutes = 15 jetcache.local.default.keyConvertor = fastjson jetcache.remote.default.uri = Redis: / / 192.168.0.210:6379 /Copy the code
  • Remote indicates remote cache
  • Local indicates the local cache

Start class to enable caching:

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

Define a simple entity class to act as a cache for the data, and you must implement the Serializable interface.

@Data
public class User implements Serializable {

	private Long id;
	
	private String name;

}
Copy the code

@ CreateCache use

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

User user = new User();
user.setId(1L);
user.setName("yinjihuan"); // Add cache usercache. put(1L, user); Usercache.remove (1L);Copy the code

It is as simple as using a local Map. There are many Settings in @createcache that we need to specify ourselves. If we do not specify them, use the default Settings.

@ Cached using

@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;
}
Copy the code
  • Name Cache name
  • Key The cache key is appended to name to form a unique cache key. SpEL is used to specify the key. If the key is not specified, it is automatically generated based on all parameters.
  • Expire Cache expiration time
  • CacheType cacheType, including cachetype. REMOTE, cachetype. LOCAL, and cachetype. BOTH. If BOTH is defined, LOCAL and REMOTE are combined to form a two-level cache

For more information about the configuration, see github.com/alibaba/jet…

Today’s introduction is here, it is still very convenient to use, about more functions we go to try it, such as cache timed refresh, cache hit ratio statistics, custom serialization and so on.

If your configuration is managed in Apollo, there will be a problem in the integration process. You need to add the upper and lower configuration to the project, and the rest can be put in Apollo.

spring.application.name=district-service
app.id=${spring.application.name}
apollo.bootstrap.enabled=true/ / this is what you want, specify the cache types, I side with lettuce jetcache. Remote). Default type = redis. LettuceCopy the code

@cached cannot specify the key attribute if it is specified on the interface. The framework automatically generates the key based on the parameter. If you have to specify the key using SPEL expressions, the target of the project compilation must be in 1.8 format. Also specify the javac -parameters parameter, otherwise use key=”args[0]” as the subscript access form. I recommend putting the @cached definition on the implementation class to make it easier to change.

I also recorded a set of video, interested in you can go to: cxytiandi.com/course/24