Caffeine Cache 简单示例 如下
POM依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>caffeine-cache</artifactId>
<groupId>com.cache</groupId>
<version>1.8-SNAPSHOT</version>
<packaging>jar</packaging>
<name>caffeine-cache</name>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-boot.version>2.0.4.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.8.6</version>
</dependency>
</dependencies>
</project>
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = {"com.caffeine.cache"})
public class MainApp {
public static void main(String[] args) {
SpringApplication.run(MainApp.class, args);
}
}
基础服务
public interface IBaseService {
String queryService(Integer id);
}
import org.springframework.stereotype.Service;
import java.util.Random;
@Service
public class BaseServiceImpl implements IBaseService {
private Random random = new Random();
@Override
public String queryService(Integer id) {
//这里可以先取redis缓存再查数据库
return String.format("%s->%s",id,(random.nextInt(100) + 1));
}
}
缓存服务
import com.github.benmanes.caffeine.cache.CacheLoader;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.github.benmanes.caffeine.cache.RemovalCause;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.TimeUnit;
@Configuration
public class CacheConfig {
@Autowired(required = false)
private IBaseService baseService;
/**
* Cache.stats() 方法返回提供统计信息的CacheStats,如:
* hitRate():返回命中与请求的比率
* hitCount(): 返回命中缓存的总数
* evictionCount():缓存逐出的数量
* averageLoadPenalty():加载新值所花费的平均时间
*/
@Bean("mycache1")
public LoadingCache<Integer, String> studentCache() {
return Caffeine.newBuilder()
.initialCapacity(1)//初始缓存长度为1
.maximumSize(10) //最大长度为10
.recordStats() //可以打开统计信息收集
.expireAfterWrite(1, TimeUnit.MINUTES)//设置缓存策略在1小时未写入过期缓存
.removalListener((Integer key, String value, RemovalCause cause) ->{
System.out.printf("移除缓存Key:%s Value:%s 移除类型:%s %n", key, value ,cause);
})
.build(new CacheLoader<Integer, String>() {
@Override
public String load(Integer key) throws Exception {
String value = baseService.queryService(key);
return value;
}
});
}
}
public interface ICacheService {
String queryById(Integer id);
void putCache(Integer key,String value);
void removeCache(Integer key);
void removeAllCache();
}
Controller测试调用
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Random;
@Controller
@ResponseBody
public class TestController {
@Autowired
private ICacheService myService1;
private Random random = new Random();
@RequestMapping(value ="/hello", method = RequestMethod.GET)
@ResponseBody
public String hello1() throws InterruptedException {
int id = random.nextInt(30) + 1;
String value = myService1.queryById(id);
return "Hello World!"+" : ["+id+" - "+value+"]";
}
}
示例较为简单,仅供参考;