1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.cache.support;
18
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.LinkedHashSet;
22 import java.util.Set;
23 import java.util.concurrent.ConcurrentHashMap;
24 import java.util.concurrent.ConcurrentMap;
25
26 import org.springframework.beans.factory.InitializingBean;
27 import org.springframework.cache.Cache;
28 import org.springframework.cache.CacheManager;
29
30
31
32
33
34
35
36
37
38
39 public abstract class AbstractCacheManager implements CacheManager, InitializingBean {
40
41 private final ConcurrentMap<String, Cache> cacheMap = new ConcurrentHashMap<String, Cache>(16);
42
43 private Set<String> cacheNames = new LinkedHashSet<String>(16);
44
45
46
47
48 @Override
49 public void afterPropertiesSet() {
50 Collection<? extends Cache> caches = loadCaches();
51
52
53 this.cacheMap.clear();
54 this.cacheNames.clear();
55 for (Cache cache : caches) {
56 addCache(cache);
57 }
58 }
59
60
61
62
63
64
65 protected abstract Collection<? extends Cache> loadCaches();
66
67
68
69
70 @Override
71 public Cache getCache(String name) {
72 Cache cache = lookupCache(name);
73 if (cache != null) {
74 return cache;
75 }
76 else {
77 Cache missingCache = getMissingCache(name);
78 if (missingCache != null) {
79 addCache(missingCache);
80 return lookupCache(name);
81 }
82 return null;
83 }
84 }
85
86 @Override
87 public Collection<String> getCacheNames() {
88 return Collections.unmodifiableSet(this.cacheNames);
89 }
90
91
92
93
94 protected final void addCache(Cache cache) {
95 this.cacheMap.put(cache.getName(), decorateCache(cache));
96 this.cacheNames.add(cache.getName());
97 }
98
99 protected final Cache lookupCache(String name) {
100 return this.cacheMap.get(name);
101 }
102
103
104
105
106
107
108
109 protected Cache decorateCache(Cache cache) {
110 return cache;
111 }
112
113
114
115
116
117
118
119
120
121
122
123
124
125 protected Cache getMissingCache(String name) {
126 return null;
127 }
128
129 }