<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.9.0</version>
</dependency>
顺便写了个工具类配合SpringBoot使用:
package com.ciih.refineinner.cache;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
/**
* 缓存之王
*
* @author Lenovo
*/
@Component
public class CaffeineService {
private Cache<String, String> cache;
public CaffeineService() {
this.cache = Caffeine.newBuilder()
.expireAfterWrite(15, TimeUnit.MINUTES)
.maximumSize(100)
.build();
}
/**
* 存储K-V
*
* @param key
* @param value
* @return
*/
public String put(String key, String value) {
cache.put(key, value);
return key;
}
/**
* 获取K-V
*
* @param key
* @return
*/
public String getIfPresent(String key) {
return cache.getIfPresent(key);
}
public String get(String key, Function<String, String> function) {
return cache.get(key, function);
}
public void remove(String key) {
cache.invalidate(key);
}
}
标签:缓存,String,cache,caffeine,SpringBoot2,key,import,public From: https://blog.51cto.com/u_14121041/6415448