feat(redis): 新增 Redis 缓存工具类并替换现有缓存实现

- 新增 GRedisCache 类,提供丰富的 Redis 操作方法
- 在 PlatformServiceImpl 中替换原有 RedisCache 为 GRedisCache- 添加 comp-redis 模块的 Maven 依赖
- 更新父项目 pom.xml,包含新的模块依赖
main-pp
liaoyong 2025-04-11 20:42:39 +08:00
parent c08dab29f3
commit 32f68276fb
5 changed files with 347 additions and 4 deletions

View File

@ -0,0 +1,81 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>ff</artifactId>
<groupId>com.ff</groupId>
<version>0.0.1</version>
</parent>
<artifactId>comp-redis</artifactId>
<version>0.0.1</version>
<name>comp-redis</name>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 自定义验证注解 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<!--常用工具类 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!-- 阿里JSON解析器 -->
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
</dependency>
<!-- io常用工具类 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.5.15</version>
<configuration>
<fork>true</fork> <!-- 如果没有该配置devtools不会生效 -->
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<finalName>comp-redis</finalName>
</build>
</project>

View File

@ -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;
/**
* IntegerString
*
* @param key
* @param value
*/
public <T> void setCacheObject(final String key, final T value) {
redisTemplate.opsForValue().set(key, value);
}
/**
* IntegerString
*
* @param key
* @param value
* @param timeout
* @param timeUnit
*/
public <T> 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> T getCacheObject(final String key) {
ValueOperations<String, T> 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 <T> long setCacheList(final String key, final List<T> dataList) {
Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
return count == null ? 0 : count;
}
/**
* list
*
* @param key
* @return
*/
public <T> List<T> getCacheList(final String key) {
return redisTemplate.opsForList().range(key, 0, -1);
}
/**
* Set
*
* @param key
* @param dataSet
* @return
*/
public <T> BoundSetOperations<String, T> setCacheSet(final String key, final Set<T> dataSet) {
BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);
Iterator<T> it = dataSet.iterator();
while (it.hasNext()) {
setOperation.add(it.next());
}
return setOperation;
}
/**
* set
*
* @param key
* @return
*/
public <T> Set<T> getCacheSet(final String key) {
return redisTemplate.opsForSet().members(key);
}
/**
* Map
*
* @param key
* @param dataMap
*/
public <T> void setCacheMap(final String key, final Map<String, T> dataMap) {
if (dataMap != null) {
redisTemplate.opsForHash().putAll(key, dataMap);
}
}
/**
* Map
*
* @param key
* @return
*/
public <T> Map<String, T> getCacheMap(final String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* Hash
*
* @param key Redis
* @param hKey Hash
* @param value
*/
public <T> 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> T getCacheMapValue(final String key, final String hKey) {
HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash();
return opsForHash.get(key, hKey);
}
/**
* Hash
*
* @param key Redis
* @param hKeys Hash
* @return Hash
*/
public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> 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<String> keys(final String pattern) {
return redisTemplate.keys(pattern);
}
}

View File

@ -16,6 +16,10 @@
<dependencies>
<dependency>
<groupId>com.ff</groupId>
<artifactId>comp-redis</artifactId>
</dependency>
<dependency>
<groupId>com.ff</groupId>
<artifactId>ff-domain</artifactId>

View File

@ -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<Platform> selectList(Platform platform) {
@ -63,14 +65,14 @@ public class PlatformServiceImpl implements IPlatformService {
DynamicDataSourceContextHolder.setDataSourceType(key.toString());
List<Platform> 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) {

13
pom.xml
View File

@ -211,6 +211,18 @@
<artifactId>ff-domain</artifactId>
<version>${ff.version}</version>
</dependency>
<dependency>
<groupId>com.ff</groupId>
<artifactId>ff-service</artifactId>
<version>${ff.version}</version>
</dependency>
<dependency>
<groupId>com.ff</groupId>
<artifactId>comp-redis</artifactId>
<version>0.0.1</version>
</dependency>
</dependencies>
</dependencyManagement>
@ -218,6 +230,7 @@
<module>ff-game</module>
<module>ff-fetcher</module>
<module>ff-bean/ff-domain</module>
<module>ff-bean/comp-redis</module>
</modules>
<packaging>pom</packaging>