|
|
package com.yoho.unions.common.redis;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.google.common.collect.Lists;
|
|
|
import com.google.common.collect.Maps;
|
|
|
import com.yoho.core.redis.YHHashOperations;
|
|
|
import com.yoho.core.redis.YHRedisTemplate;
|
|
|
import com.yoho.unions.common.utils.SerializeUtils;
|
|
|
import com.yoho.unions.helper.CacheKeyHelper;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.apache.commons.collections.MapUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
...
|
...
|
@@ -11,15 +17,21 @@ import org.springframework.stereotype.Component; |
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Collection;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
@Component
|
|
|
public class RedisHashCache {
|
|
|
|
|
|
private final static Logger logger = LoggerFactory.getLogger(RedisHashCache.class);
|
|
|
|
|
|
@Resource(name="yhHashOperations")
|
|
|
YHHashOperations<String, String, String> yhHashOperations;
|
|
|
@Resource(name = "yhHashOperations")
|
|
|
private YHHashOperations<String, String, String> hashOperations;
|
|
|
|
|
|
@Resource(name = "yhRedisTemplate")
|
|
|
private YHRedisTemplate<String, String> yHRedisTemplate;
|
|
|
|
|
|
/**
|
|
|
* 获取所有的values
|
...
|
...
|
@@ -35,7 +47,7 @@ public class RedisHashCache { |
|
|
return null;
|
|
|
}
|
|
|
|
|
|
List<String> valueList = yhHashOperations.values(key);
|
|
|
List<String> valueList = hashOperations.values(key);
|
|
|
logger.debug("values hashOperation value list size is {}", valueList == null ? 0 : valueList.size());
|
|
|
if (CollectionUtils.isEmpty(valueList)) {
|
|
|
return null;
|
...
|
...
|
@@ -51,4 +63,170 @@ public class RedisHashCache { |
|
|
return null;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* Hash存储数据
|
|
|
*
|
|
|
* @param cacheKey
|
|
|
* @param entries
|
|
|
* @param timeout
|
|
|
* @param unit
|
|
|
* @param <V>
|
|
|
*/
|
|
|
public <V> void add(String cacheKey, Map<String, List<V>> entries, long timeout, TimeUnit unit) {
|
|
|
|
|
|
if (entries == null || entries.size() == 0) {
|
|
|
logger.warn("add params is null .");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
// 将入口参数中的 Map<String, List<V>> 装换成 Map<String, String>
|
|
|
Map<String, String> valueStrMap = Maps.newHashMap();
|
|
|
for (String key : entries.keySet()) {
|
|
|
List<V> valueObjList = entries.get(key);
|
|
|
|
|
|
valueStrMap.put(key, JSONArray.toJSONString(valueObjList));
|
|
|
|
|
|
}
|
|
|
|
|
|
this.hashOperations.putAll(cacheKey, valueStrMap);
|
|
|
this.yHRedisTemplate.longExpire(cacheKey, timeout, unit);
|
|
|
} catch (Exception e) {
|
|
|
logger.warn("add failed!!! key is: {}", cacheKey);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据key和hashkey获取数据
|
|
|
*
|
|
|
* @param cacheKey
|
|
|
* @param hashKey
|
|
|
* @param clazz
|
|
|
* @param <T>
|
|
|
* @return
|
|
|
*/
|
|
|
public <T> T get(String cacheKey, String hashKey, Class<T> clazz) {
|
|
|
try {
|
|
|
if (StringUtils.isEmpty(cacheKey) || StringUtils.isEmpty(hashKey)) {
|
|
|
return null;
|
|
|
}
|
|
|
String value = this.hashOperations.get(cacheKey, hashKey);
|
|
|
if (StringUtils.isEmpty(value)) {
|
|
|
return null;
|
|
|
}
|
|
|
return CacheKeyHelper.string2Value(value, clazz);
|
|
|
} catch (Exception e) {
|
|
|
logger.warn("RedisHashCache get failed!!! cacheKey is: {},hashkey {}", cacheKey, hashKey);
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据key和hashkey获取数据
|
|
|
*
|
|
|
* @param cacheKey
|
|
|
* @param clazz
|
|
|
* @param <T>
|
|
|
* @return
|
|
|
*/
|
|
|
public <T> List<T> multiGet(String cacheKey, Collection<? extends Object> hashKeyList, Class<T> clazz) {
|
|
|
try {
|
|
|
if (StringUtils.isEmpty(cacheKey) || CollectionUtils.isEmpty(hashKeyList)) {
|
|
|
return null;
|
|
|
}
|
|
|
List<String> hkeyList = new ArrayList<>();
|
|
|
for (Object hobj : hashKeyList) {
|
|
|
hkeyList.add(hobj.toString());
|
|
|
}
|
|
|
List<String> valueList = this.hashOperations.multiGet(cacheKey, hkeyList);
|
|
|
if (CollectionUtils.isEmpty(valueList)) {
|
|
|
return null;
|
|
|
}
|
|
|
List<T> resultList = Lists.newArrayList();
|
|
|
for (String result : valueList) {
|
|
|
if (StringUtils.isEmpty(result)) {
|
|
|
continue;
|
|
|
}
|
|
|
resultList.add(CacheKeyHelper.string2Value(result, clazz));
|
|
|
}
|
|
|
return resultList;
|
|
|
} catch (Exception e) {
|
|
|
logger.warn("RedisHashCache get failed!!! cacheKey is: {},hashkey {}", cacheKey, hashKeyList);
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 存储hash结构数据
|
|
|
*
|
|
|
* @param cacheKey
|
|
|
* @param hashKey
|
|
|
* @param value
|
|
|
* @param timeout
|
|
|
* @param unit
|
|
|
* @param <T>
|
|
|
*/
|
|
|
public <T> void put(String cacheKey, String hashKey, T value, long timeout, TimeUnit unit) {
|
|
|
try {
|
|
|
if (StringUtils.isEmpty(cacheKey) || StringUtils.isEmpty(hashKey)) {
|
|
|
logger.warn("RedisHashCache put failed!!! cacheKey is: {},hashkey {}", cacheKey, hashKey);
|
|
|
return;
|
|
|
}
|
|
|
String v = CacheKeyHelper.value2String(value);
|
|
|
if (StringUtils.isEmpty(v)) {
|
|
|
logger.warn("RedisHashCache put failed!!! value {}", v);
|
|
|
return;
|
|
|
}
|
|
|
this.hashOperations.put(cacheKey, hashKey, v);
|
|
|
this.yHRedisTemplate.longExpire(cacheKey, timeout, unit);
|
|
|
} catch (Exception e) {
|
|
|
logger.warn("RedisHashCache get failed!!! cacheKey is: {},hashkey {}", cacheKey, hashKey);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 批量put
|
|
|
*
|
|
|
* @param obj key的后缀
|
|
|
* @param m 需要设置的值(该map的key是去除hashKey后缀的值)
|
|
|
* @param unit 超时时间单位
|
|
|
*/
|
|
|
public <T> void putAll(String cacheKey, Map<? extends String, ? extends T> map, long timeout, TimeUnit unit) {
|
|
|
logger.debug("Enter putAll hashOperation redis value. obj is {}, m is {}", map);
|
|
|
if (MapUtils.isEmpty(map)) {
|
|
|
return;
|
|
|
}
|
|
|
try {
|
|
|
if (StringUtils.isBlank(cacheKey)) {
|
|
|
return;
|
|
|
}
|
|
|
Map<String,String> newMap = Maps.newHashMap();
|
|
|
for(Map.Entry<? extends String, ? extends T> o : map.entrySet()){
|
|
|
newMap.put(o.getKey(),CacheKeyHelper.value2String(o.getValue()));
|
|
|
}
|
|
|
// 批量set
|
|
|
hashOperations.putAll(cacheKey, newMap);
|
|
|
// 设置超时
|
|
|
yHRedisTemplate.longExpire(cacheKey, timeout, unit);
|
|
|
} catch (Exception e) {
|
|
|
logger.warn("Redis exception. hash redis putAll . obj is {}, map is {}, exception msg is {}", map, e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 判断值是否存在
|
|
|
* @param cacheKey
|
|
|
* @return
|
|
|
*/
|
|
|
public Boolean hasKey(String cacheKey) {
|
|
|
logger.debug("Enter hasKey hashOperation redis. cacheKey is {}", cacheKey);
|
|
|
try {
|
|
|
return yHRedisTemplate.hasKey(cacheKey);
|
|
|
} catch (Exception e) {
|
|
|
logger.warn("get haskey method fail!! e {}", e);
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|