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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
| import com.cdmtc.redis.config.RedisExpireConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.*; import org.springframework.util.CollectionUtils;
import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit;
@SuppressWarnings("unchecked") public class RedisService { private Logger logger = LoggerFactory.getLogger(RedisService.class); @SuppressWarnings("rawtypes") private RedisTemplate template; @SuppressWarnings({ "rawtypes" }) private ListOperations listOpe; @SuppressWarnings("rawtypes") private ValueOperations valueOpe; @SuppressWarnings("rawtypes") private HashOperations hashOpe; @SuppressWarnings("rawtypes") private ZSetOperations zsetOpe; @SuppressWarnings("rawtypes") private SetOperations setOpe; @Autowired private RedisExpireConfig expireConfig;
@SuppressWarnings("rawtypes") public RedisService(RedisTemplate template) { this.template = template; listOpe = template.opsForList(); valueOpe = template.opsForValue(); hashOpe = template.opsForHash(); zsetOpe = template.opsForZSet(); setOpe = template.opsForSet(); this.template = template; }
private String getKey(String head, String key) { return head + ":" + key; }
public String get(String head, String key) { return (String) valueOpe.get(getKey(head, key)); }
public void set(String head, String key, String value) { valueOpe.set(getKey(head, key), value); expire(head, key); }
private boolean expire(String head, String key) { long times = expireConfig.getExpire4Header(head); if (times > 0) { try { return expire(head, key, times, TimeUnit.SECONDS); } catch (Exception e) { logger.warn("过期时间设置失败{head:" + head + ",key:" + key + "}。"); } } return false; }
public boolean expire(String head, String key, long timeout, TimeUnit unit) { return template.expire(getKey(head, key), timeout, unit); }
public boolean zadd(String head, String key, String member, double score) { boolean result = zsetOpe.add(getKey(head, key), member, score); expire(head, key); return result; }
public Set<String> rang(String head, String key, long start, long end) { return zsetOpe.range(getKey(head, key), start, end); }
public Set<String> reverseRange(String head, String key, long start, long end) { return zsetOpe.reverseRange(getKey(head, key), start, end); }
public double score(String head, String key, String member) { return zsetOpe.score(getKey(head, key), member); }
public long rank(String head, String key, String member) { return zsetOpe.rank(getKey(head, key), member); }
public long reverseRank(String head, String key, String member) { return zsetOpe.reverseRank(getKey(head, key), member); }
public Set<ZSetOperations.TypedTuple<String>> reverseRangeWithScores(String head, String key, long start, long end) { return zsetOpe.reverseRangeWithScores(getKey(head, key), start, end); }
public List<String> hmget(String head, String key, Collection<?> fields) { if (CollectionUtils.isEmpty(fields)) { return null; } return hashOpe.multiGet(getKey(head, key), fields); }
public void hset(String head, String key, String field, String value) { hashOpe.put(getKey(head, key), field, value); expire(head, key); }
public Set<String> keys(String head, String key) { return hashOpe.keys(getKey(head, key)); }
public String hget(String head, String key, String field) { return (String) hashOpe.get(getKey(head, key), field); }
public Set<String> smembers(String head, String key) { return setOpe.members(getKey(head, key)); }
public long sadd(String head, String key, String value) { long result = setOpe.add(getKey(head, key), value); expire(head, key); return result; }
public int append(String head, String key, String addString) { return valueOpe.append(getKey(head, key), addString); }
public boolean setnx(String head, String key, String lockValue) { boolean success = valueOpe.setIfAbsent(getKey(head, key), lockValue); if (success) { expire(head, key); } return success; }
public boolean delete(String head, String key) { return template.delete(getKey(head, key)); }
public long rightPush(String head, String key, String value) { return listOpe.rightPush(getKey(head, key), value); }
public Double incrementScore(String head, String key, String member, Double score) { return zsetOpe.incrementScore(getKey(head, key), member, score); }
public void hmset(String head, String key, Map<? extends String, ? extends String> map) { if (!CollectionUtils.isEmpty(map)) { hashOpe.putAll(getKey(head, key), map); expire(head, key); } }
public Double Hincrby(String head, String key, String filed, Double value) { return hashOpe.increment(getKey(head, key), filed, value); }
public Long hdel(String head, String key, String field) { return hashOpe.delete(getKey(head, key), field); }
public boolean hasKey(String head, String key) { return template.hasKey(getKey(head, key)); }
}
|