码迷,mamicode.com
首页 > 编程语言 > 详细

Spring RedisTemplate常用方法(List,Hash)

时间:2019-07-18 16:48:39      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:des   val   ash   nmap   ide   nal   redis   for   list   

@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)

标签:des   val   ash   nmap   ide   nal   redis   for   list   

原文地址:https://www.cnblogs.com/passedbylove/p/11208109.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!