@Autowired
private RedisTemplate<String, String> redisTemplate; @Override
public List<String> getCachedList(String key)
{
Long size = getCachedListSize(key);
Long end = size - 1;
return getCachedListByRange(key,0L,end);
} @Override
public Long getCachedListSize(String key)
{
return redisTemplate.opsForList().size(key);
} @Override
public List<String> getCachedListByRange(String key, Long start, Long end)
{
return redisTemplate.opsForList().range(key,start,end);
} @Override
public <T> List<T> getCachedList(List<String> jsons, Class<T> targetClass)
{
if(CollectionUtils.isEmpty(jsons))
{
return Collections.emptyList();
}
return jsons.stream().map(json->JSON.parseObject(json,targetClass)).collect(Collectors.toCollection(LinkedList::new));
} @Override
public <T> List<T> getCachedList(String snapshotKey, String ipAddr, final Predicate<? super T> filter, Class<T> targetClass)
{
List<T> list = getCachedList(snapshotKey,ipAddr, targetClass); if(CollectionUtils.isEmpty(list))
{
return Collections.emptyList();
}
return list.parallelStream().filter(filter).collect(Collectors.toList());
}

hash操作

    @Override
public <T> Map<String,T> getCachedKV(String cachedKey, String hashKey, Long nodeId, Class<T> targetClass)
{
String statusId = String.format("%s:nodeId%d", cachedKey,nodeId); List<Map<String,T>> list = getCachedKV(statusId,targetClass);
if(CollectionUtils.isEmpty(list))
{
return Collections.emptyMap();
}
Optional<Map<String, T>> matched = list.stream().filter(map -> {
return map.keySet().contains(hashKey);
}).findFirst(); if(matched.isPresent()) {
return matched.get();
}
return Collections.emptyMap();
}
@Override
public <T> List<Map<String,T>> getCachedKV(String cachedId, Class<T> targetClass)
{
Map<Object, Object> memCached = redisTemplate.opsForHash().entries(cachedId);
if (MapUtils.isEmpty(memCached)) {
return Collections.emptyList();
}
List<Map<String,T>> list = new LinkedList<Map<String,T>>(); for(Map.Entry<Object,Object> entry:memCached.entrySet())
{
String key = (String)entry.getKey();
String json = (String)entry.getValue();
list.add(Collections.singletonMap(key,JSON.parseObject(json,targetClass)));
}
return list;
} @Override
public <T> T getStatus(String statusKey, String hashKey, Long nodeId, Class<T> targetClass)
{
Map<String, T> result = getCachedKV(statusKey, hashKey, nodeId, targetClass); if(!result.isEmpty())
{
Optional<T> entity = result.values().stream().findFirst();
if(entity.isPresent())
{
return entity.get();
}
} return null;
} @Override
public <T> T getCachedObjectFromList(String cacheId, Class<T> targetClass)
{
List<T> list = new LinkedList<>(); Map<Object, Object> memCached = redisTemplate.opsForHash().entries(cacheId); Optional<String> matched = memCached.values().stream().map(String::valueOf).findFirst();
if(matched.isPresent())
{
String json = matched.get();
return JSON.parseObject(json,targetClass);
}
return null;
} @Override
public List<SnmpNode> getCachedNodes()
{
return getHashValues(CACHED_NODE,SnmpNode.class);
} @Override
public <T> List<T> getHashValues(String cacheId, Class<T> targetClass)
{
List<T> list = new LinkedList<>(); Map<Object, Object> memCached = redisTemplate.opsForHash().entries(cacheId);
if(MapUtils.isEmpty(memCached))
{
return Collections.synchronizedList(Collections.emptyList());
} for (Map.Entry<Object, Object> entry : memCached.entrySet()) {
String key = (String) entry.getKey();
String json = (String) entry.getValue();
T instance = JSON.parseObject(json,targetClass);
log.debug("list@{}查询到的数据,key:{},val:{}",cacheId,key,json);
list.add(instance);
}
return list;
}

Spring RedisTemplate常用方法(List,Hash)的更多相关文章

  1. spring redistemplate中setHashValueSerializer的设置

    笔者曾经对redis键值使用了不同类型的序列化方法 用过默认值.JdkSerializationRedisSerializer.StringRedisSerializer还用改以下自定类型的序列化工具 ...

  2. Spring RedisTemplate操作-xml配置(1)

    网上没能找到全的spring redistemplate操作例子,故特意化了点时间做了接口调用练习,基本包含了所有redistemplate方法. 该操作例子是个系列,该片为spring xml配置, ...

  3. 曹工说Spring Boot源码(20)-- 码网灰灰,疏而不漏,如何记录Spring RedisTemplate每次操作日志

    写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...

  4. Spring JDBC常用方法详细示例

    Spring JDBC使用简单,代码简洁明了,非常适合快速开发的小型项目.下面对开发中常用的增删改查等方法逐一示例说明使用方法 1 环境准备 启动MySQL, 创建一个名为test的数据库 创建Mav ...

  5. spring RedisTemplate的使用(一)--xml配置或JavaConfig配置

    1.xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="h ...

  6. Spring RedisTemplate操作-事务操作(9)

    @Autowired @Qualifier("redisTemplate") private RedisTemplate<String, String> stringr ...

  7. Spring RedisTemplate操作-发布订阅操作(8)

    @Component("sub") public class Sub implements MessageListener{ @Autowired private StringRe ...

  8. [BZOJ 3198] [Sdoi2013] spring 【容斥 + Hash】

    题目链接:BZOJ - 3198 题目分析 题目要求求出有多少对泉有恰好 k 个值相等. 我们用容斥来做. 枚举 2^6 种状态,某一位是 1 表示这一位相同,那么假设 1 的个数为 x . 答案就是 ...

  9. Spring RedisTemplate操作-序列化性能测试(12)

    @Autowired @Qualifier("redisTemplate") private RedisTemplate<String, String> stringr ...

随机推荐

  1. 配置lnmp时出现“file not found?

    问题: 昨天测试并搭建lnmp架构,在测试过程中,挂载好测试环境时在浏览器上测试网站出现“file not found",使用命令curl时也出现”file not found", ...

  2. Redis08-击穿&穿透&雪崩&spring data redis

    一.常见概念 击穿: 概念:redis作为缓存,设置了key的过期时间,key在过期的时候刚好出现并发访问,直接击穿redis,访问数据库 解决方案:使用setnx() ->相当于一把锁,设置的 ...

  3. Datasets and Evaluation Metrics used in Recommendation System

    Movielens and Netflix remain the most-used datasets. Other datasets such as Amazon, Yelp and CiteUli ...

  4. 集合(五) TreeMap

    4.TreeMap SortedMap接口继承Map接口,是排序键值对的接口,实现排序的的方法是Comparator.而NavigableMap接口继承于SortedMap,新增了一些导航方法.而Tr ...

  5. Internet层协议下IP协议

    Internet层协议特征 运行于 OSI 网络层面向无连接的协议独立处理数据包分层编址尽力而为传输无数据恢复功能 Internet层主要包含IP.ICMP.ARP.RARP几个协议. 这一主要说IP ...

  6. 关于HTML5视频标签的问题

    一.基本 video标签在兼容性上还是比较差的,如果要在页面中使用video标签,需要考虑三种情况,支持Ogg Theora或者VP8的(Opera.Mozilla.Chrome),支持H.264的( ...

  7. 纯前端导出Excel表格

    <html> <head> <p style="font-size: 20px;color: red;">使用a标签方式将json导出csv文件 ...

  8. K8s中RS和Deployment

    什么是ReplicaSet? ReplicaSet是下一代复本控制器.ReplicaSet和 Replication Controller之间的唯一区别是现在的选择器支持.Replication Co ...

  9. codeforces271D

    Good Substrings CodeForces - 271D 给你一个只包含小写字母的字符串s.问你在这个字符串中有多少个不同的子串.且要求这些子串中不得出现超过k个的特殊字母.*子串s1和子串 ...

  10. codeforces269B

    Greenhouse Effect CodeForces - 269B Emuskald is an avid horticulturist and owns the world's longest ...