2
2
3
3
import cn .hutool .core .collection .CollUtil ;
4
4
import com .google .common .cache .CacheBuilder ;
5
- import org .code4everything .boot .cache .guava .GuavaCacheCreator ;
5
+ import org .code4everything .boot .cache .guava .GuavaCache ;
6
6
import org .code4everything .boot .cache .guava .GuavaCacheManager ;
7
7
import org .springframework .cache .Cache ;
8
8
9
9
import java .util .ArrayList ;
10
10
import java .util .Arrays ;
11
11
import java .util .Collection ;
12
+ import java .util .Map ;
12
13
13
14
/**
14
15
* Spring Cache工具类
@@ -20,21 +21,47 @@ public class CacheUtils {
20
21
21
22
private CacheUtils () {}
22
23
24
+ // ---------------------------------------Guava---------------------------------------------------------------------
25
+
26
+ public static GuavaCacheManager newGuavaCacheManager (Map <String , CacheBuilder <Object , Object >> cacheBuilderMap ) {
27
+ return newGuavaCacheManager (cacheBuilderMap , null );
28
+ }
29
+
30
+
31
+ public static GuavaCacheManager newGuavaCacheManager (Map <String , CacheBuilder <Object , Object >> cacheBuilderMap ,
32
+ CacheBuilder <Object , Object > defaultCacheBuilder ) {
33
+ Collection <Cache > caches = new ArrayList <>();
34
+ cacheBuilderMap .forEach ((k , v ) -> caches .add (new GuavaCache (k , v .build ())));
35
+ return new GuavaCacheManager (caches , defaultCacheBuilder );
36
+ }
37
+
23
38
public static GuavaCacheManager newGuavaCacheManager (CacheBuilder <Object , Object > cacheBuilder , String ... names ) {
24
39
return newGuavaCacheManager (cacheBuilder , Arrays .asList (names ));
25
40
}
26
41
27
42
public static GuavaCacheManager newGuavaCacheManager (CacheBuilder <Object , Object > cacheBuilder ,
28
43
Collection <String > names ) {
29
- CacheCreator cacheCreator = new GuavaCacheCreator (cacheBuilder );
30
44
if (CollUtil .isEmpty (names )) {
31
- return new GuavaCacheManager (cacheCreator );
45
+ return new GuavaCacheManager (cacheBuilder );
32
46
}
33
47
Collection <Cache > caches = new ArrayList <>(names .size ());
34
48
for (String name : names ) {
35
- caches .add (cacheCreator . createCache (name ));
49
+ caches .add (new GuavaCache (name , cacheBuilder . build () ));
36
50
}
37
- return new GuavaCacheManager (caches , cacheCreator );
51
+ return new GuavaCacheManager (caches , cacheBuilder );
52
+ }
53
+
54
+ // --------------------------------------------Custom---------------------------------------------------------------
55
+
56
+ public static GuavaCacheManager newCacheManager (Map <String , CacheCreator > cacheCreatorMap ) {
57
+ return newCacheManager (cacheCreatorMap , null );
58
+ }
59
+
60
+ public static GuavaCacheManager newCacheManager (Map <String , CacheCreator > cacheCreatorMap ,
61
+ CacheCreator defaultCacheCreator ) {
62
+ Collection <Cache > caches = new ArrayList <>();
63
+ cacheCreatorMap .forEach ((k , v ) -> caches .add (v .createCache (k )));
64
+ return new GuavaCacheManager (caches , defaultCacheCreator );
38
65
}
39
66
40
67
public static BootCacheManager newCacheManager (CacheCreator cacheCreator , String ... names ) {
0 commit comments