SearchRedis.java
3.56 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
package com.yoho.search.cache.impls;
import com.yoho.core.redis.cluster.operations.nosync.YHRedisTemplate;
import com.yoho.core.redis.cluster.operations.nosync.YHValueOperations;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.search.cache.model.CacheObject;
import com.yoho.search.common.utils.RedisCacheUtils;
import com.yoho.search.core.redis.components.YohoSearchRedisComponent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.BitSet;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@Service("searchRedis")
public class SearchRedis implements CacheInterface {
private static final Logger logger = LoggerFactory.getLogger(SearchRedis.class);
@Autowired
private YohoSearchRedisComponent yohoSearchRedisComponent;
@Override
public boolean exist(RedisKeyBuilder redisKeyBuilder) {
try {
YHRedisTemplate redisTemplate = yohoSearchRedisComponent.getRedisTemplate();
return RedisCacheUtils.exist(redisTemplate, redisKeyBuilder);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return false;
}
}
@Override
public CacheObject get(RedisKeyBuilder redisKeyBuilder) {
try {
YHValueOperations valueOperations = yohoSearchRedisComponent.getValueOperations();
return RedisCacheUtils.get(valueOperations, redisKeyBuilder, CacheObject.class);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return null;
}
}
@Override
public void addOrUpdate(RedisKeyBuilder redisKeyBuilder, CacheObject value, int expiredTimeInMinute) {
try {
YHValueOperations valueOperations = yohoSearchRedisComponent.getValueOperations();
RedisCacheUtils.add(valueOperations, redisKeyBuilder, value, expiredTimeInMinute);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
@Override
public void mutiSet(Map<RedisKeyBuilder, String> map, int expiredTimeInMinute) {
YHRedisTemplate redisTemplate = yohoSearchRedisComponent.getRedisTemplate();
RedisCacheUtils.mutiSet(redisTemplate, map, expiredTimeInMinute);
}
@Override
public List<String> mutiGet(Collection<RedisKeyBuilder> redisKeyBuilders) {
YHValueOperations valueOperations = yohoSearchRedisComponent.getValueOperations();
return RedisCacheUtils.mutiGet(valueOperations, redisKeyBuilders);
}
@Override
public boolean setBitSet(RedisKeyBuilder redisKeyBuilder, BitSet bitSet, int expiredTimeInMinute) {
try {
return RedisCacheUtils.setBitSet(yohoSearchRedisComponent.getRedisTemplate(), redisKeyBuilder, bitSet, expiredTimeInMinute);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return false;
}
}
@Override
public Map<Integer, Boolean> getFromBitSet(RedisKeyBuilder key, Collection<Integer> offsets) {
try {
YHRedisTemplate redisTemplate = yohoSearchRedisComponent.getRedisTemplate();
if (!redisTemplate.hasKey(key)) {
return null;
}
return RedisCacheUtils.getBits(redisTemplate, key, offsets);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return null;
}
}
}