@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. web开发中的支付宝支付和微信支付

    https://www.jianshu.com/p/155757d2b9eb <!-- wxPay --SDK--> <script src="https://res.wx ...

  2. CSS基础学习-13.CSS 浮动

    如果前一个元素设置浮动属性,则之后的元素也会继承float属性,我觉得这里说是继承不太对,可以理解为会影响到之后的元素,所以在设置浮动元素之后的元素要想不被影响就需要清除浮动.元素设置左浮动,则清除左 ...

  3. c# 获取api 数据

    private string GetDataFromServerApi(string url, string body) { string str = ""; try { Http ...

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

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

  5. Eclipse里Maven配置

    简单记录一下,太特么困了,这几天天天加班很晚来着 : 选中.Apply and Close. 完成. 日他得,腰都快加断了……:) ---------------------------------- ...

  6. 决策树--CART树详解

    1.CART简介 CART是一棵二叉树,每一次分裂会产生两个子节点.CART树分为分类树和回归树. 分类树主要针对目标标量为分类变量,比如预测一个动物是否是哺乳动物. 回归树针对目标变量为连续值的情况 ...

  7. centos7编译安装php7.3

    处理问题   解决php configure: error: Cannot find ldap libraries in /usr/lib.错误 cp -frp /usr/lib64/libldap* ...

  8. django之ajax结合sweetalert使用,分页器和bulk_create批量插入 07

    目录 sweetalert插件 bulk_create 批量插入数据 分页器 简易版本的分页器的推导 自定义分页器的使用(组件) sweetalert插件 有这么一个需求: ​ 当用户进行一个删除数据 ...

  9. vue路由登录拦截(vue router登录权限控制)

    实现原理: 哪些路由需要验证需要在路由文件router/index.js中指定: { path: '/', component: Home, name: 'Home', iconCls: 'fa fa ...

  10. Amazon Redshift and the Case for Simpler Data Warehouses

    Redshift是Amazon一个商业产品上的进化 但并不是技术的进化,他使用的无非都是传统数仓领域的技术 如果说创新,就是大量使用Amazon本身的云服务的云原生架构,大大提升的产品的迭代速度,可维 ...