|
@@ -0,0 +1,236 @@
|
|
|
|
|
+package cn.poyee.common.redis.service;
|
|
|
|
|
+
|
|
|
|
|
+import cn.poyee.common.constraint.Constant;
|
|
|
|
|
+import cn.poyee.common.util.JsonUtil;
|
|
|
|
|
+
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
|
+
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.data.redis.core.RedisCallback;
|
|
|
|
|
+import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
|
|
+import org.springframework.data.redis.core.script.RedisScript;
|
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
+
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@Component
|
|
|
|
|
+public class RedisService {
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private StringRedisTemplate template;
|
|
|
|
|
+
|
|
|
|
|
+ public void set(String key, Object object, long exp, TimeUnit unit) {
|
|
|
|
|
+ template.opsForValue().set(key, Objects.requireNonNull(JsonUtil.toStr(object)), exp, unit);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void set(String key, Object object) {
|
|
|
|
|
+ template.opsForValue().set(key, Objects.requireNonNull(JsonUtil.toStr(object)));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public <T> T get(String category, String key, Class<T> clazz) {
|
|
|
|
|
+ String v = template.opsForValue().get(category.concat(key));
|
|
|
|
|
+ T t = JsonUtil.toBean(v, clazz);
|
|
|
|
|
+ return t;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * lpush 方法 并指定过期时间
|
|
|
|
|
+ */
|
|
|
|
|
+ public void lPush(String key, String value, Long seconds) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ template.executePipelined((RedisCallback<String>) connection -> {
|
|
|
|
|
+ connection.lPush(key.getBytes(), value.getBytes());
|
|
|
|
|
+ connection.expire(key.getBytes(), seconds);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ });
|
|
|
|
|
+ } catch (Exception ex) {
|
|
|
|
|
+ log.error("RedisUtils#pipelineSetEx fail! e:{}", ex.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void pipelineSetEx(Map<String, String> keyValues, Long seconds) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ template.executePipelined((RedisCallback<String>) connection -> {
|
|
|
|
|
+ for (Map.Entry<String, String> entry : keyValues.entrySet()) {
|
|
|
|
|
+ connection.setEx(entry.getKey().getBytes(), seconds,
|
|
|
|
|
+ entry.getValue().getBytes());
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ });
|
|
|
|
|
+ } catch (Exception ex) {
|
|
|
|
|
+ log.error("RedisUtils#pipelineSetEx fail! e:{}", ex.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Long lastIndexOf(String key, String value) {
|
|
|
|
|
+ return template.opsForList().lastIndexOf(key, value);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Long listRemove(String key, long count, String value) {
|
|
|
|
|
+ return template.opsForList().remove(key, count, value);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Long listSize(String key) {
|
|
|
|
|
+ return template.opsForList().size(key);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public <T> T get(String key, Class<T> clazz) {
|
|
|
|
|
+ String v = template.opsForValue().get(key);
|
|
|
|
|
+ if (null == v) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ T t = JsonUtil.toBean(v, clazz);
|
|
|
|
|
+ return t;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public String get(String category, String key) {
|
|
|
|
|
+ return template.opsForValue().get(category.concat(key));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //
|
|
|
|
|
+ public void delete(String key) {
|
|
|
|
|
+ template.delete(key);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void delete(Collection<String> keys) {
|
|
|
|
|
+ template.delete(keys);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void delete(List<String> keys) {
|
|
|
|
|
+ template.delete(keys);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Set<String> keys(String pattern) {
|
|
|
|
|
+ return template.keys(pattern);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Set<String> keys(String category, String pattern) {
|
|
|
|
|
+ return template.keys(category + pattern);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public List<String> multiGet(Set<String> pattern) {
|
|
|
|
|
+ return template.opsForValue().multiGet(pattern);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void multiSet(Map<String, String> pattern) {
|
|
|
|
|
+ template.opsForValue().multiSet(pattern);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Map<Object, Object> hmget(String key) {
|
|
|
|
|
+ template.opsForHash().entries(key);
|
|
|
|
|
+ return template.opsForHash().entries(key);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public String hmget(String key, String hmkey) {
|
|
|
|
|
+ Object obj = template.opsForHash().get(key, hmkey);
|
|
|
|
|
+
|
|
|
|
|
+ return String.valueOf(obj);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Set<Object> hmkeys(String key) {
|
|
|
|
|
+ return template.opsForHash().keys(key);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public boolean putAll(String key, Object object) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ set(key, object);
|
|
|
|
|
+ return true;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public long hmdel(String key, String hkey) {
|
|
|
|
|
+ return template.opsForHash().delete(key, hkey);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public boolean hmhas(String key, String hkey) {
|
|
|
|
|
+ return template.opsForHash().hasKey(key, hkey);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public long leftPush(String key, String values) {
|
|
|
|
|
+ return template.opsForList().leftPush(key, values);
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Boolean hasKey(String key) {
|
|
|
|
|
+ return template.hasKey(key);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 执行指定的lua脚本返回执行结果
|
|
|
|
|
+ * --KEYS[1]: 限流 key
|
|
|
|
|
+ * --ARGV[1]: 限流窗口
|
|
|
|
|
+ * --ARGV[2]: 当前时间戳(作为score)
|
|
|
|
|
+ * --ARGV[3]: 阈值
|
|
|
|
|
+ * --ARGV[4]: score 对应的唯一value
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param redisScript
|
|
|
|
|
+ * @param keys
|
|
|
|
|
+ * @param args
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public Boolean execLimitLua(RedisScript<Long> redisScript, List<String> keys, String... args) {
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ Long execute = template.execute(redisScript, keys, args);
|
|
|
|
|
+ return Constant.TRUE.equals(execute.intValue());
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("redis execLimitLua fail! e:{}", e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Long setRemove(String key, String... v) {
|
|
|
|
|
+ return template.opsForSet().remove(key, v);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Long setAdd(String key, String... v) {
|
|
|
|
|
+ return template.opsForSet().add(key, v);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Long setSize(String key) {
|
|
|
|
|
+ return template.opsForSet().size(key);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Set<String> members(String key) {
|
|
|
|
|
+ return template.opsForSet().members(key);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public boolean expire(String key, long exp, TimeUnit unit) {
|
|
|
|
|
+ return Boolean.TRUE.equals(template.expire(key, exp, unit));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ public Long removeRange(String key, long start, long end) {
|
|
|
|
|
+ return template.opsForZSet().removeRange(key, start, end);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Set reverseRange(String key, long start, long end) {
|
|
|
|
|
+ return template.opsForZSet().reverseRange(key, start, end);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Long increment(String key, final long timeout, final TimeUnit unit) {
|
|
|
|
|
+ final Long increment = template.opsForValue().increment(key);
|
|
|
|
|
+ template.expire(key, timeout, unit);
|
|
|
|
|
+ return increment;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Long decrement(String key, final long timeout, final TimeUnit unit) {
|
|
|
|
|
+ final Long decrement = template.opsForValue().decrement(key);
|
|
|
|
|
+ template.expire(key, timeout, unit);
|
|
|
|
|
+ return decrement;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public boolean setBit(String key, int idx, boolean bool) {
|
|
|
|
|
+ return template.opsForValue().setBit(key, idx, bool);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public boolean getBit(String key, int idx) {
|
|
|
|
|
+ return template.opsForValue().getBit(key, idx);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|