Make writing a habit together! This is the fourth day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.

What is JetCache?

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.

All features:

  • Access the Cache system through a unified API
  • Declarative method caching with annotations, support for TTL and two-level caching
  • Created and configured by annotationsCacheThe instance
  • For allCacheAutomatic statistics for 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

How to use

  1. Add the dependent
// redis implementation 'org.springframework.boot:spring-boot-starter-data-redis' implementation 'org.apache.com mons: Commons - pool2 / / jetcache implementation' com. Alicp. Jetcache: jetcache - starter - redis: 2.6.2 'Copy the code
  1. Configuration yml
jetcache:
  statIntervalMinutes: 15
  areaInCacheName: false
  local:
    default:
      type: linkedhashmap
      keyConvertor: fastjson
  remote:
    default:
      type: redis
      keyConvertor: fastjson
      valueEncoder: java
      valueDecoder: java
      poolConfig:
        minIdle: 5
        maxIdle: 20
        maxTotal: 50
      host: 127.0. 01.
      port: 6379
Copy the code
  1. Then create an App class under the root of the business package, EnableMethodCache, EnableCreateCacheAnnotation two annotations to activate this Cached and CreateCache annotations, other Spring Boot procedures and standards are the same. This class can be run directly from the main method.
package io.zhengsh.order;
​
import com.alicp.jetcache.anno.config.EnableCreateCacheAnnotation;
import com.alicp.jetcache.anno.config.EnableMethodCache;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
​
@MapperScan("io.zhengsh.order.mapper")
@SpringBootApplication
@EnableMethodCache(basePackages = "io.zhengsh.order.service")
@EnableCreateCacheAnnotation
public class OrderApplication {
​
    public static void main(String[] args) { SpringApplication.run(OrderApplication.class); }}Copy the code
  1. Add annotations to the business class. The business class code is as follows:
public interface OrderService { OrderVo get(Long id); OrderVo createOrder(OrderDto orderDto); } @service public class OrderServiceImpl implements OrderService {private Logger Logger = LoggerFactory.getLogger(OrderServiceImpl.class); @Override public OrderVo get(Long id) { logger.info("OrderService#get invoke!" ); Order orderVo = new OrderVo(); orderVo.setCode("100"); return orderVo; } @Override public OrderVo createOrder(OrderDto orderDto) { logger.info("OrderService#createOrder invoke!" ); return null; }}Copy the code
  1. The test code
@SpringBootTest
class OrderServiceTest {
​
    private Logger logger = LoggerFactory.getLogger(OrderServiceTest.class);
​
    @Autowired
    private OrderService orderService;
​
    @Test
    void get(a) {
        OrderVo orderVo = orderService.get(100L);
        logger.info("orderVo#code : {} ", orderVo.getCode());
​
        OrderVo orderVo1 = orderService.get(100L);
        logger.info("orderVo#code : {} ", orderVo1.getCode());
    }
​
    @Test
    void createOrder(a) {}}Copy the code

The output is as follows:

// OrderService#get invoke! // orderVo#code : 100 // orderVo#code : 100Copy the code

Q&A

We can refer to the official Issues:

  • Github.com/alibaba/jet…