PushRedisService.java
4.71 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
145
146
147
148
package com.yoho.yhmessage.redis;
import com.yoho.core.redis.cluster.annotation.Redis;
import com.yoho.core.redis.cluster.operations.nosync.*;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
*
* PUSH专用redis,和公用redis集群分开
*
* @author hugufei
*/
@Service
public class PushRedisService {
@Redis("yohoPushRedis")
private YHRedisTemplate pushRedisTemplate;
@Redis("yohoPushRedis")
private YHValueOperations pushValueOperations;
@Redis("yohoPushRedis")
private YHListOperations pushListOperations;
@Redis("yohoPushRedis")
private YHSetOperations pushSetOperations;
@Redis("yohoPushRedis")
private YHHashOperations pushHashOperations;
RedisSerializer<String> stringSerializer = new StringRedisSerializer();
/*************************** redisTemplate *****************************/
public YHRedisTemplate getPushRedisTemplate() {
return pushRedisTemplate;
}
public void longExpire(RedisKeyBuilder redisKey, long expiredTs, TimeUnit timeUnit) {
RedisKeyBuilder redisKeyBuilder = RedisKeyBuilder.newInstance().appendVar(redisKey);
pushRedisTemplate.expire(redisKeyBuilder, expiredTs, timeUnit);
}
public void delete(RedisKeyBuilder redisKey) {
pushRedisTemplate.delete(redisKey);
}
public long getExpire(RedisKeyBuilder redisKey) {
RedisKeyBuilder redisKeyBuilder = RedisKeyBuilder.newInstance().appendVar(redisKey);
return pushRedisTemplate.getExpire(redisKeyBuilder);
}
/*************************** value_operation *****************************/
public boolean value_setIfAbsent(RedisKeyBuilder redisKey, String string) {
return this.pushValueOperations.setIfAbsent(redisKey, string);
}
/*************************** list_operation *****************************/
public void list_rightPushAll(RedisKeyBuilder redisKeyBuilder, List<String> values) {
this.pushListOperations.rightPushAll(redisKeyBuilder, values);
}
public List<String> list_range(RedisKeyBuilder redisKey, long fromIndex, long toIndex) {
return this.pushListOperations.range(redisKey, fromIndex, toIndex);
}
public long list_size(RedisKeyBuilder redisKey) {
return this.pushListOperations.size(redisKey);
}
public void setValue(RedisKeyBuilder redisKey, String value, long expiredTs, TimeUnit timeUnit) {
pushValueOperations.set(redisKey, value, expiredTs, timeUnit);
}
public String getValue(RedisKeyBuilder redisKey) {
return pushValueOperations.get(redisKey);
}
public boolean isMember(RedisKeyBuilder redisKey, String value) {
return pushSetOperations.isMember(redisKey, value);
}
public void sAdd(RedisKeyBuilder redisKey, String value) {
pushSetOperations.add(redisKey, value);
}
public long increment(RedisKeyBuilder redisKey, long increment) {
return pushValueOperations.increment(redisKey, increment);
}
public long hashIncrement(RedisKeyBuilder redisKey, String hKey, long increment, int expireHours){
long count = pushHashOperations.increment(redisKey,hKey,increment);
//设置超时时间
if (pushRedisTemplate.getExpire(redisKey) <= 0) {
pushRedisTemplate.longExpire(redisKey, expireHours, TimeUnit.HOURS);
}
return count;
}
public void getSetFilter(Set<String> orginSet, String redisKey){
if(CollectionUtils.isEmpty(orginSet)){
return;
}
List<String> orginList = new ArrayList<>(orginSet);
Set<String> invalidSet = new HashSet<>();
List<Object> filteredList = pushRedisTemplate.executePipelined(connection -> {
connection.openPipeline();
byte[] key = stringSerializer.serialize(redisKey);
for (String str : orginList) {
connection.sIsMember(key, stringSerializer.serialize(str));
}
return null;
});
for (int j = 0; j < orginSet.size(); j++) {
//如果存在 则过滤
if ("true".equals(filteredList.get(j).toString())) {
invalidSet.add(orginList.get(j));
}
}
orginSet.removeAll(invalidSet);
}
public void setListValue(String redisKey,List<String> valueList,int hours){
//写入redis中的当天发送uid set集合
pushRedisTemplate.executePipelined(connection -> {
connection.openPipeline();
byte[] key = stringSerializer.serialize(redisKey);
for (String value : valueList) {
connection.sAdd(key, stringSerializer.serialize(value));
}
return null;
});
//设置超时时间
RedisKeyBuilder redisTodayKey = RedisKeyBuilder.newInstance().appendFixed(redisKey);
pushRedisTemplate.expire(redisTodayKey,hours,TimeUnit.HOURS);
}
}