# JetCache **Repository Path**: jempson/JetCache ## Basic Information - **Project Name**: JetCache - **Description**: JetCache 是一个基于 Java 的缓存系统封装,提供统一的 API 和注解来简化缓存的使用 - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 50 - **Created**: 2018-01-23 - **Last Updated**: 2021-11-03 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README [![Build Status](https://travis-ci.org/alibaba/jetcache.svg?branch=master)](https://travis-ci.org/alibaba/jetcache) [![Coverage Status](https://coveralls.io/repos/alibaba/jetcache/badge.png?branch=master)](https://coveralls.io/r/alibaba/jetcache?branch=master) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.alicp.jetcache/jetcache-parent/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.alicp.jetcache/jetcache-parent/) [![License](https://img.shields.io/badge/license-Apache%202-4EB1BA.svg)](https://www.apache.org/licenses/LICENSE-2.0.html) # introduction JetCache is a Java cache abstraction which provides consistent use for various caching solutions. Presently There are four implements: ```RedisCache```, ```TairCache```(not open source on github), ```CaffeineCache``` (in memory), a simple ```LinkedHashMapCache``` (in memory). Features of JetCache: * Operate cache through consistent Cache API. * Declarative method caching using annotation with TTL(Time To Live) and two level caching support * Create & configure ```Cache``` instance using annotation * Auto collect access statistics for Cache instance and method cache * The policy of key generation and value serialization can be customized * Auto Refresh (2.2+) * Asynchronous access using Cache API (2.2+, with redis lettuce client) * Spring Boot support requirements: * JDK1.8 * Spring Framework4.0.8+ (optional, with annotation support) * Spring Boot1.1.9+ (optional) Visit [wiki](https://github.com/alibaba/jetcache/wiki) for more documents. # getting started ## method cache Declare method cache using ```@Cached``` annotation. ```expire = 3600``` indicates that the elements will expires in 3600 seconds after put. ```java public interface UserService { @Cached(expire = 3600, cacheType = CacheType.REMOTE) User getUserById(long userId); } ``` ## cache API Create a ```Cache``` instance using ```@CreateCache``` annotation: ```java @CreateCache(expire = 100, cacheType = CacheType.BOTH, localLimit = 50) private Cache userCache; ``` The code above create a ```Cache``` instance. ```cacheType = CacheType.BOTH``` define a two level cache (a local in-memory-cache and a remote cache system) with local elements limited upper to 50(LRU based evict). You can use it like a map: ```java UserDO user = userCache.get(12345L); userCache.put(12345L, loadUserFromDataBase(12345L)); userCache.remove(12345L); userCache.computeIfAbsent(1234567L, (key) -> loadUserFromDataBase(1234567L)); ``` ## configuration with Spring Boot pom: ```xml com.alicp.jetcache jetcache-starter-redis ${jetcache.latest.version} ``` App class: ```java @SpringBootApplication @EnableMethodCache(basePackages = "com.company.mypackage") @EnableCreateCacheAnnotation public class MySpringBootApp { public static void main(String[] args) { SpringApplication.run(MySpringBootApp.class); } } ``` spring boot application.yml config: ```yaml jetcache: statIntervalMinutes: 15 local: default: type: linkedhashmap keyConvertor: fastjson limit: 100 remote: default: type: redis keyConvertor: fastjson valueEncoder: java valueDecoder: java poolConfig: minIdle: 5 maxIdle: 20 maxTotal: 50 host: ${redis.host} port: ${redis.port} ``` ## configuration without Spring Boot pom: ```xml com.alicp.jetcache jetcache-anno ${jetcache.latest.version} ``` config ```java @Configuration @EnableMethodCache(basePackages = "com.company.mypackage") @EnableCreateCacheAnnotation public class JetCacheConfig { @Bean public Pool pool(){ GenericObjectPoolConfig pc = new GenericObjectPoolConfig(); pc.setMinIdle(2); pc.setMaxIdle(10); pc.setMaxTotal(10); return new JedisPool(pc, "localhost", 6379); } @Bean public SpringConfigProvider springConfigProvider() { return new SpringConfigProvider(); } @Bean public GlobalCacheConfig config(SpringConfigProvider configProvider, Pool pool){ GlobalCacheConfig pc = new GlobalCacheConfig(); Map localBuilders = new HashMap(); EmbeddedCacheBuilder localBuilder = LinkedHashMapCacheBuilder .createLinkedHashMapCacheBuilder() .keyConvertor(FastjsonKeyConvertor.INSTANCE); localBuilders.put(CacheConsts.DEFAULT_AREA, localBuilder); Map remoteBuilders = new HashMap(); RedisCacheBuilder remoteCacheBuilder = RedisCacheBuilder.createRedisCacheBuilder() .keyConvertor(FastjsonKeyConvertor.INSTANCE) .valueEncoder(JavaValueEncoder.INSTANCE) .valueDecoder(JavaValueDecoder.INSTANCE) .jedisPool(pool); remoteBuilders.put(CacheConsts.DEFAULT_AREA, remoteCacheBuilder); GlobalCacheConfig globalCacheConfig = new GlobalCacheConfig(); globalCacheConfig.setConfigProvider(configProvider); globalCacheConfig.setLocalCacheBuilders(localBuilders); globalCacheConfig.setRemoteCacheBuilders(remoteBuilders); globalCacheConfig.setStatIntervalMinutes(15); return globalCacheConfig; } } ``` ## more docs Visit [wiki](https://github.com/alibaba/jetcache/wiki) for more documents.