zoukankan      html  css  js  c++  java
  • Redis(七):RedisTemplate 操作API

    一、scan

    SCAN 命令用于迭代当前数据库中的数据库键。

    SSCAN 命令用于迭代集合键中的元素。

    HSCAN 命令用于迭代哈希键中的键值对。

    ZSCAN 命令用于迭代有序集合中的元素(包括元素成员和元素分值)。

    1. 数据库基本命令

    1)扫描所有数据表

    scan 0

    2)扫描hash表Real_Gps中的两条记录

    HSCAN Real_Gps 0 MATCH * COUNT 2

    2. RedisTemplate操作scan

    //1. 一次性获取Real_Gps中数据  
    Map<Object, Object> map1 =redisTemplate.opsForHash().entries("Real_Gps");
    
    //2. 使用Scan方式遍历获取Real_Gps中的数据   
    ScanOptions scanOptions = ScanOptions.scanOptions().count(1).match("*").build();  
    Cursor<Entry<Object, Object>> cursor = redisTemplate.opsForHash().scan("Real_Gps", scanOptions);  
    while(cursor.hasNext()) {  
        Map.Entry<Object, Object> entry = cursor.next();  
        entry.getKey();  
        entry.getValue();  
    }  

    二、HashMap

    public class RedisUtil {
        /*
        //Object --> JSONString
        String jsonStr = JSONObject.toJSONString(storageClient);
        //存入
        redisUtil.hput("fdfs", "storageClient", jsonStr);
        //获取
        Object obj = redisUtil.hget("fdfs","storageClient");
        //JSONString --> Object
        StorageClient storageClient = JSONObject.parseObject(obj.toString(),StorageClient.class);
         */
    
        @Autowired
        private RedisTemplate redisTemplate;
    
        /**
         * 根据键、项获取值
         * @param key
         * @param hashKey
         * @return
         */
        public Object get(Object key, Object hashKey){
            return redisTemplate.opsForHash().get(key, hashKey);
        }
    
        /**
         * 根据指定键、多个项,返回对应多个值的集合
         * @param key
         * @param hashKeys
         * @return
         */
        public List multiGet(Object key, Collection hashKeys){
            return redisTemplate.opsForHash().multiGet(key, hashKeys);
        }
    
        /**
         * 插入键、项、值
         * @param key
         * @param hashKey
         * @param value
         */
        public void put(Object key, Object hashKey, Object value){
            redisTemplate.opsForHash().put(key, hashKey, value);
        }
    
        /**
         * 一次性插入键、多个项、多个值
         * @param key
         * @param map
         */
        public void putAll(Object key, Map map){
            redisTemplate.opsForHash().putAll(key, map);
        }
    
        /**
         * 如果指定项和值不存在,则插入
         * @param key
         * @param hashKey
         * @param value
         * @return
         */
        public Boolean putIfAbsent(Object key, Object hashKey, Object value){
            return redisTemplate.opsForHash().putIfAbsent(key, hashKey, value);
        }
    
        /**
         * 删除指定一个或多少键位下的项
         * @param key
         * @param item
         * @return
         */
        public Long delete(Object key, Object... item){
            return redisTemplate.opsForHash().delete(key, item);
        }
    
        /**
         * 根据指定键、项判断项是否存在
         * @param key
         * @param hashKey
         * @return
         */
        public Boolean hasKey(Object key, Object hashKey){
            return redisTemplate.opsForHash().hasKey(key, hashKey);
        }
    
        /**
         * 获取指定键下所有的项、值,并返回Map集合
         * @param key
         * @return
         */
        public Map<String,Object> entries(Object key){
            return redisTemplate.opsForHash().entries(key);
        }
    
        /**
         * 将指定键、项对应的数字增加相应量(redis中数据也会增加)
         * @param key
         * @param hashKey
         * @param delta
         * @return
         */
        public Long increment(Object key, Object hashKey, long delta){
            return redisTemplate.opsForHash().increment(key, hashKey, delta);
        }
    
        /**
         * 将指定键、项对应的数字增加相应量(redis中数据也会增加)
         * @param key
         * @param hashKey
         * @param delta
         * @return
         */
        public Double increment(Object key, Object hashKey, double delta){
            return redisTemplate.opsForHash().increment(key, hashKey, delta);
        }
    
        /**
         * 根据指定键返回对应所有项的集合
         * @param key
         * @return
         */
        public Set keys(Object key){
            return redisTemplate.opsForHash().keys(key);
        }
    
        /**
         * 根据指定键返回对应所有值的集合
         * @param key
         * @return
         */
        public List values(Object key){
            return redisTemplate.opsForHash().values(key);
        }
    
        /**
         * 根据指定键返回对应项值的数量
         * @param key
         * @return
         */
        public Long size(Object key){
            return redisTemplate.opsForHash().size(key);
        }
    }
  • 相关阅读:
    DHT(Distributed Hash Table) Translator
    Introducing shard translator
    【转】shell脚本中echo显示内容带颜色
    javac 错误: 编码GBK的不可映射字符
    一致性哈希(consistent hashing)
    在bash shell中使用getfattr查看文件扩展属性
    css3在不同型号手机浏览器上的兼容一览表
    META是什么意思?
    JS异步加载的三种方式
    AJAX中的同步加载与异步加载
  • 原文地址:https://www.cnblogs.com/yifanSJ/p/9108310.html
Copyright © 2011-2022 走看看