From 43b2559f6bc7658919f56bfe629d7a30ecdc4afc Mon Sep 17 00:00:00 2001 From: tangwei Date: Thu, 2 Jul 2026 15:13:04 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20redis=E9=80=BB=E8=BE=91=E5=BC=95?= =?UTF-8?q?=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/yfd/platform/PlatformApplication.java | 2 +- .../com/yfd/platform/config/RedisConfig.java | 48 ++++ .../com/yfd/platform/config/WebConfig.java | 24 +- .../yfd/platform/utils/RedisCacheUtil.java | 227 ++++++++++++++++++ 4 files changed, 298 insertions(+), 3 deletions(-) create mode 100644 backend/src/main/java/com/yfd/platform/config/RedisConfig.java create mode 100644 backend/src/main/java/com/yfd/platform/utils/RedisCacheUtil.java diff --git a/backend/src/main/java/com/yfd/platform/PlatformApplication.java b/backend/src/main/java/com/yfd/platform/PlatformApplication.java index 94785cf1..9d7f55ed 100644 --- a/backend/src/main/java/com/yfd/platform/PlatformApplication.java +++ b/backend/src/main/java/com/yfd/platform/PlatformApplication.java @@ -23,7 +23,7 @@ import org.springframework.web.bind.annotation.RestController; @ServletComponentScan("com.yfd.platform.config") @MapperScan(basePackages = {"com.yfd.platform.*.mapper","com.yfd.platform.*.*.mapper", "com.yfd.platform.common"}) //@ComponentScan("com.zny.dec") -@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class, DataRedisAutoConfiguration.class}) +@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class}) //@SpringBootApplication @Import({DynamicDataSourceConfig.class}) @EnableCaching diff --git a/backend/src/main/java/com/yfd/platform/config/RedisConfig.java b/backend/src/main/java/com/yfd/platform/config/RedisConfig.java new file mode 100644 index 00000000..afc69288 --- /dev/null +++ b/backend/src/main/java/com/yfd/platform/config/RedisConfig.java @@ -0,0 +1,48 @@ +package com.yfd.platform.config; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +@Configuration +public class RedisConfig { + + @Bean + public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) { + RedisTemplate redisTemplate = new RedisTemplate<>(); + redisTemplate.setConnectionFactory(redisConnectionFactory); + + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); + objectMapper.registerModule(new JavaTimeModule()); + objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + + Jackson2JsonRedisSerializer jsonSerializer = new Jackson2JsonRedisSerializer<>(objectMapper, Object.class); + + StringRedisSerializer stringSerializer = new StringRedisSerializer(); + + redisTemplate.setKeySerializer(stringSerializer); + redisTemplate.setHashKeySerializer(stringSerializer); + redisTemplate.setValueSerializer(jsonSerializer); + redisTemplate.setHashValueSerializer(jsonSerializer); + + redisTemplate.afterPropertiesSet(); + return redisTemplate; + } + + @Bean + public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) { + StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(); + stringRedisTemplate.setConnectionFactory(redisConnectionFactory); + return stringRedisTemplate; + } +} \ No newline at end of file diff --git a/backend/src/main/java/com/yfd/platform/config/WebConfig.java b/backend/src/main/java/com/yfd/platform/config/WebConfig.java index 8356277c..1539d29b 100644 --- a/backend/src/main/java/com/yfd/platform/config/WebConfig.java +++ b/backend/src/main/java/com/yfd/platform/config/WebConfig.java @@ -7,12 +7,15 @@ import jakarta.annotation.Resource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import java.util.concurrent.TimeUnit; + @Configuration public class WebConfig implements WebMvcConfigurer { @Resource @@ -21,11 +24,28 @@ public class WebConfig implements WebMvcConfigurer { @Value("${app.zip-import.temp-dir}") private String platformPath; - + @Resource + private StringRedisTemplate stringRedisTemplate; @Bean public Cache loginuserCache() { - return CacheUtil.newLRUCache(200);//用户登录缓存数 缺省200 + return CacheUtil.newLRUCache(200); + } + + public void putLoginCache(String key, String value, long timeoutSeconds) { + stringRedisTemplate.opsForValue().set(key, value, timeoutSeconds, TimeUnit.SECONDS); + } + + public void putLoginCache(String key, String value) { + stringRedisTemplate.opsForValue().set(key, value); + } + + public String getLoginCache(String key) { + return stringRedisTemplate.opsForValue().get(key); + } + + public void removeLoginCache(String key) { + stringRedisTemplate.delete(key); } @Bean diff --git a/backend/src/main/java/com/yfd/platform/utils/RedisCacheUtil.java b/backend/src/main/java/com/yfd/platform/utils/RedisCacheUtil.java new file mode 100644 index 00000000..2640763f --- /dev/null +++ b/backend/src/main/java/com/yfd/platform/utils/RedisCacheUtil.java @@ -0,0 +1,227 @@ +package com.yfd.platform.utils; + +import jakarta.annotation.Resource; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.stereotype.Component; + +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +@Component +public class RedisCacheUtil { + + @Resource + private RedisTemplate redisTemplate; + + @Resource + private StringRedisTemplate stringRedisTemplate; + + public void set(String key, Object value) { + redisTemplate.opsForValue().set(key, value); + } + + public void set(String key, Object value, long timeout, TimeUnit unit) { + redisTemplate.opsForValue().set(key, value, timeout, unit); + } + + public void set(String key, Object value, long timeoutSeconds) { + redisTemplate.opsForValue().set(key, value, timeoutSeconds, TimeUnit.SECONDS); + } + + @SuppressWarnings("unchecked") + public T get(String key, Class clazz) { + Object value = redisTemplate.opsForValue().get(key); + if (value == null) { + return null; + } + return (T) value; + } + + public Object get(String key) { + return redisTemplate.opsForValue().get(key); + } + + public String getString(String key) { + return stringRedisTemplate.opsForValue().get(key); + } + + public void setString(String key, String value) { + stringRedisTemplate.opsForValue().set(key, value); + } + + public void setString(String key, String value, long timeoutSeconds) { + stringRedisTemplate.opsForValue().set(key, value, timeoutSeconds, TimeUnit.SECONDS); + } + + public void setString(String key, String value, long timeout, TimeUnit unit) { + stringRedisTemplate.opsForValue().set(key, value, timeout, unit); + } + + public Boolean delete(String key) { + return redisTemplate.delete(key); + } + + public Long delete(Collection keys) { + return redisTemplate.delete(keys); + } + + public Boolean exists(String key) { + return redisTemplate.hasKey(key); + } + + public Boolean expire(String key, long timeout, TimeUnit unit) { + return redisTemplate.expire(key, timeout, unit); + } + + public Boolean expire(String key, long timeoutSeconds) { + return redisTemplate.expire(key, timeoutSeconds, TimeUnit.SECONDS); + } + + public Long getExpire(String key) { + return redisTemplate.getExpire(key, TimeUnit.SECONDS); + } + + public Long increment(String key, long delta) { + return redisTemplate.opsForValue().increment(key, delta); + } + + public Long increment(String key) { + return redisTemplate.opsForValue().increment(key, 1); + } + + public Long decrement(String key, long delta) { + return redisTemplate.opsForValue().decrement(key, delta); + } + + public Long decrement(String key) { + return redisTemplate.opsForValue().decrement(key, 1); + } + + public void hSet(String key, String hashKey, Object value) { + redisTemplate.opsForHash().put(key, hashKey, value); + } + + @SuppressWarnings("unchecked") + public T hGet(String key, String hashKey, Class clazz) { + Object value = redisTemplate.opsForHash().get(key, hashKey); + if (value == null) { + return null; + } + return (T) value; + } + + public Object hGet(String key, String hashKey) { + return redisTemplate.opsForHash().get(key, hashKey); + } + + public Map hGetAll(String key) { + return redisTemplate.opsForHash().entries(key); + } + + public void hSetAll(String key, Map map) { + redisTemplate.opsForHash().putAll(key, map); + } + + public void hDelete(String key, Object... hashKeys) { + redisTemplate.opsForHash().delete(key, hashKeys); + } + + public Boolean hExists(String key, String hashKey) { + return redisTemplate.opsForHash().hasKey(key, hashKey); + } + + public Long hSize(String key) { + return redisTemplate.opsForHash().size(key); + } + + public Set hKeys(String key) { + return redisTemplate.opsForHash().keys(key); + } + + public List hValues(String key) { + return redisTemplate.opsForHash().values(key); + } + + public Long lPush(String key, Object... values) { + return redisTemplate.opsForList().leftPushAll(key, values); + } + + public Long rPush(String key, Object... values) { + return redisTemplate.opsForList().rightPushAll(key, values); + } + + public Object lPop(String key) { + return redisTemplate.opsForList().leftPop(key); + } + + public Object rPop(String key) { + return redisTemplate.opsForList().rightPop(key); + } + + public List lRange(String key, long start, long end) { + return redisTemplate.opsForList().range(key, start, end); + } + + public Long lSize(String key) { + return redisTemplate.opsForList().size(key); + } + + public void lTrim(String key, long start, long end) { + redisTemplate.opsForList().trim(key, start, end); + } + + public Long sAdd(String key, Object... values) { + return redisTemplate.opsForSet().add(key, values); + } + + public Set sMembers(String key) { + return redisTemplate.opsForSet().members(key); + } + + public Boolean sIsMember(String key, Object value) { + return redisTemplate.opsForSet().isMember(key, value); + } + + public Long sRemove(String key, Object... values) { + return redisTemplate.opsForSet().remove(key, values); + } + + public Long sSize(String key) { + return redisTemplate.opsForSet().size(key); + } + + public boolean zAdd(String key, Object value, double score) { + return redisTemplate.opsForZSet().add(key, value, score); + } + + public Set zRange(String key, long start, long end) { + return redisTemplate.opsForZSet().range(key, start, end); + } + + public Double zScore(String key, Object value) { + return redisTemplate.opsForZSet().score(key, value); + } + + public Long zRemove(String key, Object... values) { + return redisTemplate.opsForZSet().remove(key, values); + } + + public Long zSize(String key) { + return redisTemplate.opsForZSet().size(key); + } + + public Set keys(String pattern) { + return redisTemplate.keys(pattern); + } + +// public void flushDb() { +// redisTemplate.execute((connection) -> { +// connection.flushDb(); +// return null; +// }); +// } +} \ No newline at end of file