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
| package cn.sdadgz.web_springboot.utils;
import lombok.extern.slf4j.Slf4j; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component;
import javax.annotation.Resource; import java.time.Duration; import java.util.Map; import java.util.Set; import java.util.function.Consumer;
@Component @Slf4j public class RedisUtil {
public static final int DEFAULT_INCR_INT_VALUE = 1;
public static final int DEFAULT_LOCK_TIMEOUT = 233;
@Resource private RedisTemplate<String, Object> redisTemplate;
public Set<String> getKeys(String prefix) { return redisTemplate.keys(prefix + "*"); }
public Object get(String key) { return redisTemplate.opsForValue().get(key); }
public <T> T get(String key, Class<T> t) { Object o = get(key); if (t.isInstance(o)) { return t.cast(o); } return null; }
public void set(String key, Object value) { redisTemplate.opsForValue().set(key, value); }
public void set(String key, Object value, long second) {
redisTemplate.opsForValue().set(key, value, Duration.ofSeconds(second)); }
public void setIncrExp(String key, long timeout) { setIncrExp(key, timeout, (k) -> log.info("创建自增定期键:{},过期时间{}秒", k, timeout)); }
public void setIncrExp(String key, long timeout, Consumer<String> consumer) { if (GeneralUtil.isNull(get(key))) { set(key, DEFAULT_INCR_INT_VALUE, timeout); consumer.accept(key); return; }
redisTemplate.opsForValue().increment(key); }
public Boolean expire(String key, long second) { return redisTemplate.expire(key, Duration.ofSeconds(second)); }
public Map<Object, Object> getHash(String key) { return redisTemplate.opsForHash().entries(key); }
public Object getHash(String key, Object index) { return redisTemplate.opsForHash().get(key, index); }
public <T> T getHash(String key, Object index, Class<T> clazz) { return clazz.cast(getHash(key, index)); }
public void setHash(String key, Object k, Object v) { redisTemplate.opsForHash().put(key, k, v); }
public Long addHash(String key, Object k, long step) { return redisTemplate.opsForHash().increment(key, k, step); }
public Set<Object> getSet(String key) { return redisTemplate.opsForSet().members(key); }
public Boolean getSet(String key, Object value) { return redisTemplate.opsForSet().isMember(key, value); }
public void setSet(String key, Object... value) { redisTemplate.opsForSet().add(key, value); }
public Boolean delKey(String key){ return redisTemplate.delete(key); }
public Boolean lock(String key) { return lock(key, DEFAULT_LOCK_TIMEOUT); }
public Boolean lock(String key, long ms) { return redisTemplate.opsForValue().setIfAbsent(key, System.currentTimeMillis(), Duration.ofMillis(ms)); }
public Boolean unlock(String key){ return delKey(key); }
}
|