From 32f68276fb1f609c43c8fe173fc8db75b7ac0909 Mon Sep 17 00:00:00 2001 From: cengy Date: Fri, 11 Apr 2025 20:42:39 +0800 Subject: [PATCH] =?UTF-8?q?feat(redis):=20=E6=96=B0=E5=A2=9E=20Redis=20?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E5=B7=A5=E5=85=B7=E7=B1=BB=E5=B9=B6=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2=E7=8E=B0=E6=9C=89=E7=BC=93=E5=AD=98=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 GRedisCache 类,提供丰富的 Redis 操作方法 - 在 PlatformServiceImpl 中替换原有 RedisCache 为 GRedisCache- 添加 comp-redis 模块的 Maven 依赖 - 更新父项目 pom.xml,包含新的模块依赖 --- ff-bean/comp-redis/pom.xml | 81 ++++++ .../main/java/com/ff/redis/GRedisCache.java | 243 ++++++++++++++++++ ff-game/pom.xml | 4 + .../service/impl/PlatformServiceImpl.java | 10 +- pom.xml | 13 + 5 files changed, 347 insertions(+), 4 deletions(-) create mode 100644 ff-bean/comp-redis/pom.xml create mode 100644 ff-bean/comp-redis/src/main/java/com/ff/redis/GRedisCache.java diff --git a/ff-bean/comp-redis/pom.xml b/ff-bean/comp-redis/pom.xml new file mode 100644 index 0000000..477d53e --- /dev/null +++ b/ff-bean/comp-redis/pom.xml @@ -0,0 +1,81 @@ + + + 4.0.0 + + ff + com.ff + 0.0.1 + + + comp-redis + 0.0.1 + comp-redis + jar + + + + + + + org.projectlombok + lombok + provided + + + + org.springframework.boot + spring-boot-starter-web + + + + + org.springframework.boot + spring-boot-starter-validation + + + + + org.apache.commons + commons-lang3 + + + + + com.alibaba.fastjson2 + fastjson2 + + + + commons-io + commons-io + + + + org.springframework.boot + spring-boot-starter-data-redis + + + + + + + org.springframework.boot + spring-boot-maven-plugin + 2.5.15 + + true + + + + + repackage + + + + + + comp-redis + + + diff --git a/ff-bean/comp-redis/src/main/java/com/ff/redis/GRedisCache.java b/ff-bean/comp-redis/src/main/java/com/ff/redis/GRedisCache.java new file mode 100644 index 0000000..a30b333 --- /dev/null +++ b/ff-bean/comp-redis/src/main/java/com/ff/redis/GRedisCache.java @@ -0,0 +1,243 @@ +package com.ff.redis; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.BoundSetOperations; +import org.springframework.data.redis.core.HashOperations; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.ValueOperations; +import org.springframework.stereotype.Component; + +import java.util.*; +import java.util.concurrent.TimeUnit; + +/** + * spring redis 工具类 + * + * @author ff + **/ +@SuppressWarnings(value = {"unchecked", "rawtypes"}) +@Component +public class GRedisCache { + @Autowired + public RedisTemplate redisTemplate; + + /** + * 缓存基本的对象,Integer、String、实体类等 + * + * @param key 缓存的键值 + * @param value 缓存的值 + */ + public void setCacheObject(final String key, final T value) { + redisTemplate.opsForValue().set(key, value); + } + + /** + * 缓存基本的对象,Integer、String、实体类等 + * + * @param key 缓存的键值 + * @param value 缓存的值 + * @param timeout 时间 + * @param timeUnit 时间颗粒度 + */ + public void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit) { + redisTemplate.opsForValue().set(key, value, timeout, timeUnit); + } + + /** + * 设置有效时间 + * + * @param key Redis键 + * @param timeout 超时时间 + * @return true=设置成功;false=设置失败 + */ + public boolean expire(final String key, final long timeout) { + return expire(key, timeout, TimeUnit.SECONDS); + } + + /** + * 设置有效时间 + * + * @param key Redis键 + * @param timeout 超时时间 + * @param unit 时间单位 + * @return true=设置成功;false=设置失败 + */ + public boolean expire(final String key, final long timeout, final TimeUnit unit) { + return redisTemplate.expire(key, timeout, unit); + } + + /** + * 获取有效时间 + * + * @param key Redis键 + * @return 有效时间 + */ + public long getExpire(final String key) { + return redisTemplate.getExpire(key); + } + + /** + * 判断 key是否存在 + * + * @param key 键 + * @return true 存在 false不存在 + */ + public Boolean hasKey(String key) { + return redisTemplate.hasKey(key); + } + + /** + * 获得缓存的基本对象。 + * + * @param key 缓存键值 + * @return 缓存键值对应的数据 + */ + public T getCacheObject(final String key) { + ValueOperations operation = redisTemplate.opsForValue(); + return operation.get(key); + } + + /** + * 删除单个对象 + * + * @param key + */ + public boolean deleteObject(final String key) { + return redisTemplate.delete(key); + } + + /** + * 删除集合对象 + * + * @param collection 多个对象 + * @return + */ + public boolean deleteObject(final Collection collection) { + return redisTemplate.delete(collection) > 0; + } + + /** + * 缓存List数据 + * + * @param key 缓存的键值 + * @param dataList 待缓存的List数据 + * @return 缓存的对象 + */ + public long setCacheList(final String key, final List dataList) { + Long count = redisTemplate.opsForList().rightPushAll(key, dataList); + return count == null ? 0 : count; + } + + /** + * 获得缓存的list对象 + * + * @param key 缓存的键值 + * @return 缓存键值对应的数据 + */ + public List getCacheList(final String key) { + return redisTemplate.opsForList().range(key, 0, -1); + } + + /** + * 缓存Set + * + * @param key 缓存键值 + * @param dataSet 缓存的数据 + * @return 缓存数据的对象 + */ + public BoundSetOperations setCacheSet(final String key, final Set dataSet) { + BoundSetOperations setOperation = redisTemplate.boundSetOps(key); + Iterator it = dataSet.iterator(); + while (it.hasNext()) { + setOperation.add(it.next()); + } + return setOperation; + } + + /** + * 获得缓存的set + * + * @param key + * @return + */ + public Set getCacheSet(final String key) { + return redisTemplate.opsForSet().members(key); + } + + /** + * 缓存Map + * + * @param key + * @param dataMap + */ + public void setCacheMap(final String key, final Map dataMap) { + if (dataMap != null) { + redisTemplate.opsForHash().putAll(key, dataMap); + } + } + + /** + * 获得缓存的Map + * + * @param key + * @return + */ + public Map getCacheMap(final String key) { + return redisTemplate.opsForHash().entries(key); + } + + /** + * 往Hash中存入数据 + * + * @param key Redis键 + * @param hKey Hash键 + * @param value 值 + */ + public void setCacheMapValue(final String key, final String hKey, final T value) { + redisTemplate.opsForHash().put(key, hKey, value); + } + + /** + * 获取Hash中的数据 + * + * @param key Redis键 + * @param hKey Hash键 + * @return Hash中的对象 + */ + public T getCacheMapValue(final String key, final String hKey) { + HashOperations opsForHash = redisTemplate.opsForHash(); + return opsForHash.get(key, hKey); + } + + /** + * 获取多个Hash中的数据 + * + * @param key Redis键 + * @param hKeys Hash键集合 + * @return Hash对象集合 + */ + public List getMultiCacheMapValue(final String key, final Collection hKeys) { + return redisTemplate.opsForHash().multiGet(key, hKeys); + } + + /** + * 删除Hash中的某条数据 + * + * @param key Redis键 + * @param hKey Hash键 + * @return 是否成功 + */ + public boolean deleteCacheMapValue(final String key, final String hKey) { + return redisTemplate.opsForHash().delete(key, hKey) > 0; + } + + /** + * 获得缓存的基本对象列表 + * + * @param pattern 字符串前缀 + * @return 对象列表 + */ + public Collection keys(final String pattern) { + return redisTemplate.keys(pattern); + } +} diff --git a/ff-game/pom.xml b/ff-game/pom.xml index 253cdea..a6b57b9 100644 --- a/ff-game/pom.xml +++ b/ff-game/pom.xml @@ -16,6 +16,10 @@ + + com.ff + comp-redis + com.ff ff-domain diff --git a/ff-game/src/main/java/com/ff/game/service/impl/PlatformServiceImpl.java b/ff-game/src/main/java/com/ff/game/service/impl/PlatformServiceImpl.java index 6dd8f6b..2b6b93c 100644 --- a/ff-game/src/main/java/com/ff/game/service/impl/PlatformServiceImpl.java +++ b/ff-game/src/main/java/com/ff/game/service/impl/PlatformServiceImpl.java @@ -1,11 +1,11 @@ package com.ff.game.service.impl; import com.ff.base.constant.CacheConstants; -import com.ff.base.core.redis.RedisCache; import com.ff.base.datasource.DynamicDataSourceContextHolder; import com.ff.game.domain.Platform; import com.ff.game.mapper.PlatformMapper; import com.ff.game.service.IPlatformService; +import com.ff.redis.GRedisCache; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -21,8 +21,10 @@ public class PlatformServiceImpl implements IPlatformService { @Autowired PlatformMapper platformMapper; + @Autowired - RedisCache redisCache; + GRedisCache gRedisCache; + @Override public List selectList(Platform platform) { @@ -63,14 +65,14 @@ public class PlatformServiceImpl implements IPlatformService { DynamicDataSourceContextHolder.setDataSourceType(key.toString()); List list = selectList(new Platform()); for (Platform pp : list) { - redisCache.setCacheObject(getCacheKey(pp.getPlatformCode()), pp); + gRedisCache.setCacheObject(getCacheKey(pp.getPlatformCode()), pp); } } } @Override public Platform get(String platformCode) { - return redisCache.getCacheObject(getCacheKey(platformCode)); + return gRedisCache.getCacheObject(getCacheKey(platformCode)); } private String getCacheKey(String configKey) { diff --git a/pom.xml b/pom.xml index 8bf1d59..b17b6dc 100644 --- a/pom.xml +++ b/pom.xml @@ -211,6 +211,18 @@ ff-domain ${ff.version} + + + com.ff + ff-service + ${ff.version} + + + + com.ff + comp-redis + 0.0.1 + @@ -218,6 +230,7 @@ ff-game ff-fetcher ff-bean/ff-domain + ff-bean/comp-redis pom