RedisCacheUtils.java
3.21 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
package com.yoho.search.common.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
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.base.utils.SearchCollectionUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.*;
import java.util.concurrent.TimeUnit;
public class RedisCacheUtils {
public static <T> T get(YHValueOperations valueOperations, RedisKeyBuilder redisKeyBuilder, Class<T> clazz) {
RedisKeyBuilder keyBuilder = RedisKeyBuilder.newInstance().appendFixed(redisKeyBuilder);
String compressedVal = valueOperations.get(keyBuilder);
if (StringUtils.isBlank(compressedVal)) {
return null;
}
String uncompressStr = SnappyUtils.uncompress(compressedVal);
return unserializeFromString(uncompressStr, clazz);
}
public static <T> void add(YHValueOperations valueOperations, RedisKeyBuilder redisKeyBuilder, T t, int expiredTimeInMinute) {
String uncompressStr = serializeToString(t);
String compressedVal = SnappyUtils.compress(uncompressStr);
if (StringUtils.isBlank(compressedVal)) {
return;
}
valueOperations.set(redisKeyBuilder, compressedVal, expiredTimeInMinute, TimeUnit.MINUTES);
}
public static boolean exist(YHRedisTemplate redisTemplate, RedisKeyBuilder redisKeyBuilder) {
return redisTemplate.hasKey(redisKeyBuilder);
}
public static void mutiSet(YHRedisTemplate redisTemplate, Map<RedisKeyBuilder, String> map, int expiredTimeInMinute) {
redisTemplate.mset(map, expiredTimeInMinute*60);
}
public static List<String> mutiGet(YHValueOperations valueOperations, Collection<RedisKeyBuilder> redisKeyBuilders) {
return valueOperations.multiGet(redisKeyBuilders);
}
/**
* 设置bitset-整个设置
* @param redisTemplate
* @param redisKeyBuilder
* @param bitSet
* @param expiredTimeInMinute
* @return
*/
public static boolean setBitSet(YHRedisTemplate redisTemplate, RedisKeyBuilder redisKeyBuilder, BitSet bitSet, int expiredTimeInMinute) {
return redisTemplate.setBits(redisKeyBuilder,bitSet,expiredTimeInMinute * 60);
}
/**
* 获取bitset
* @param redisTemplate
* @param redisKeyBuilder
* @param offsets
* @return
*/
public static Map<Integer, Boolean> getBits(YHRedisTemplate redisTemplate, RedisKeyBuilder redisKeyBuilder, Collection<Integer> offsets) {
List<Long> LongOffsets = SearchCollectionUtils.toList(offsets,(a)->a.longValue());
Map<Long, Boolean> redisResults = redisTemplate.getBits(redisKeyBuilder,LongOffsets);
if(redisResults==null || redisResults.isEmpty()){
return null;
}
Map<Integer, Boolean> results = new HashMap<>();
for (Map.Entry<Long, Boolean> entry: redisResults.entrySet()) {
results.put(entry.getKey().intValue(),entry.getValue());
}
return results;
}
/**
* 序列化保持null值
*
* @param t
* @return
*/
private static <T> String serializeToString(T t) {
return JSON.toJSONString(t, SerializerFeature.WriteMapNullValue);
}
private static <T> T unserializeFromString(String cacheObjectValue, Class<T> clazz) {
return JSON.parseObject(cacheObjectValue, clazz);
}
}