1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.core.impl.cache;
17
18 import org.apache.commons.collections.CollectionUtils;
19 import org.apache.commons.lang.StringUtils;
20 import org.apache.log4j.Logger;
21 import org.kuali.rice.core.api.cache.CacheAdminService;
22 import org.kuali.rice.core.api.cache.CacheTarget;
23 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
24 import org.springframework.beans.factory.InitializingBean;
25 import org.springframework.cache.Cache;
26 import org.springframework.cache.CacheManager;
27
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.List;
31
32
33
34
35
36
37
38 public class CacheAdminServiceImpl implements CacheAdminService, InitializingBean {
39
40 private static final Logger LOG = Logger.getLogger(CacheAdminServiceImpl.class);
41
42 private CacheManager cacheManager;
43
44 @Override
45 public void flush(Collection<CacheTarget> cacheTargets) throws RiceIllegalArgumentException {
46 if (CollectionUtils.isNotEmpty(cacheTargets)) {
47 logCacheFlush(cacheTargets);
48 for (CacheTarget cacheTarget : cacheTargets) {
49 if (cacheTarget == null) {
50 throw new RiceIllegalArgumentException("cacheTarget is null");
51 }
52 final Cache c = getCache(cacheTarget.getCache());
53 if (c != null) {
54 if (cacheTarget.containsKey()) {
55 c.evict(cacheTarget.getKey());
56 } else {
57 c.clear();
58 }
59 }
60 }
61 }
62 }
63
64 protected void logCacheFlush(Collection<CacheTarget> cacheTargets) {
65 if (LOG.isDebugEnabled()) {
66 List<String> cacheTargetLog = new ArrayList<String>(cacheTargets.size());
67 for (CacheTarget cacheTarget : cacheTargets) {
68 cacheTargetLog.add(cacheTarget.toString());
69 }
70 LOG.debug("Performing local flush of cache targets [" + StringUtils.join(cacheTargetLog, ", ") + "]");
71 }
72 }
73
74 private Cache getCache(String cache) {
75 return cacheManager.getCache(cache);
76 }
77
78 public void setCacheManager(CacheManager cacheManager) {
79 this.cacheManager = cacheManager;
80 }
81
82 @Override
83 public void afterPropertiesSet() throws Exception {
84 if (cacheManager == null) {
85 throw new IllegalStateException("the cacheManager must be set");
86 }
87 }
88 }