第四篇文章:springboot中使用redis

step1:在pom.xml文件中引入所需的依赖

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

 step2:在application.properties配置文件中写配置信息:

     ### redis 缓存配置
     ### 默认redis 数据库为db0
     spring.redis.database=0

     ### 服务器地址,默认为localhost
     spring.redis.host=localhost

     ### 链接端口,默认为6379
     spring.redis.port=6379

     ### redis 密码默认为空
     spring.redis.password=

step3:

 第一个用于value为字符串类型的key-value键值对操作

第二个用于value为哈希表类型的key-value键值对操作

第三个用于value为列表类型的key-value键值对操作

第四个用于value为set类型的key-value键值对操作

第五个用于value为有序set类型的key-value键值对操作

操作value是字符串类型的key-value

    @Resource
    private StringRedisTemplate stringRedisTemplate;


    @Test
    public  void  testString()
    {
        stringRedisTemplate.opsForValue().set("city","beijing");
        String value=stringRedisTemplate.opsForValue().get("city");
        System.out.println(value);//  beijing

        //最后一个参数表示单位,10秒
        stringRedisTemplate.opsForValue().set("city2","shanghai",10l, TimeUnit.SECONDS);
   
    }

 操作value是哈希类型的key-value

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Test
    public void  testHash()
    {
        stringRedisTemplate.opsForHash().put("001","name","xiaoming");
        stringRedisTemplate.opsForHash().put("001","age","20");
        stringRedisTemplate.opsForHash().put("001","address","beijing");

        String age=(String)stringRedisTemplate.opsForHash().get("001","age");
        System.out.println(age);

        Set feilds=stringRedisTemplate.opsForHash().keys("001");
        System.out.println(feilds);//[name, age, address]

        List values=stringRedisTemplate.opsForHash().values("001");
        System.out.println(values);//[xiaoming, 20, beijing]
    }

 操作value是list类型的key-value

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Test
    public void testList()
    {
        stringRedisTemplate.opsForList().leftPush("002","a");//存入一个值
        stringRedisTemplate.opsForList().leftPushAll("002","b","c","d");//一次性存入多个值

        List list=stringRedisTemplate.opsForList().range("002",0,-1);
        System.out.println(list);//[d, c, b, a]

        String temp= stringRedisTemplate.opsForList().rightPop("002");
        System.out.println(temp);//a

        String temp2= stringRedisTemplate.opsForList().leftPop("002");
        System.out.println(temp2);//d

        Long length=stringRedisTemplate.opsForList().size("002");
        System.out.println(length);//2

    }

 操作value是set类型的key-value

    @Test
    public  void  testSet()
    {
        stringRedisTemplate.opsForSet().add("003","a","b","c","a");

        Set set=stringRedisTemplate.opsForSet().members("003");
        System.out.println(set);//[a, b, c]

        stringRedisTemplate.opsForSet().remove("003","a");
        
    }

 操作value是Zset类型的key-value

    @Test
    public void testZset()
    {
        stringRedisTemplate.opsForZSet().add("004","a",10.0);
        stringRedisTemplate.opsForZSet().add("004","b",11.0);
        stringRedisTemplate.opsForZSet().add("004","c",12.0);
        stringRedisTemplate.opsForZSet().add("004","d",13.0);

        Set set=stringRedisTemplate.opsForZSet().range("004",0,-1);
        System.out.println(set);//[a, b, c, d]

        //修改分数
        stringRedisTemplate.opsForZSet().incrementScore("004","b",20.0);

        set=stringRedisTemplate.opsForZSet().range("004",0,-1);
        System.out.println(set);//[a, c, d, b]

        stringRedisTemplate.opsForZSet().remove("004","a","b");

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值