Authored by mali

缓存商品

  1 +package com.yohoufo.common.cache;
  2 +
  3 +import com.alibaba.fastjson.JSONObject;
  4 +
  5 +/**
  6 + * Created by li.ma on 2019/3/6.
  7 + */
  8 +public class CachekeyParamsAndValue {
  9 + private Object[] cacheKeyparams;
  10 +
  11 + private Object value;
  12 +
  13 + public void setCacheKeyparams(Object... cacheKeyparams) {
  14 + this.cacheKeyparams = cacheKeyparams;
  15 + }
  16 +
  17 + public Object[] getCacheKeyparams() {
  18 + return cacheKeyparams;
  19 + }
  20 +
  21 + public Object getValue() {
  22 + return value;
  23 + }
  24 +
  25 + public void setValue(Object value) {
  26 + this.value = value;
  27 + }
  28 +
  29 + @Override
  30 + public String toString() {
  31 + return JSONObject.toJSONString(this);
  32 + }
  33 +
  34 + public class Builder {
  35 + private Object[] cacheKeyparams;
  36 +
  37 + private Object value;
  38 +
  39 + public Builder setCacheKeyparams(Object... cacheKeyparams) {
  40 + this.cacheKeyparams = cacheKeyparams;
  41 + return this;
  42 + }
  43 +
  44 + public Builder setValue(Object value) {
  45 + this.value = value;
  46 + return this;
  47 + }
  48 +
  49 + public CachekeyParamsAndValue build() {
  50 + CachekeyParamsAndValue cachekeyParamsAndValue = new CachekeyParamsAndValue();
  51 + cachekeyParamsAndValue.setCacheKeyparams(this.cacheKeyparams);
  52 + cachekeyParamsAndValue.setValue(this.value);
  53 + return cachekeyParamsAndValue;
  54 + }
  55 + }
  56 +
  57 +}
@@ -2,9 +2,11 @@ package com.yohoufo.product.cache; @@ -2,9 +2,11 @@ package com.yohoufo.product.cache;
2 2
3 import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder; 3 import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
4 import com.yohoufo.common.cache.CacheClient; 4 import com.yohoufo.common.cache.CacheClient;
  5 +import com.yohoufo.common.cache.CachekeyParamsAndValue;
5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.stereotype.Service; 7 import org.springframework.stereotype.Service;
7 8
  9 +import java.util.HashMap;
8 import java.util.Map; 10 import java.util.Map;
9 11
10 /** 12 /**
@@ -21,7 +23,7 @@ public class UfoProductCacheService { @@ -21,7 +23,7 @@ public class UfoProductCacheService {
21 * @param value 缓存的值 23 * @param value 缓存的值
22 * @param keyParams 缓存key可变的参数列表 可空 24 * @param keyParams 缓存key可变的参数列表 可空
23 */ 25 */
24 - public void setCacheByString(UfoProductCacheKeyEnum cacheKeyEnum, Object value, String... keyParams) { 26 + public void setCacheByString(UfoProductCacheKeyEnum cacheKeyEnum, Object value, Object... keyParams) {
25 RedisKeyBuilder redisKeyBuilder = getRedisKeyBuilder(cacheKeyEnum, keyParams); 27 RedisKeyBuilder redisKeyBuilder = getRedisKeyBuilder(cacheKeyEnum, keyParams);
26 cacheClient.setEx(redisKeyBuilder, value, cacheKeyEnum.getExpireSecondTime()); 28 cacheClient.setEx(redisKeyBuilder, value, cacheKeyEnum.getExpireSecondTime());
27 } 29 }
@@ -33,9 +35,13 @@ public class UfoProductCacheService { @@ -33,9 +35,13 @@ public class UfoProductCacheService {
33 * @param value 缓存的值 35 * @param value 缓存的值
34 * @param keyParams 缓存key可变的参数列表 可空 36 * @param keyParams 缓存key可变的参数列表 可空
35 */ 37 */
36 - public void msetCacheByString(UfoProductCacheKeyEnum cacheKeyEnum, Object value, String... keyParams) {  
37 - RedisKeyBuilder redisKeyBuilder = getRedisKeyBuilder(cacheKeyEnum, keyParams);  
38 - Map<RedisKeyBuilder, Object> map = null; 38 + public void msetCacheByString(UfoProductCacheKeyEnum cacheKeyEnum, CachekeyParamsAndValue... keyParams) {
  39 + Map<RedisKeyBuilder, Object> map = new HashMap<>();
  40 + for (CachekeyParamsAndValue params : keyParams) {
  41 + RedisKeyBuilder redisKeyBuilder = getRedisKeyBuilder(cacheKeyEnum, params.getCacheKeyparams());
  42 + map.put(redisKeyBuilder, params.getValue());
  43 + }
  44 +
39 cacheClient.mset(map, cacheKeyEnum.getExpireSecondTime()); 45 cacheClient.mset(map, cacheKeyEnum.getExpireSecondTime());
40 } 46 }
41 47
@@ -47,7 +53,7 @@ public class UfoProductCacheService { @@ -47,7 +53,7 @@ public class UfoProductCacheService {
47 * @param <T> 53 * @param <T>
48 * @return 54 * @return
49 */ 55 */
50 - public <T> T getCacheByString(UfoProductCacheKeyEnum cacheKeyEnum, Class<T> clazz, String... keyParams) { 56 + public <T> T getCacheByString(UfoProductCacheKeyEnum cacheKeyEnum, Class<T> clazz, Object... keyParams) {
51 RedisKeyBuilder redisKeyBuilder = getRedisKeyBuilder(cacheKeyEnum, keyParams); 57 RedisKeyBuilder redisKeyBuilder = getRedisKeyBuilder(cacheKeyEnum, keyParams);
52 return cacheClient.get(redisKeyBuilder, clazz); 58 return cacheClient.get(redisKeyBuilder, clazz);
53 } 59 }
@@ -64,7 +70,7 @@ public class UfoProductCacheService { @@ -64,7 +70,7 @@ public class UfoProductCacheService {
64 } 70 }
65 71
66 72
67 - private RedisKeyBuilder getRedisKeyBuilder(UfoProductCacheKeyEnum cacheKeyEnum, String[] keyParams) { 73 + private RedisKeyBuilder getRedisKeyBuilder(UfoProductCacheKeyEnum cacheKeyEnum, Object[] keyParams) {
68 RedisKeyBuilder redisKeyBuilder = RedisKeyBuilder.newInstance(); 74 RedisKeyBuilder redisKeyBuilder = RedisKeyBuilder.newInstance();
69 redisKeyBuilder.appendFixed(cacheKeyEnum.getCacheKey()).appendVarWithMH(keyParams); 75 redisKeyBuilder.appendFixed(cacheKeyEnum.getCacheKey()).appendVarWithMH(keyParams);
70 return redisKeyBuilder; 76 return redisKeyBuilder;