redis-五种存储类型

@Controller
@RequestMapping("/redis")
public class testredis extends BaseController {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

  /*  JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();

    JedisPool jedisPool = new JedisPool(jedisPoolConfig, "192.168.0.60", 6379, 3000, null);
    Jedis jedis = null;*/

    /**
     * string 类型设置
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("/setString")
    @ResponseBody
    public PageData setString() throws Exception {
        PageData result = new PageData();
        PageData pageData = getPageData();
        //jedis = jedisPool.getResource();
        try {
            final ValueOperations<String, String> temp_string = redisTemplate.opsForValue();
            //opsForValue 设置 string 类型的值
            //1: 设置key ,value,和超时时间,以及时间类型,(分钟,还是秒,小时)
          temp_string.set("chen22vvv", "ss");
            //设置过期时间
            // redisTemplate.expire("chen22", 100, TimeUnit.SECONDS);
            // temp_string.increment("ddddd",2);//当前key每次增加2 相当于计数器
            // temp_string.decrement("ddddd",1);//当前key每次减一
            //temp_string.increment("ddddd"); //每次 自动加一

            /**
             *  string 类型批量插入数据  开始
             */
         /*   Map<String, String> map = new HashMap<>();
            map.put("大哥", "333");
            map.put("小弟", "3334");
            //批量存入數據
            temp_string.multiSet(map);
            //批量取出來數據
            ArrayList<String> arrayList = new ArrayList();
            arrayList.add("小弟");
            arrayList.add("大哥");

            java.util.List<String> strings = temp_string.multiGet(arrayList);
            System.out.println(strings);*/
            /**
             *  string 类型批量插入数据 结束
             */


        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
        return result;
    }

    /**
     * hash
     *
     * @return
     * @throws Exception
     */

    @RequestMapping("/setHash")
    @ResponseBody
    public PageData setHash() throws Exception {
        PageData result = new PageData();
        PageData pageData = getPageData();
        HashOperations<String, Object, Object> temp_hash = redisTemplate.opsForHash();
        try {
            //temp_hash.put("hash_put","33","44");
            /*  temp_hash.put("hash_put","3355","3434");
            temp_hash.put("hash_put","test2","test2");*/
            //得到当一级键  的所有二级键的value值 以list集合返回
            java.util.List<Object> hash_put = temp_hash.values("hash_put");
            //得到当一级键  的所有数据, 已map 集合返回
            Map<Object, Object> hash_put1 = temp_hash.entries("hash_put");
            //取出当前 找到 一级键 下的二级键 对应的值
            Object hash_put2 = temp_hash.get("hash_put", "33");
            //判断当前 变量中 是否有指定 二级键
            Boolean hash_put3 = temp_hash.hasKey("hash_put", "3355");
            if (hash_put3) {
                System.out.println("有此键值");
            }
            //得到当前 一级键下面的 所有键,用set集合返回
            Set<Object> hash_put4 = temp_hash.keys("hash_put");

            //得到当前 一级键值的 二级键的数量
            Long hash_put5 = temp_hash.size("hash_put");
            //给当前的二级 键对应的值 加上3
            Long hash_put6 = temp_hash.increment("hash_put", "33", 3);

            // 指定 二级键 已list返回对应数valu
            List<Object> list = new ArrayList<Object>();
            list.add("33");
            list.add("3355");
            List mapValueList = temp_hash.multiGet("hash_put", list);

            // 批量增加  键值数据
          //  Map newMap = new HashMap();
          //  newMap.put("map3", "map3-3");
          //  newMap.put("map5", "map5-5");
            //temp_hash.putAll("hash_put",newMap);
            //没有弄明白
            //  temp_hash.putIfAbsent("hash_put","map56","map677");


            // 判断 是否有此键, 如果有取出来
          //  Cursor<Map.Entry<Object, Object>> cursor = temp_hash.scan("hash_put", ScanOptions.scanOptions().match("map3").build());
            //Cursor<Map.Entry<Object,Object>> cursor = redisTemplate.opsForHash().scan("hashValue",ScanOptions.NONE);
          //  while (cursor.hasNext()) {
           //     Map.Entry<Object, Object> entry = cursor.next();
           //     System.out.println("通过scan(H key, ScanOptions options)方法获取匹配键值对:" + entry.getKey() + "---->" + entry.getValue());
          //  }
            // 删除指定键值
         //   Long delete = redisTemplate.opsForHash().delete("hash_put", "map3", "map5");


/*
            System.out.println(hash_put2);
            System.out.println(hash_put);
            System.out.println(hash_put1);
            System.out.println(hash_put3);
            System.out.println(hash_put4);
            System.out.println(hash_put5);
            System.out.println(hash_put6);
            System.out.println(mapValueList);
            */
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
        return result;
    }


    /**
     * setList 类型设置
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("/setList")
    @ResponseBody
    public PageData setList() throws Exception {
        PageData result = new PageData();
        PageData pageData = getPageData();
        //jedis = jedisPool.getResource();
        try {
            ListOperations<String, String> temp_list = redisTemplate.opsForList();
            // 增加  list 数据
            /*temp_list.leftPush("list", "a");
            temp_list.leftPush("list", "b");
            temp_list.leftPush("list", "c");
            temp_list.leftPush("list", "d");*/

            // 指定 索引取出数据
            String list = temp_list.index("list", 0);
            // 查找 区间段索引值
            java.util.List<String> list1 = temp_list.range("list", 0, 2);
            //把a 移动到 d 前面一个
            //temp_list.leftPush("list","d","a");


            //把a 移动到 d 前面一个 ,再把 m  移动到 a前面
            // temp_list.leftPushAll("list","d","a","m");

                  /*  List newList = new ArrayList();
                    newList.add("d");
                    newList.add("p");
                    newList.add("q");
                    temp_list.leftPushAll("list",newList);*/
            // 向下追加数据
            //  temp_list.rightPush("list","666");
            //批量增加数据  1
            //temp_list.rightPushAll("list","j","k");

            //批量增加数据  2
          /*  List newList = new ArrayList();
            newList.add("d");
            newList.add("p");
            newList.add("q");
            temp_list.rightPushAll("list", newList);*/
          // 获得当前 list 的数据的数量
           // Long list2 = temp_list.size("list");
            //移除最左边的一个数据
            //String list2 = temp_list.leftPop("list");
            //移除左边第一个的时间
           // temp_list.leftPop("list",1, TimeUnit.SECONDS);
            //移除最右边的一个数据
           // temp_list.rightPop("list");
            //移除右边第一个的时间
           // temp_list.leftPop("list",1, TimeUnit.SECONDS);
            // 再索引位置,如果有值覆盖掉,没有添加进去
           // temp_list.set("list",3,"156");
            //从存储在键中的列表中删除等于值的元素的第一个计数事件。count> 0:删除等于从左到右移动的值的第一个元素;count< 0:删除等于从右到左移动的值的第一个元素;count = 0:删除等于value的所有元素。
          //  temp_list.remove("list",3,"d");
            //保留 1-5区间的数据,其他移除掉
            temp_list.trim("list",1,5);
            // System.out.println(list);
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
        return result;
    }



    /**
     * setSet 类型设置
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("/")
    @ResponseBody
    public PageData setSet() throws Exception {
        PageData result = new PageData();
        PageData pageData = getPageData();
        //jedis = jedisPool.getResource();
        try {
            SetOperations<String, String> temp_set = redisTemplate.opsForSet();
            //向集合中添加数据
           // temp_set.add("set","22","ff","bb");
            // 删除集合中 指定数据
          //  temp_set.remove("set","bb");
            //获得集合中所有的数据
           /* Set<String> set = temp_set.members("set");
            System.out.println(set);*/
           //获得集合中的数据
          /*  Long set = temp_set.size("set");
            System.out.println(set);*/
          //p判断当前集合是否有此数据
           /* Boolean member = temp_set.isMember("set", "ff");
            if(member){
                System.out.println("当前集合 有此数据");
            }*/
           //取出当前 集合中的  几个数据,但是不从集合总删除
      /*      java.util.List<String> set = temp_set.randomMembers("set", 1);
            System.out.println(set);*/
      //从集合中 取出数据 并且在集合中移除掉该数据
            java.util.List<String> set = temp_set.pop("set", 1);
            System.out.println(set);
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
        return result;
    }
    /**
     * setZSet 类型设置
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("/zz")
    @ResponseBody
    public PageData vxccc() throws Exception {
        PageData result = new PageData();
        PageData pageData = getPageData();
        //jedis = jedisPool.getResource();
        try {
            ZSetOperations<String, String> temp_zset = redisTemplate.opsForZSet();
            //向集合中添加数据
           // temp_zset.add("zset","ddCC",2);
            // 删除集合中 指定数据
         //temp_zset.remove("zset","dd");
            //指定 key的到 数据
           /* Double zset = temp_zset.score("zset","dd");
            System.out.println(zset);
*/
           // 集合中 的指定键 加一
              temp_zset.incrementScore("zset","ddCC",1);

        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
        return result;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值