Spring-data-redis入门

环境要求:Redis 2.6及以上,javase 8.0及以上;

一、Spring Data Redis 介绍

Spring-data-redis是spring的一部分,提供了在srping应用中通过简单的配置访问redis服务,对reids底层开发包(Jedis, JRedis, and RJC)进行了高度封装,RedisTemplate提供了redis各种操作、异常处理及序列化,支持发布订阅,并对spring 3.1 cache进行了实现。
spring-data-redis针对jedis提供了如下功能:

1.连接池自动管理,提供了一个高度封装的“RedisTemplate”类.

2.针对jedis客户端中大量api进行了归类封装,将同一类型操作封装为operation接口

  • ValueOperations:简单键值对操作 String

  • SetOperations:set类型数据操作 set

  • ZSetOperations:zset类型数据操作 sortedset---->zset

  • HashOperations:针对hash类型的数据操作 hash

  • ListOperations:针对list类型的数据操作 list
    二、入门案例

1、环境构建

使用springboot构建项目选择redis依赖
  在这里插入图片描述
  2、在resources下新建redis-config. properties文件并加入redis相关配置

# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host-name=49.232.162.73
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=100
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=10000
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=5
# 连接超时时间(毫秒)
spring.redis.timeout=1000000
## 最大连接数
spring.redis.pool.max-total=200

3、在test中SpringDataRedisDemoApplicationTests.java中测试相关api

package com.cenobitor.spring_data_redis_demo;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
import java.util.Set;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringDataRedisDemoApplicationTests {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void contextLoads() {
    }

    /**
     * 值得操作
     */
    @Test
    public void setValue(){
        redisTemplate.boundValueOps("name").set("redis");
    }

    @Test
    public void getValue(){
        String str = (String) redisTemplate.boundValueOps("name").get();
        System.out.println(str);
        Assert.assertNotNull(str);
    }

    /**
     * set类型的操作
     */
    @Test
    public void setSetValue(){
        redisTemplate.boundSetOps("nameset").add("曹操");
        redisTemplate.boundSetOps("nameset").add("孙权");
        redisTemplate.boundSetOps("nameset").add("刘备");
    }
    @Test
    public void getSetValue(){
        Set nameset = redisTemplate.boundSetOps("nameset").members();
        System.out.println(nameset);//[刘备, 孙权, 曹操]
    }
    //删除集合中的某个元素
    @Test
    public void deleteSetValue(){
        Long remove = redisTemplate.boundSetOps("nameset").remove("刘备");
        System.out.println(remove);
        Assert.assertEquals("1",remove);
    }
    //删除整个集合
    @Test
    public void deleteSet(){
        Boolean nameset = redisTemplate.delete("nameset");
        Assert.assertEquals(true,nameset);
    }

    /**
     * List类型操作
     */
    //右压栈:后添加的对象排在后边
    @Test
    public void setListValue1(){
        redisTemplate.boundListOps("namelist1").rightPush("刘备");
        redisTemplate.boundListOps("namelist1").rightPush("关羽");
        redisTemplate.boundListOps("namelist1").rightPush("张飞");
    }
    @Test
    public void getListValue1(){
        List list = redisTemplate.boundListOps("namelist1").range(0, -1);
        System.out.println(list);//[刘备, 关羽, 张飞]
    }
    //左压栈:后添加的对象排在前边
    @Test
    public void setListValue2(){
        redisTemplate.boundListOps("namelist2").leftPush("刘备");
        redisTemplate.boundListOps("namelist2").leftPush("关羽");
        redisTemplate.boundListOps("namelist2").leftPush("张飞");
    }
    @Test
    public void getListValue2(){
        List list = redisTemplate.boundListOps("namelist2").range(0, -1);
        System.out.println(list);//[张飞, 关羽, 刘备]
    }
    //查询集合某个元素
    @Test
    public void searchListByIndex(){
        String s = (String) redisTemplate.boundListOps("namelist1").index(1);
        System.out.println(s);//关羽
    }
    //移除集合某个元素
    @Test
    public void removeListByIndex(){
        redisTemplate.boundListOps("namelist1").remove(1, "关羽");
    }

    /**
     * Hash类型操作
     */
    @Test
    public void setHashValue(){
        redisTemplate.boundHashOps("namehash").put("a", "唐僧");
        redisTemplate.boundHashOps("namehash").put("b", "悟空");
        redisTemplate.boundHashOps("namehash").put("c", "八戒");
        redisTemplate.boundHashOps("namehash").put("d", "沙僧");
    }
    @Test
    public void getHash(){
        //提取所有的KEY
        Set s = redisTemplate.boundHashOps("namehash").keys();
        System.out.println(s);//[a, b, c, d]
        //提取所有的值
        List values = redisTemplate.boundHashOps("namehash").values();
        System.out.println(values);//[唐僧, 悟空, 八戒, 沙僧]
        //根据KEY提取值
        String str = (String) redisTemplate.boundHashOps("namehash").get("b");
        System.out.println(str);//悟空
    }
    //根据KEY移除值
    @Test
    public void removeHashByKey() {
        redisTemplate.boundHashOps("namehash").delete("c");
    }
}

redis官方文档: https://docs.spring.io/spring-data/redis/docs/2.2.3.RELEASE/reference/html/#redis:template

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值