ESCacheManager.java
1.92 KB
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
package com.zhaoonline.es.cache;
import java.util.Collection;
import java.util.Collections;
import org.springframework.cache.Cache;
import org.springframework.cache.transaction.AbstractTransactionSupportingCacheManager;
import org.springframework.cache.transaction.TransactionAwareCacheDecorator;
import com.zhaoonline.es.core.ESClient;
/**
* class name:ESCacheManager <BR>
* class description: ESCacheManager的作用是为了将Cache交给Spring来管理 <BR>
* Remark: <BR>
* @version 1.00 2016年8月23日
* @author zhaoonline)yangyoupeng
*/
public class ESCacheManager extends AbstractTransactionSupportingCacheManager {
private ESClient client=null;
public ESCacheManager(ESClient client) {
this.client=client;
}
@Override
public Cache getCache(String name) {
Cache cache = super.getCache(name);
if (cache == null ) {
return createAndAddCache(name);
}
return cache;
}
@SuppressWarnings("deprecation")
private Cache createAndAddCache(String cacheName) {
addCache(createCache(cacheName));
return super.getCache(cacheName);
}
protected ESCache createCache(String cacheName) {
return new ESCache(cacheName,client);
}
/**
* @Override
* @see org.springframework.cache.support.AbstractCacheManager#loadCaches() <BR>
* Method name: loadCaches <BR>
* Description:这里我们创建一个emptyList <BR>
* Remark: <BR>
* @return <BR>
*/
@Override
protected Collection<? extends Cache> loadCaches() {
return Collections.<Cache> emptyList();
}
/* (non-Javadoc)
* @see
org.springframework.cache.transaction.AbstractTransactionSupportingCacheManager#decorateCache(org.springframework.cache.Cache)
*/
@Override
protected Cache decorateCache(Cache cache) {
if (isCacheAlreadyDecorated(cache)) {
return cache;
}
return super.decorateCache(cache);
}
protected boolean isCacheAlreadyDecorated(Cache cache) {
return isTransactionAware() && cache instanceof TransactionAwareCacheDecorator;
}
}