|
@@ -0,0 +1,96 @@
|
|
|
+package com.hrsk.cloud.eg.infrastructure.utils;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author zhangyy
|
|
|
+ * @version 1.0
|
|
|
+ * @description: RedisUtil
|
|
|
+ * @date 2024/8/29 10:19
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class RedisUtil {
|
|
|
+ @Resource
|
|
|
+ private RedisTemplate<String, Object> redisTemplate;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量删除对应的value
|
|
|
+ *
|
|
|
+ * @param keys
|
|
|
+ */
|
|
|
+ public void remove(final String... keys) {
|
|
|
+ for (String key : keys) {
|
|
|
+ remove(key);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 删除对应的value
|
|
|
+ *
|
|
|
+ * @param key
|
|
|
+ */
|
|
|
+ public void remove(final String key) {
|
|
|
+ if (exists(key)) {
|
|
|
+ redisTemplate.delete(key);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断缓存中是否有对应的value
|
|
|
+ *
|
|
|
+ * @param key
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public boolean exists(final String key) {
|
|
|
+ return redisTemplate.hasKey(key);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @description: 原子设置(过期时间必须填写)
|
|
|
+ * @param: long 过期时间 秒为单位
|
|
|
+ * @return:
|
|
|
+ * @author zhangyy
|
|
|
+ * @date: 2024/8/29 10:35
|
|
|
+ */
|
|
|
+ public Boolean atomSet(final String key, final Object value,final long time) {
|
|
|
+ return redisTemplate.opsForValue().setIfAbsent(key, value,time, TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @description: 从redis 获取信息 k-v (v简单对象)
|
|
|
+ * @param:
|
|
|
+ * @return:
|
|
|
+ * @author zhangyy
|
|
|
+ * @date: 2024/8/29 10:38
|
|
|
+ */
|
|
|
+ public Object get(final String key) {
|
|
|
+ if (exists(key)) {
|
|
|
+ return redisTemplate.opsForValue().get(key);
|
|
|
+ }
|
|
|
+ return new Object();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @description:
|
|
|
+ * @param: long time 秒
|
|
|
+ * @return:
|
|
|
+ * @author zhangyy
|
|
|
+ * @date: 2024/8/29 10:43
|
|
|
+ */
|
|
|
+ public Boolean set(final String key, final Object value,final long time) {
|
|
|
+ try {
|
|
|
+ redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
|
|
|
+ return true;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("redis set error",e);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|