zoukankan      html  css  js  c++  java
  • Java操作Redis

    导入依赖

    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.9.0</version>
    </dependency>
    <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    JedisAPI类

    连接Redis

    public void before(){
            //连接Redis
            jedis=new Jedis("192.168.7.89");
            //设置权限密码
            jedis.auth("redis");
            jedis.ping();
            System.out.println("redis连接成功");
        }

    Redis操作String类

    @Test
        public void operationString(){
            //切换数据库
            jedis.select(1);
            //设置一个String类型的key-value
            jedis.set("setkey01","strValue01");
            //如何设置key的有效期
            //jedis.exprire("strKey01",20);
            //获取String数据
            String value = jedis.get("strKey01");
            System.out.println("对应存放的value值为:"+value);
            //修改数据
            jedis.set("strKey01","strValue");
            //删除数据
            jedis.del("strKey01");
            //设置多个值
            jedis.mset("strKay01","strValue01","strKey02","strValue02","strKey03","strValue03","1");
            //+1操作
            jedis.incr("strKey04");
            //-1操作
            jedis.decr("strKey04");
            //拼接
            jedis.append("strKey04","append");
        }

    redis操作list

        @Test
        public void  openrationList(){
            //清空集合
            jedis.del("listkey01","listkey02");
            //设置List集合值,左侧插入
            jedis.lpush("listkey01","张三","李四","王五");
            //设置List集合值,右侧插入
            jedis.rpush("listkey02","北京","上海","广州");
            //获取List集合值
            List<String> listkey01 = jedis.lrange("list可以01",0,-1);
            for (String item:listkey01){
                System.out.println(item);
            }
            //更改List集合数据
            jedis.lset("listkey01",0,"赵六");
        }

    redis操作set

    @Test
        public void operationSet(){
            //设置Set集合
            jedis.sadd("listkey01","张三","李四","王五");
            //获取
            Set<String> setkey01=jedis.smembers("setkey01");
            for (String item:setkey01){
                System.out.println(item);
            }
        }

    redis操作SortedSet

    public void openrationSortedSet(){
            //设置SortedSet集合
            jedis.zadd("setKey02",1,"北京");
            jedis.zadd(":setkey02",2,"上海");
            //获取SortedSed
            Set<String> setkey02 = jedis.zrange("setkey02",0,-1);
            for (String item:setkey02){
                System.out.println(item);
            }
        }

    redis操作map

    @Test
        public void operationMap(){
            //设置Map
            Map<String,String> map=new HashMap<>();
            map.put("name","张三");
            jedis.hmset("mapkey01",map);
    
            //获取值
            List<String> value = jedis.hmget("mapkey01","name","age");
            for (String item:value){
                System.out.println(item);
            }
  • 相关阅读:
    利用clear清除浮动的一些问题
    配置SpringBoot方便的切换jar和war
    java并发实战:连接池实现
    canvas绘制圆角头像
    对象的合并及拷贝
    JS数组去重
    浏览器端用JS实现创建和下载图片
    超过固定宽度(或行数)显示...(或省略)
    ssh实现免密登录
    Mac 日常使用问题收集
  • 原文地址:https://www.cnblogs.com/dabrk/p/12120183.html
Copyright © 2011-2022 走看看