zoukankan      html  css  js  c++  java
  • 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("list@{}查询到的数据,key:{},val:{}",cacheId,key,json);
                list.add(instance);
            }
            return list;
        }
  • 相关阅读:
    超棒的jQuery矢量地图生成插件 JQVAMP
    签名一年过期 项目导入出现 红叉叉
    invalid commandline parameter: Files\Android\androidsdk\tools/emulatorarm.exe 错误
    线性布局 相对布局 参数
    触摸事件 按下 移动 弹起
    Activity service 通信
    android view的setVisibility方法值的意思
    TextView 支持 html 图片显示
    布局动态添加 相对布局
    存储数据 SharedPreferences
  • 原文地址:https://www.cnblogs.com/passedbylove/p/11208109.html
Copyright © 2011-2022 走看看