Переглянути джерело

feat: 添加redis相关配置与依赖

yangliu 3 місяців тому
батько
коміт
6319a0ea76

+ 0 - 60
redis-spring-boot-starter/pom.xml

@@ -1,60 +0,0 @@
-<?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>hfln-framework-design-starter</artifactId>
-        <groupId>cn.hfln.framework</groupId>
-        <version>1.0.0-SNAPSHOT</version>
-    </parent>
-    <modelVersion>4.0.0</modelVersion>
-    <artifactId>redis-spring-boot-starter</artifactId>
-    <name>redis-spring-boot-starter</name>
-    <description>通用 Redis Starter,自动装配,支持常用 Redis 操作</description>
-    <dependencies>
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-data-redis</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-autoconfigure</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>org.springframework</groupId>
-            <artifactId>spring-context</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>org.springframework</groupId>
-            <artifactId>spring-core</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>org.apache.commons</groupId>
-            <artifactId>commons-lang3</artifactId>
-        </dependency>
-        <!-- Jackson 依赖 -->
-        <dependency>
-            <groupId>com.fasterxml.jackson.core</groupId>
-            <artifactId>jackson-databind</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>com.fasterxml.jackson.core</groupId>
-            <artifactId>jackson-core</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>com.fasterxml.jackson.core</groupId>
-            <artifactId>jackson-annotations</artifactId>
-        </dependency>
-        <!-- 测试依赖 -->
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-test</artifactId>
-            <scope>test</scope>
-        </dependency>
-        <dependency>
-            <groupId>org.junit.jupiter</groupId>
-            <artifactId>junit-jupiter</artifactId>
-            <scope>test</scope>
-        </dependency>
-    </dependencies>
-</project> 

+ 0 - 27
redis-spring-boot-starter/src/main/java/cn/hfln/framework/redis/autoconfigure/AutoRedisConfiguration.java

@@ -1,27 +0,0 @@
-package cn.hfln.framework.redis.autoconfigure;
-
-import cn.hfln.framework.redis.util.RedisUtil;
-import cn.hfln.framework.redis.properties.RedisProperties;
-import org.springframework.boot.autoconfigure.AutoConfigureAfter;
-import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
-import org.springframework.boot.context.properties.EnableConfigurationProperties;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.data.redis.core.RedisTemplate;
-
-@Configuration
-@AutoConfigureAfter(name = "org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration")
-@EnableConfigurationProperties(RedisProperties.class)
-public class AutoRedisConfiguration {
-
-    public AutoRedisConfiguration() {
-        System.out.println("【RedisAutoConfiguration】构造方法被调用");
-    }
-
-    @Bean
-    @ConditionalOnBean(RedisTemplate.class)
-    public RedisUtil redisUtil(RedisTemplate<String, Object> redisTemplate, RedisProperties redisProperties) {
-        System.out.println("【RedisAutoConfiguration】redisUtil Bean 创建");
-        return new RedisUtil(redisTemplate, redisProperties);
-    }
-}

+ 0 - 28
redis-spring-boot-starter/src/main/java/cn/hfln/framework/redis/properties/RedisProperties.java

@@ -1,28 +0,0 @@
-package cn.hfln.framework.redis.properties;
-
-import org.springframework.boot.context.properties.ConfigurationProperties;
-
-@ConfigurationProperties(prefix = "hfln.redis")
-public class RedisProperties {
-    /**
-     * key 前缀
-     */
-    private String keyPrefix = "";
-    /**
-     * 默认超时时间(秒)
-     */
-    private long defaultTimeout = 3600;
-
-    public String getKeyPrefix() {
-        return keyPrefix;
-    }
-    public void setKeyPrefix(String keyPrefix) {
-        this.keyPrefix = keyPrefix;
-    }
-    public long getDefaultTimeout() {
-        return defaultTimeout;
-    }
-    public void setDefaultTimeout(long defaultTimeout) {
-        this.defaultTimeout = defaultTimeout;
-    }
-} 

+ 0 - 114
redis-spring-boot-starter/src/main/java/cn/hfln/framework/redis/util/RedisUtil.java

@@ -1,114 +0,0 @@
-package cn.hfln.framework.redis.util;
-
-import cn.hfln.framework.redis.properties.RedisProperties;
-import org.springframework.data.redis.core.RedisTemplate;
-
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.TimeUnit;
-
-/**
- * Redis 工具类,封装常用操作,支持 key 前缀、默认超时、分布式锁等
- */
-public class RedisUtil {
-    private final RedisTemplate<String, Object> redisTemplate;
-    private final RedisProperties redisProperties;
-
-    public RedisUtil(RedisTemplate<String, Object> redisTemplate, RedisProperties redisProperties) {
-        System.out.println("【RedisUtil】构造方法被调用");
-        this.redisTemplate = redisTemplate;
-        this.redisProperties = redisProperties;
-    }
-
-    private String wrapKey(String key) {
-        String prefix = redisProperties != null ? redisProperties.getKeyPrefix() : "";
-        return prefix + key;
-    }
-
-    // ============================= String ============================
-    public void set(String key, Object value) {
-        set(key, value, redisProperties != null ? redisProperties.getDefaultTimeout() : 3600, TimeUnit.SECONDS);
-    }
-    public void set(String key, Object value, long timeout, TimeUnit unit) {
-        redisTemplate.opsForValue().set(wrapKey(key), value, timeout, unit);
-    }
-    public Object get(String key) {
-        return redisTemplate.opsForValue().get(wrapKey(key));
-    }
-    public boolean del(String key) {
-        return Boolean.TRUE.equals(redisTemplate.delete(wrapKey(key)));
-    }
-
-    // ============================= Hash =============================
-    public void hSet(String key, String field, Object value) {
-        redisTemplate.opsForHash().put(wrapKey(key), field, value);
-    }
-    public Object hGet(String key, String field) {
-        return redisTemplate.opsForHash().get(wrapKey(key), field);
-    }
-    public Map<Object, Object> hGetAll(String key) {
-        return redisTemplate.opsForHash().entries(wrapKey(key));
-    }
-    public void hDel(String key, String... fields) {
-        redisTemplate.opsForHash().delete(wrapKey(key), (Object[]) fields);
-    }
-
-    // ============================= List =============================
-    public void lPush(String key, Object value) {
-        redisTemplate.opsForList().leftPush(wrapKey(key), value);
-    }
-    public Object lPop(String key) {
-        return redisTemplate.opsForList().leftPop(wrapKey(key));
-    }
-    public List<Object> lRange(String key, long start, long end) {
-        return redisTemplate.opsForList().range(wrapKey(key), start, end);
-    }
-
-    // ============================= Set ==============================
-    public void sAdd(String key, Object... values) {
-        redisTemplate.opsForSet().add(wrapKey(key), values);
-    }
-    public Set<Object> sMembers(String key) {
-        return redisTemplate.opsForSet().members(wrapKey(key));
-    }
-    public boolean sIsMember(String key, Object value) {
-        return Boolean.TRUE.equals(redisTemplate.opsForSet().isMember(wrapKey(key), value));
-    }
-
-    // ============================= ZSet =============================
-    public void zAdd(String key, Object value, double score) {
-        redisTemplate.opsForZSet().add(wrapKey(key), value, score);
-    }
-    public Set<Object> zRange(String key, long start, long end) {
-        return redisTemplate.opsForZSet().range(wrapKey(key), start, end);
-    }
-
-    // ============================= 通用 =============================
-    public boolean expire(String key, long timeout, TimeUnit unit) {
-        return Boolean.TRUE.equals(redisTemplate.expire(wrapKey(key), timeout, unit));
-    }
-    public long getExpire(String key, TimeUnit unit) {
-        return redisTemplate.getExpire(wrapKey(key), unit);
-    }
-    public boolean hasKey(String key) {
-        return Boolean.TRUE.equals(redisTemplate.hasKey(wrapKey(key)));
-    }
-
-    // ============================= 原子操作 =========================
-    public Long incr(String key) {
-        return redisTemplate.opsForValue().increment(wrapKey(key));
-    }
-    public Long incrBy(String key, long delta) {
-        return redisTemplate.opsForValue().increment(wrapKey(key), delta);
-    }
-
-    // ============================= 分布式锁(简单实现) =============
-    public boolean tryLock(String key, String value, long timeout, TimeUnit unit) {
-        Boolean success = redisTemplate.opsForValue().setIfAbsent(wrapKey(key), value, timeout, unit);
-        return Boolean.TRUE.equals(success);
-    }
-    public void unlock(String key) {
-        del(key);
-    }
-} 

+ 0 - 2
redis-spring-boot-starter/src/main/resources/META-INF/spring.factories

@@ -1,2 +0,0 @@
-org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
-cn.hfln.framework.redis.autoconfigure.AutoRedisConfiguration

+ 0 - 8
redis-spring-boot-starter/src/test/java/cn/hfln/framework/redis/RedisTestApplication.java

@@ -1,8 +0,0 @@
-package cn.hfln.framework.redis;
-
-import org.springframework.boot.autoconfigure.SpringBootApplication;
-
-@SpringBootApplication
-public class RedisTestApplication {
-    // 仅用于测试上下文启动,无需实现内容
-} 

+ 0 - 23
redis-spring-boot-starter/src/test/java/cn/hfln/framework/redis/RedisUtilIntegrationTest.java

@@ -1,23 +0,0 @@
-package cn.hfln.framework.redis;
-
-import cn.hfln.framework.redis.util.RedisUtil;
-import org.junit.jupiter.api.Test;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.test.context.SpringBootTest;
-
-import static org.junit.jupiter.api.Assertions.*;
-
-@SpringBootTest(classes = RedisTestApplication.class)
-class RedisUtilIntegrationTest {
-
-    @Autowired
-    private RedisUtil redisUtil;
-
-    @Test
-    void testSetGetDel() {
-        redisUtil.set("testKey", "testValue");
-        Object value = redisUtil.get("testKey");
-        assertEquals("testValue", value);
-        redisUtil.del("testKey");
-    }
-}