Authored by mali

缓存商品

package com.yohoufo.common.cache;
import com.alibaba.fastjson.JSONObject;
/**
* Created by li.ma on 2019/3/6.
*/
public class CachekeyParamsAndValue {
private Object[] cacheKeyparams;
private Object value;
public void setCacheKeyparams(Object... cacheKeyparams) {
this.cacheKeyparams = cacheKeyparams;
}
public Object[] getCacheKeyparams() {
return cacheKeyparams;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
@Override
public String toString() {
return JSONObject.toJSONString(this);
}
public class Builder {
private Object[] cacheKeyparams;
private Object value;
public Builder setCacheKeyparams(Object... cacheKeyparams) {
this.cacheKeyparams = cacheKeyparams;
return this;
}
public Builder setValue(Object value) {
this.value = value;
return this;
}
public CachekeyParamsAndValue build() {
CachekeyParamsAndValue cachekeyParamsAndValue = new CachekeyParamsAndValue();
cachekeyParamsAndValue.setCacheKeyparams(this.cacheKeyparams);
cachekeyParamsAndValue.setValue(this.value);
return cachekeyParamsAndValue;
}
}
}
\ No newline at end of file
... ...
... ... @@ -2,9 +2,11 @@ package com.yohoufo.product.cache;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yohoufo.common.cache.CacheClient;
import com.yohoufo.common.cache.CachekeyParamsAndValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
/**
... ... @@ -21,7 +23,7 @@ public class UfoProductCacheService {
* @param value 缓存的值
* @param keyParams 缓存key可变的参数列表 可空
*/
public void setCacheByString(UfoProductCacheKeyEnum cacheKeyEnum, Object value, String... keyParams) {
public void setCacheByString(UfoProductCacheKeyEnum cacheKeyEnum, Object value, Object... keyParams) {
RedisKeyBuilder redisKeyBuilder = getRedisKeyBuilder(cacheKeyEnum, keyParams);
cacheClient.setEx(redisKeyBuilder, value, cacheKeyEnum.getExpireSecondTime());
}
... ... @@ -33,9 +35,13 @@ public class UfoProductCacheService {
* @param value 缓存的值
* @param keyParams 缓存key可变的参数列表 可空
*/
public void msetCacheByString(UfoProductCacheKeyEnum cacheKeyEnum, Object value, String... keyParams) {
RedisKeyBuilder redisKeyBuilder = getRedisKeyBuilder(cacheKeyEnum, keyParams);
Map<RedisKeyBuilder, Object> map = null;
public void msetCacheByString(UfoProductCacheKeyEnum cacheKeyEnum, CachekeyParamsAndValue... keyParams) {
Map<RedisKeyBuilder, Object> map = new HashMap<>();
for (CachekeyParamsAndValue params : keyParams) {
RedisKeyBuilder redisKeyBuilder = getRedisKeyBuilder(cacheKeyEnum, params.getCacheKeyparams());
map.put(redisKeyBuilder, params.getValue());
}
cacheClient.mset(map, cacheKeyEnum.getExpireSecondTime());
}
... ... @@ -47,7 +53,7 @@ public class UfoProductCacheService {
* @param <T>
* @return
*/
public <T> T getCacheByString(UfoProductCacheKeyEnum cacheKeyEnum, Class<T> clazz, String... keyParams) {
public <T> T getCacheByString(UfoProductCacheKeyEnum cacheKeyEnum, Class<T> clazz, Object... keyParams) {
RedisKeyBuilder redisKeyBuilder = getRedisKeyBuilder(cacheKeyEnum, keyParams);
return cacheClient.get(redisKeyBuilder, clazz);
}
... ... @@ -64,7 +70,7 @@ public class UfoProductCacheService {
}
private RedisKeyBuilder getRedisKeyBuilder(UfoProductCacheKeyEnum cacheKeyEnum, String[] keyParams) {
private RedisKeyBuilder getRedisKeyBuilder(UfoProductCacheKeyEnum cacheKeyEnum, Object[] keyParams) {
RedisKeyBuilder redisKeyBuilder = RedisKeyBuilder.newInstance();
redisKeyBuilder.appendFixed(cacheKeyEnum.getCacheKey()).appendVarWithMH(keyParams);
return redisKeyBuilder;
... ...