Kaynağa Gözat

添加 redis 模块

reghao 3 yıl önce
ebeveyn
işleme
eb73e09829

+ 0 - 1
jdk/pom.xml

@@ -15,5 +15,4 @@
         <maven.compiler.source>11</maven.compiler.source>
         <maven.compiler.target>11</maven.compiler.target>
     </properties>
-
 </project>

+ 6 - 1
pom.xml

@@ -11,6 +11,7 @@
     <modules>
         <module>jdk</module>
         <module>tool</module>
+        <module>redis</module>
     </modules>
 
     <properties>
@@ -27,9 +28,13 @@
     </dependencies>
 
     <distributionManagement>
-        <repository>
+         <repository>
             <id>local-maven</id>
             <url>http://nexus.alpha.iquizoo.com/repository/maven-local/</url>
         </repository>
+        <!--<repository>
+            <id>tnb-maven-local</id>
+            <url>http://nexus.reghao.cn/repository/tnb-maven-local/</url>
+        </repository>-->
     </distributionManagement>
 </project>

+ 26 - 0
redis/pom.xml

@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>jutil</artifactId>
+        <groupId>cn.reghao.jutil</groupId>
+        <version>1.0.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>redis</artifactId>
+
+    <properties>
+        <maven.compiler.source>11</maven.compiler.source>
+        <maven.compiler.target>11</maven.compiler.target>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>io.lettuce</groupId>
+            <artifactId>lettuce-core</artifactId>
+            <version>5.3.6.RELEASE</version>
+        </dependency>
+    </dependencies>
+</project>

+ 52 - 0
redis/src/main/java/cn/reghao/jutil/redis/ObjectSerializeCodec.java

@@ -0,0 +1,52 @@
+package cn.reghao.jutil.redis;
+
+import io.lettuce.core.codec.RedisCodec;
+
+import java.io.*;
+import java.nio.ByteBuffer;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+
+/**
+ * 存储对象到 Redis 时使用的序列化编码器
+ *
+ * @author reghao
+ * @date 2022-02-25 17:49:51
+ */
+public class ObjectSerializeCodec<T> implements RedisCodec<String, T> {
+    private final Charset charset = StandardCharsets.UTF_8;
+
+    @Override
+    public String decodeKey(ByteBuffer bytes) {
+        return charset.decode(bytes).toString();
+    }
+
+    @Override
+    public T decodeValue(ByteBuffer bytes) {
+        try {
+            byte[] array = new byte[bytes.remaining()];
+            bytes.get(array);
+            ObjectInputStream is = new ObjectInputStream(new ByteArrayInputStream(array));
+            return (T)is.readObject();
+        } catch (Exception e) {
+            return null;
+        }
+    }
+
+    @Override
+    public ByteBuffer encodeKey(String key) { // str -> bytes -> byteBuffer
+        return ByteBuffer.wrap(key.getBytes(charset));
+    }
+
+    @Override
+    public ByteBuffer encodeValue(T value) {
+        try {
+            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+            ObjectOutputStream os = new ObjectOutputStream(bytes);
+            os.writeObject(value);
+            return ByteBuffer.wrap(bytes.toByteArray());
+        } catch (IOException e) {
+            return null;
+        }
+    }
+}

+ 29 - 0
redis/src/main/java/cn/reghao/jutil/redis/RedisConfig.java

@@ -0,0 +1,29 @@
+package cn.reghao.jutil.redis;
+
+/**
+ * @author reghao
+ * @date 2022-02-25 17:49:51
+ */
+public class RedisConfig {
+    private final String host;
+    private final String password;
+    private final int db;
+
+    public RedisConfig(String host, String password, int db) {
+        this.host = host;
+        this.password = password;
+        this.db = db;
+    }
+
+    public String getHost() {
+        return host;
+    }
+
+    public String getPassword() {
+        return password;
+    }
+
+    public int getDb() {
+        return db;
+    }
+}

+ 67 - 0
redis/src/main/java/cn/reghao/jutil/redis/RedisManager.java

@@ -0,0 +1,67 @@
+package cn.reghao.jutil.redis;
+
+import cn.reghao.jutil.redis.ds.RedisList;
+import cn.reghao.jutil.redis.ds.RedisSet;
+import cn.reghao.jutil.redis.pubsub.RedisPubSub;
+import io.lettuce.core.RedisClient;
+import io.lettuce.core.RedisURI;
+import io.lettuce.core.api.StatefulRedisConnection;
+import io.lettuce.core.api.sync.RedisCommands;
+import io.lettuce.core.api.sync.RedisListCommands;
+import io.lettuce.core.api.sync.RedisSetCommands;
+
+import java.time.Duration;
+
+/**
+ * Redis 客户端
+ *
+ * @author reghao
+ * @date 2022-02-25 17:49:51
+ */
+public class RedisManager {
+    private final RedisClient redisClient;
+    private final ObjectSerializeCodec<Object> objectSerializeCodec;
+    private RedisSet redisSet;
+    private RedisList redisList;
+    private RedisPubSub redisPubSub;
+
+    public RedisManager(RedisConfig config) {
+        RedisURI uri = RedisURI.Builder
+                .redis(config.getHost())
+                .withPassword(config.getPassword().toCharArray())
+                .withDatabase(config.getDb())
+                .withTimeout(Duration.ofSeconds(60))
+                .build();
+        this.redisClient = RedisClient.create(uri);
+        this.objectSerializeCodec = new ObjectSerializeCodec<>();
+    }
+
+    public RedisSet redisSet() {
+        if (redisSet == null) {
+            StatefulRedisConnection<String, Object> conn = redisClient.connect(objectSerializeCodec);
+            RedisSetCommands<String, Object> setCommands = conn.sync();
+            this.redisSet = new RedisSet(setCommands);
+        }
+        return redisSet;
+    }
+
+    public RedisList redisList() {
+        if (redisList == null) {
+            StatefulRedisConnection<String, Object> conn = redisClient.connect(objectSerializeCodec);
+            RedisListCommands<String, Object> listCommands = conn.sync();
+            this.redisList = new RedisList(listCommands);
+        }
+        return redisList;
+    }
+
+    public RedisPubSub redisPubSub() {
+        if (redisPubSub == null) {
+            this.redisPubSub = new RedisPubSub(redisClient, objectSerializeCodec);
+        }
+        return redisPubSub;
+    }
+
+    public RedisCommands<String, Object> redisCommands() {
+        return redisClient.connect(objectSerializeCodec).sync();
+    }
+}

+ 23 - 0
redis/src/main/java/cn/reghao/jutil/redis/ds/RedisList.java

@@ -0,0 +1,23 @@
+package cn.reghao.jutil.redis.ds;
+
+import io.lettuce.core.api.sync.RedisListCommands;
+
+/**
+ * @author reghao
+ * @date 2022-02-25 17:49:51
+ */
+public class RedisList {
+    private final RedisListCommands<String, Object> listCommands;
+
+    public RedisList(RedisListCommands<String, Object> listCommands) {
+        this.listCommands = listCommands;
+    }
+
+    public void lpush(String key, Object value) {
+        listCommands.lpush(key, value);
+    }
+
+    public Object lpop(String key) {
+        return listCommands.lpop(key);
+    }
+}

+ 49 - 0
redis/src/main/java/cn/reghao/jutil/redis/ds/RedisSet.java

@@ -0,0 +1,49 @@
+package cn.reghao.jutil.redis.ds;
+
+import io.lettuce.core.api.sync.RedisSetCommands;
+
+import java.util.Set;
+
+/**
+ * @author reghao
+ * @date 2022-02-25 17:49:51
+ */
+public class RedisSet {
+    private final RedisSetCommands<String, Object> setCommands;
+
+    public RedisSet(RedisSetCommands<String, Object> setCommands) {
+        this.setCommands = setCommands;
+    }
+
+    public void sadd(String key, Object value) {
+        setCommands.sadd(key, value);
+    }
+
+    public void smove(String srcKey, String destKey, Object value) {
+        setCommands.smove(srcKey, destKey, value);
+    }
+
+    public void sadd(String key, Object... values) {
+        setCommands.sadd(key, values);
+    }
+
+    public Object spop(String key) {
+        return setCommands.spop(key);
+    }
+
+    public Set<Object> spop(String key, long count) {
+        return setCommands.spop(key, count);
+    }
+
+    public Set<Object> smembers(String key) {
+        return setCommands.smembers(key);
+    }
+
+    public boolean sismember(String key, Object value) {
+        return setCommands.sismember(key, value);
+    }
+
+    public long scard(String key) {
+        return setCommands.scard(key);
+    }
+}

+ 30 - 0
redis/src/main/java/cn/reghao/jutil/redis/pubsub/RedisPubSub.java

@@ -0,0 +1,30 @@
+package cn.reghao.jutil.redis.pubsub;
+
+import cn.reghao.jutil.redis.ObjectSerializeCodec;
+import io.lettuce.core.RedisClient;
+import io.lettuce.core.pubsub.RedisPubSubListener;
+import io.lettuce.core.pubsub.StatefulRedisPubSubConnection;
+
+/**
+ * @author reghao
+ * @date 2022-02-25 17:49:51
+ */
+public class RedisPubSub {
+    private final StatefulRedisPubSubConnection<String, Object> subConn;
+    private final StatefulRedisPubSubConnection<String, Object> pubConn;
+
+    public RedisPubSub(RedisClient redisClient, ObjectSerializeCodec<Object> objectSerializeCodec) {
+        this.subConn = redisClient.connectPubSub(objectSerializeCodec);
+        this.pubConn = redisClient.connectPubSub(objectSerializeCodec);
+    }
+
+    public void publish(String channel, Object message) {
+        pubConn.async().publish(channel, message);
+        //pubConn.sync().publish(channel, message);
+    }
+
+    public void subscribe(String channel, RedisPubSubListener<String, Object> listener) {
+        subConn.addListener(listener);
+        subConn.async().subscribe(channel);
+    }
+}