From 58b395f3f9d06ebccaa24cf068cbb3cbf6882e93 Mon Sep 17 00:00:00 2001
From: 童刚 <pBP8jclM@8yTJ@4h>
Date: 星期一, 08 八月 2022 11:28:33 +0800
Subject: [PATCH] 1

---
 src/main/java/com/hx/redisson/redis/service/impl/RedisServiceImpl.java |   62 +++++++++++++++++++++++++++++++
 src/main/java/com/hx/redisson/redis/service/RedisService.java          |   35 +++++++++++++++++
 2 files changed, 97 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/hx/redisson/redis/service/RedisService.java b/src/main/java/com/hx/redisson/redis/service/RedisService.java
new file mode 100644
index 0000000..9beda9a
--- /dev/null
+++ b/src/main/java/com/hx/redisson/redis/service/RedisService.java
@@ -0,0 +1,35 @@
+package com.hx.redisson.redis.service;
+
+import java.util.concurrent.TimeUnit;
+
+public interface RedisService {
+
+    /**
+     * 鏅�氱紦瀛樿幏鍙�
+     *
+     * @param key 閿�
+     * @return 鍊�
+     */
+     Object get(String key);
+
+
+    /**
+     * 鏅�氱紦瀛樻斁鍏ュ苟璁剧疆鏃堕棿
+     *
+     * @param key   閿�
+     * @param value 鍊�
+     * @param time  鏃堕棿(绉�) time瑕佸ぇ浜�0 濡傛灉time灏忎簬绛変簬0 灏嗚缃棤闄愭湡
+     * @return true鎴愬姛 false 澶辫触
+     */
+     boolean set(String key, Object value, long time);
+
+
+    /**
+     * 鏅�氱紦瀛樻斁鍏�
+     *
+     * @param key   閿�
+     * @param value 鍊�
+     * @return true鎴愬姛 false澶辫触
+     */
+     boolean set(String key, Object value);
+}
diff --git a/src/main/java/com/hx/redisson/redis/service/impl/RedisServiceImpl.java b/src/main/java/com/hx/redisson/redis/service/impl/RedisServiceImpl.java
new file mode 100644
index 0000000..73cff8b
--- /dev/null
+++ b/src/main/java/com/hx/redisson/redis/service/impl/RedisServiceImpl.java
@@ -0,0 +1,62 @@
+package com.hx.redisson.redis.service.impl;
+
+import com.hx.redisson.manager.RedisManager;
+import com.hx.redisson.redis.service.RedisService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.stereotype.Service;
+
+import java.util.concurrent.TimeUnit;
+
+@Service
+public class RedisServiceImpl implements RedisService {
+
+
+    @Autowired
+    private RedisTemplate redisTemplate;
+
+    /**
+     * 鏅�氱紦瀛樿幏鍙�
+     *
+     * @param key 閿�
+     * @return 鍊�
+     */
+    @Override
+    public Object get(String key) {
+        return key == null ? null : redisTemplate.opsForValue().get(key);
+    }
+
+    /**
+     * 鏅�氱紦瀛樻斁鍏ュ苟璁剧疆鏃堕棿
+     *
+     * @param key   閿�
+     * @param value 鍊�
+     * @param time  鏃堕棿(绉�) time瑕佸ぇ浜�0 濡傛灉time灏忎簬绛変簬0 灏嗚缃棤闄愭湡
+     * @return true鎴愬姛 false 澶辫触
+     */
+    @Override
+    public boolean set(String key, Object value, long time) {
+        try {
+            if (time > 0) {
+                redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
+            } else {
+                set(key, value);
+            }
+            return true;
+        } catch (Exception e) {
+            e.printStackTrace();
+            return false;
+        }
+    }
+
+    @Override
+    public boolean set(String key, Object value) {
+        try {
+            redisTemplate.opsForValue().set(key, value);
+            return true;
+        } catch (Exception e) {
+            e.printStackTrace();
+            return false;
+        }
+    }
+}

--
Gitblit v1.8.0