|
|
package com.yohoufo.product.cache;
|
|
|
|
|
|
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
|
|
|
import com.yohoufo.common.cache.CacheClient;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* Created by li.ma on 2019/3/6.
|
|
|
*/
|
|
|
@Service
|
|
|
public class UfoProductCacheService {
|
|
|
@Autowired
|
|
|
private CacheClient cacheClient;
|
|
|
|
|
|
/**
|
|
|
* 单个设置字符串缓存
|
|
|
* @param cacheKeyEnum 缓存的枚举类
|
|
|
* @param value 缓存的值
|
|
|
* @param keyParams 缓存key可变的参数列表 可空
|
|
|
*/
|
|
|
public void setCacheByString(UfoProductCacheKeyEnum cacheKeyEnum, Object value, String... keyParams) {
|
|
|
RedisKeyBuilder redisKeyBuilder = getRedisKeyBuilder(cacheKeyEnum, keyParams);
|
|
|
cacheClient.setEx(redisKeyBuilder, value, cacheKeyEnum.getExpireSecondTime());
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 批量设置字符串缓存 仅适合同一场场景的缓存批量设置
|
|
|
* @param cacheKeyEnum 缓存的枚举类
|
|
|
* @param value 缓存的值
|
|
|
* @param keyParams 缓存key可变的参数列表 可空
|
|
|
*/
|
|
|
public void msetCacheByString(UfoProductCacheKeyEnum cacheKeyEnum, Object value, String... keyParams) {
|
|
|
RedisKeyBuilder redisKeyBuilder = getRedisKeyBuilder(cacheKeyEnum, keyParams);
|
|
|
Map<RedisKeyBuilder, Object> map = null;
|
|
|
cacheClient.mset(map, cacheKeyEnum.getExpireSecondTime());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取缓存
|
|
|
* @param cacheKeyEnum 缓存的枚举类
|
|
|
* @param clazz 缓存的值对象的类型
|
|
|
* @param keyParams 缓存key可变的参数列表 可空
|
|
|
* @param <T>
|
|
|
* @return
|
|
|
*/
|
|
|
public <T> T getCacheByString(UfoProductCacheKeyEnum cacheKeyEnum, Class<T> clazz, String... keyParams) {
|
|
|
RedisKeyBuilder redisKeyBuilder = getRedisKeyBuilder(cacheKeyEnum, keyParams);
|
|
|
return cacheClient.get(redisKeyBuilder, clazz);
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 删除缓存
|
|
|
* @param cacheKeyEnum 缓存的枚举类
|
|
|
* @param keyParams 缓存key可变的参数列表 可空
|
|
|
*/
|
|
|
public void deleteCacheByString(UfoProductCacheKeyEnum cacheKeyEnum, String... keyParams) {
|
|
|
RedisKeyBuilder redisKeyBuilder = getRedisKeyBuilder(cacheKeyEnum, keyParams);
|
|
|
cacheClient.delete(redisKeyBuilder.getKey());
|
|
|
}
|
|
|
|
|
|
|
|
|
private RedisKeyBuilder getRedisKeyBuilder(UfoProductCacheKeyEnum cacheKeyEnum, String[] keyParams) {
|
|
|
RedisKeyBuilder redisKeyBuilder = RedisKeyBuilder.newInstance();
|
|
|
redisKeyBuilder.appendFixed(cacheKeyEnum.getCacheKey()).appendVarWithMH(keyParams);
|
|
|
return redisKeyBuilder;
|
|
|
}
|
|
|
} |
...
|
...
|
|