4
4
import com .google .common .cache .CacheBuilder ;
5
5
import org .code4everything .boot .cache .guava .GuavaCache ;
6
6
import org .code4everything .boot .cache .guava .GuavaCacheManager ;
7
+ import org .code4everything .boot .cache .redis .RedisCache ;
8
+ import org .code4everything .boot .cache .redis .RedisCacheCreator ;
9
+ import org .code4everything .boot .cache .redis .RedisCacheManager ;
7
10
import org .springframework .cache .Cache ;
8
11
9
12
import java .util .ArrayList ;
@@ -21,16 +24,43 @@ public class CacheUtils {
21
24
22
25
private CacheUtils () {}
23
26
27
+
28
+ // -------------------------------------------Redis-----------------------------------------------------------------
29
+
30
+ public static RedisCacheManager newRedisCacheManager (Map <String , RedisCacheCreator > redisCacheCreatorMap ) {
31
+ return newRedisCacheManager (redisCacheCreatorMap , null );
32
+ }
33
+
34
+ public static RedisCacheManager newRedisCacheManager (Map <String , RedisCacheCreator > redisCacheCreatorMap ,
35
+ RedisCacheCreator defaultRedisCacheCreator ) {
36
+ Collection <RedisCache > caches = new ArrayList <>();
37
+ redisCacheCreatorMap .forEach ((k , v ) -> caches .add (v .createCache (k )));
38
+ return new RedisCacheManager (caches , defaultRedisCacheCreator );
39
+ }
40
+
41
+ public static RedisCacheManager newRedisCacheManager (RedisCacheCreator redisCacheCreator , String ... names ) {
42
+ return newRedisCacheManager (redisCacheCreator , Arrays .asList (names ));
43
+ }
44
+
45
+ public static RedisCacheManager newRedisCacheManager (RedisCacheCreator redisCacheCreator ,
46
+ Collection <String > names ) {
47
+ if (CollUtil .isEmpty (names )) {
48
+ return new RedisCacheManager (redisCacheCreator );
49
+ }
50
+ Collection <RedisCache > caches = new ArrayList <>(names .size ());
51
+ names .forEach (name -> caches .add (redisCacheCreator .createCache (name )));
52
+ return new RedisCacheManager (caches , redisCacheCreator );
53
+ }
54
+
24
55
// ---------------------------------------Guava---------------------------------------------------------------------
25
56
26
57
public static GuavaCacheManager newGuavaCacheManager (Map <String , CacheBuilder <Object , Object >> cacheBuilderMap ) {
27
58
return newGuavaCacheManager (cacheBuilderMap , null );
28
59
}
29
60
30
-
31
61
public static GuavaCacheManager newGuavaCacheManager (Map <String , CacheBuilder <Object , Object >> cacheBuilderMap ,
32
62
CacheBuilder <Object , Object > defaultCacheBuilder ) {
33
- Collection <Cache > caches = new ArrayList <>();
63
+ Collection <GuavaCache > caches = new ArrayList <>();
34
64
cacheBuilderMap .forEach ((k , v ) -> caches .add (new GuavaCache (k , v .build ())));
35
65
return new GuavaCacheManager (caches , defaultCacheBuilder );
36
66
}
@@ -44,24 +74,22 @@ public static GuavaCacheManager newGuavaCacheManager(CacheBuilder<Object, Object
44
74
if (CollUtil .isEmpty (names )) {
45
75
return new GuavaCacheManager (cacheBuilder );
46
76
}
47
- Collection <Cache > caches = new ArrayList <>(names .size ());
48
- for (String name : names ) {
49
- caches .add (new GuavaCache (name , cacheBuilder .build ()));
50
- }
77
+ Collection <GuavaCache > caches = new ArrayList <>(names .size ());
78
+ names .forEach (name -> caches .add (new GuavaCache (name , cacheBuilder .build ())));
51
79
return new GuavaCacheManager (caches , cacheBuilder );
52
80
}
53
81
54
82
// --------------------------------------------Custom---------------------------------------------------------------
55
83
56
- public static GuavaCacheManager newCacheManager (Map <String , CacheCreator > cacheCreatorMap ) {
84
+ public static BootCacheManager newCacheManager (Map <String , CacheCreator > cacheCreatorMap ) {
57
85
return newCacheManager (cacheCreatorMap , null );
58
86
}
59
87
60
- public static GuavaCacheManager newCacheManager (Map <String , CacheCreator > cacheCreatorMap ,
61
- CacheCreator defaultCacheCreator ) {
88
+ public static BootCacheManager newCacheManager (Map <String , CacheCreator > cacheCreatorMap ,
89
+ CacheCreator defaultCacheCreator ) {
62
90
Collection <Cache > caches = new ArrayList <>();
63
91
cacheCreatorMap .forEach ((k , v ) -> caches .add (v .createCache (k )));
64
- return new GuavaCacheManager (caches , defaultCacheCreator );
92
+ return new BootCacheManager (caches , defaultCacheCreator );
65
93
}
66
94
67
95
public static BootCacheManager newCacheManager (CacheCreator cacheCreator , String ... names ) {
@@ -73,9 +101,7 @@ public static BootCacheManager newCacheManager(CacheCreator cacheCreator, Collec
73
101
return new BootCacheManager (cacheCreator );
74
102
}
75
103
Collection <Cache > caches = new ArrayList <>(names .size ());
76
- for (String name : names ) {
77
- caches .add (cacheCreator .createCache (name ));
78
- }
104
+ names .forEach (name -> caches .add (cacheCreator .createCache (name )));
79
105
return new BootCacheManager (caches , cacheCreator );
80
106
}
81
107
}
0 commit comments