@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. IMP-00058: ORACLE error 1882 encountered

    问题现象: IMP: ORACLE error encountered ORA: timezone region not found ORA: at "SYS.DBMS_EXPORT_EXT ...

  2. New!Devexpress WinForms各版本支持Visual Studo和SQL Server版本对应图

    点击获取DevExpress v19.2.3最新完整版试用下载 本文主要为大家介绍DevExpress WinForms各大版本支持的VS版本和支持的.Net版本图,Devexpress WinFor ...

  3. Java-生成缩略图工具类

    import java.awt.Color; import java.awt.Graphics2D; import java.awt.Image; import java.awt.RenderingH ...

  4. Java之正则表达式来判断字符串中是否包含字母

    /** * 使用正则表达式来判断字符串中是否包含字母 * @param str 待检验的字符串 * @return 返回是否包含 * true: 包含字母 ;false 不包含字母 */ public ...

  5. WTL自定义控件:edit内容改变响应

    头文件内容: BEGIN_MSG_MAP(myEdit) COMMAND_CODE_HANDLER_EX(EN_CHANGE, OnEnChange) END_MSG_MAP()4 void OnEn ...

  6. 【Wince-ListView】Wince中的 ListView怎么显示网格?

    using System.Runtime.InteropServices; using System.Windows.Forms; namespace CETEST { public class Co ...

  7. linux下安装虚拟环境

    安装pip $ wget https://bootstrap.pypa.io/get-pip.py $ python get-pip.pypython3 -m pip install --upgrad ...

  8. 使用matplotlib绘制常用图表(2)-常用图标设置

    一.使用subplots绘制子图 import numpy as np from matplotlib import pyplot as plt %matplotlib inline x = np.a ...

  9. zabbix添加开机自启

    原文链接:https://www.cnblogs.com/zydev/p/6888805.html 1. zabbix客户端的系统服务脚本 1.1 拷贝启动脚本 zabbix的源码提供了系统服务脚本, ...

  10. linux_svn命令操作

    转至元数据起始   linux下svn命令大全 1.将文件checkout到本地目录 svn checkout path(path是服务器上的目录)例如:svn checkout svn://192. ...