maven

1
2
3
4
5
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.9.3</version>
</dependency>

config

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.stark37125.core.common.enums.CacheNameEnums;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

/**
* @author shuzhuo
*/
@Configuration
public class CaffeineCacheConfig {

@Bean
@Primary
public CacheManager caffeineCacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
List<org.springframework.cache.Cache> caches = new ArrayList<>();
Map<String, Cache<Object, Object>> map = getCacheType();
for (String name : map.keySet()) {
caches.add(new CaffeineCache(name, map.get(name)));
}
cacheManager.setCaches(caches);
return cacheManager;
}

/**
* 初始化自定义缓存策略
*
* @return
*/
private static Map<String, Cache<Object, Object>> getCacheType() {
Map<String, Cache<Object, Object>> map = new ConcurrentHashMap<>(16);
map.put(CacheNameEnums.SAVE_KEY.key(), Caffeine.newBuilder().recordStats()
// 设置最后一次写入或访问后经过固定时间过期
.expireAfterWrite(5, TimeUnit.SECONDS)
// 初始的缓存空间大小
.initialCapacity(100)
// 缓存的最大条数
.maximumSize(10000)
.build());
return map;
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.experimental.Accessors;

/**
* 缓存枚举
*
* @author shuzhuo
*/
@AllArgsConstructor
public enum CacheNameEnums {

/**
* 缓存key
*/
SAVE_KEY("cacheName1", "结果记录保存 key"),

;

@Getter
@Accessors(fluent = true)
private String key;

private String description;

}

util

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

import com.stark37125.core.common.enums.CacheNameEnums;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;

import java.util.Objects;

/**
* 缓存管理器
*
* @author shuzhuo
*/
@Slf4j
public class CacheManagerUtil {

private CacheManagerUtil() {
}

public static CacheManager cacheManager = SpringContextHolder.getBean(CacheManager.class);


/**
* set
*
* @param key 关键
* @param value 价值
* @param cacheNameEnums 缓存枚举
* @return boolean
*/
public static boolean set(CacheNameEnums cacheNameEnums, Object key, Object value) {
Cache cache = cacheManager.getCache(cacheNameEnums.key());
if (Objects.isNull(cache)) {
return false;
}
cache.put(key, value);
return true;
}

/**
* get
*
* @param key 关键
* @param cacheNameEnums 缓存枚举
* @return {@link Object}
*/
public static Object get(CacheNameEnums cacheNameEnums, Object key) {
Cache cache = cacheManager.getCache(cacheNameEnums.key());
if (Objects.isNull(cache)) {
return null;
}
Cache.ValueWrapper valueWrapper = cache.get(key);
if (Objects.isNull(valueWrapper)) {
return null;
}
return valueWrapper.get();
}

/**
* get 字符串
*
* @param cacheNameEnums 缓存名称枚举
* @param key 关键
* @return {@link Object}
*/
public static Object getString(CacheNameEnums cacheNameEnums, Object key) {
return get(cacheNameEnums, key, String.class);
}

/**
* get
*
* @param key 关键
* @param type 类型
* @param cacheNameEnums 缓存枚举
* @return {@link T}
*/
public static <T> T get(CacheNameEnums cacheNameEnums, Object key, Class<T> type) {
Cache cache = cacheManager.getCache(cacheNameEnums.key());
if (Objects.isNull(cache)) {
return null;
}
return cache.get(key, type);
}

/**
* 删除
*
* @param cacheNameEnums 缓存名称枚举
* @param key 关键
* @return boolean
*/
public static boolean remove(CacheNameEnums cacheNameEnums, Object key) {
Cache cache = cacheManager.getCache(cacheNameEnums.key());
if (Objects.isNull(cache)) {
return false;
}
cache.evict(key);
return true;
}
}

单元测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

import com.stark37125.core.common.enums.CacheNameEnums;
import com.stark37125.core.util.CacheManagerUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SpringbootTest001 {


@Test
public void test001() throws Exception {

String key="111";
boolean set = CacheManagerUtil.set(CacheNameEnums.SAVE_KEY, key, "2");
System.out.println(set);
for (int i = 0; i < 15; i++) {
Thread.sleep(1000);
System.out.println("当前cache");
String s = CacheManagerUtil.get(CacheNameEnums.SAVE_KEY, key, String.class);
System.out.println(s);
}


}
}