|
|
@@ -0,0 +1,66 @@
|
|
|
+package cn.iaun.it.util;
|
|
|
+
|
|
|
+
|
|
|
+import org.springframework.data.redis.core.HashOperations;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.data.redis.core.ValueOperations;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.time.Duration;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+@Component
|
|
|
+public class RedisUtil { // 常用redis操作
|
|
|
+ private final RedisTemplate<String, String> redisTemplate;
|
|
|
+
|
|
|
+ public RedisUtil(RedisTemplate<String, String> redisTemplate) {
|
|
|
+ this.redisTemplate = redisTemplate;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected ValueOperations<String, String> _set(String key, String val) {
|
|
|
+ ValueOperations<String, String> ops = redisTemplate.opsForValue();
|
|
|
+ ops.set(key, val);
|
|
|
+ return ops;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void set(String key, String val) {
|
|
|
+ _set(key, val);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void set(String key, String val, Duration expire) {
|
|
|
+ ValueOperations<String, String> ops = _set(key, val);
|
|
|
+ ops.getAndExpire(key, expire);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void set(String key, String val, Long time, TimeUnit timeUnit) {
|
|
|
+ ValueOperations<String, String> ops = _set(key, val);
|
|
|
+ ops.getAndExpire(key, time, timeUnit);
|
|
|
+ }
|
|
|
+
|
|
|
+ public String get(String key) {
|
|
|
+ ValueOperations<String, String> ops = redisTemplate.opsForValue();
|
|
|
+ return ops.get(key);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void hSet(String hash, String hashKey, String hashVal) {
|
|
|
+ HashOperations<String, String, String> opsHash = redisTemplate.opsForHash();
|
|
|
+ opsHash.put(hash, hashKey, hashVal);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void hSet(String hash, Map<String, String> map) {
|
|
|
+ HashOperations<String, String, String> opsHash = redisTemplate.opsForHash();
|
|
|
+ opsHash.putAll(hash, map);
|
|
|
+ }
|
|
|
+
|
|
|
+ public String hGet(String hash, String hashKey) {
|
|
|
+ HashOperations<String, String, String> opsHash = redisTemplate.opsForHash();
|
|
|
+ return opsHash.get(hash, hashKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Set<String> hKeys(String hash) {
|
|
|
+ HashOperations<String, String, String> opsHash = redisTemplate.opsForHash();
|
|
|
+ return opsHash.keys(hash);
|
|
|
+ }
|
|
|
+}
|