Spring RedisTemplate常用方法(List,Hash)
Posted passedbylove
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring RedisTemplate常用方法(List,Hash)相关的知识,希望对你有一定的参考价值。
@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("[email protected]查询到的数据,key:,val:",cacheId,key,json); list.add(instance); return list;
以上是关于Spring RedisTemplate常用方法(List,Hash)的主要内容,如果未能解决你的问题,请参考以下文章
Redis 入门和 RedisTemplate 常用方法(常用命令 + 案例源码)
redis-RedisTemplate.opsForValue 常用方法
springboot 中 RedisCacheManager rm = new RedisCacheManager(redisTemplate);我的项目没这个构造