EhCache.java
3.98 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package com.yoho.search.cache.impls;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.search.cache.model.CacheObject;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import net.sf.ehcache.Statistics;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.InputStream;
import java.util.*;
@Service("ehCache")
public class EhCache implements CacheInterface {
private static final Logger logger = LoggerFactory.getLogger(EhCache.class);
private CacheManager manager;
private Cache cache;
private EhCache() {
}
@PostConstruct
private void init() {
try {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("ehcache.xml");
this.manager = CacheManager.create(inputStream);
this.cache = manager.getCache("yohosearch");
} catch (Throwable e) {
e.printStackTrace();
}
}
public Statistics getStatistics(){
return cache.getStatistics();
}
@PreDestroy
private void destroy() {
if (this.manager != null) {
this.manager.shutdown();
}
}
@Override
public void addOrUpdate(RedisKeyBuilder redisKeyBuilder, CacheObject value, int expiredTimeInMinute) {
try {
Element element = new Element(redisKeyBuilder.getKey(), value);
element.setTimeToLive(expiredTimeInMinute * 60);
cache.put(element);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
@Override
public CacheObject get(RedisKeyBuilder redisKeyBuilder) {
try {
Element e = cache.get(redisKeyBuilder.getKey());
return (e == null) ? null : (CacheObject) e.getObjectValue();
} catch (Exception e) {
logger.error(e.getMessage(), e);
return null;
}
}
@Override
public boolean exist(RedisKeyBuilder redisKeyBuilder) {
return this.get(redisKeyBuilder) == null ? false : true;
}
@Override
public void mutiSet(final Map<RedisKeyBuilder,String> map, final int expiredTimeInMinute) {
try {
Collection<Element> elements = new ArrayList<>();
for (Map.Entry<RedisKeyBuilder, ? extends String> entry: map.entrySet()) {
Element element = new Element(entry.getKey().getKey(), entry.getValue());
element.setTimeToLive(expiredTimeInMinute * 60);
elements.add(element);
}
cache.putAll(elements);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
@Override
public List<String> mutiGet(Collection<RedisKeyBuilder> redisKeyBuilders) {
Collection<String> keys = new ArrayList<>();
for (RedisKeyBuilder redisKeyBuilder:redisKeyBuilders) {
keys.add(redisKeyBuilder.getKey());
}
Map<Object, Element> elementMap = cache.getAll(keys);
List<String> values = new ArrayList<>();
for (RedisKeyBuilder redisKeyBuilder:redisKeyBuilders) {
Element element = elementMap.get(redisKeyBuilder.getKey());
if(element==null){
values.add(null);
}else{
values.add((String)element.getObjectValue());
}
}
return values;
}
@Override
public boolean setBitSet(RedisKeyBuilder redisKeyBuilder, BitSet bitSet, int expiredTimeInMinute) {
try {
Element element = new Element(redisKeyBuilder.getKey(),bitSet);
element.setTimeToLive(expiredTimeInMinute * 60);
cache.put(element);
return true;
} catch (Exception e) {
logger.error(e.getMessage(), e);
return false;
}
}
@Override
public Map<Integer, Boolean> getFromBitSet(RedisKeyBuilder key, Collection<Integer> offsets){
try {
Element e = this.cache.get(key.getKey());
if(e==null){
return null;
}
BitSet cacheBitSet = (BitSet)e.getObjectValue();
Map<Integer, Boolean> results = new HashMap<>();
for (Integer offset:offsets) {
results.put(offset,cacheBitSet.get(offset));
}
return results;
} catch (Exception e) {
logger.error(e.getMessage(), e);
return null;
}
}
}