springboot集成redis(基础篇)

一 前言

一篇关于springboot集成redis的小知识,如果有学过redis基础篇的读者很容易看懂,没学过的建议了解一下再看本篇内容;

公众号:知识追寻者

知识追寻者(Inheriting the spirit of open source, Spreading technology knowledge;)

二 集成配置

2.1 依赖

springboot 2.1.1 版本

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

2.2 配置类

使用 自定义的配置 替换 JdkSerializationRedisSerializer, 否则序列化进 redis 会显示为特殊字符;

查阅 RedisReactiveAutoConfiguration 类可以查看 reactiveRedisTemplate 装配过程‘;

查阅 RedisAutoConfiguration 类可以查看 redisTemplate 装配过程‘;

/**
 * @Author lsc
 * <p> redis配置 替换默认的jdk序列化机制 </p>
 */
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        // 创建redisTemplate
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);

        // 使用Jackson2JsonRedisSerialize替换默认序列化
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

        // key采用String的序列化方式
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        // value序列化方式采用jackson
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的key也采用String的序列化方式
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        // hash的value序列化方式采用jackson
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

2.3 application.yml

redis 的 连接属性配置和连接池配置;

server:
  port: 9000

spring:
  redis:
    # Redis服务器地址
    host: localhost
    #Redis服务器连接端口
    port: 6379
    #password:
    # Redis数据库索引(默认为0)
    database: 1
    # 连接超时时间(毫秒)
    timeout: 5000
    jedis:
      pool:
        #连接池最大连接数(使用负值表示没有限制)
        max-active: 100
        # 连接池中的最小空闲连接
        max-idle: 10
        # 连接池最大阻塞等待时间(使用负值表示没有限制)
        max-wait: 100000

三 数据类型测试

3.1 String操作

    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    // string 入库
    @Test
    public void testForValue1(){
        String key = "zszxz";
        String value = "知识追寻者";
        redisTemplate.opsForValue().set(key, value);
    }
    
    // string 出库
    @Test
    public void testForValue2(){
        String key = "zszxz";
        Object value = redisTemplate.opsForValue().get(key);
        // 知识追寻者
        System.out.println(value);
    }

    // string key过期时间入库
    @Test
    public void testForValue3(){
        String key = "today";
        String value = "周六";
        long time = 60;
        redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
    }

3.2 key操作

	@Autowired
    RedisTemplate<String,Object> redisTemplate;

    // 测试 key 是否存在
    @Test
    public void test1(){
        String key = "zszxz";
        Boolean exist = redisTemplate.hasKey(key);
        // true
        System.out.println(exist);
    }
    // 设置 key 过期时间
    @Test
    public void test2(){
        String key = "zszxz";
        long time = 60;
        redisTemplate.expire(key, time, TimeUnit.SECONDS);
    }

    // 获取key过期时间
    @Test
    public void test3(){
        String key = "zszxz";
        Long expire = redisTemplate.getExpire(key, TimeUnit.SECONDS);
        // 7
        System.out.println(expire);
    }

    // 删除key
    @Test
    public void test4(){
        String key = "zszxz";
        redisTemplate.delete(key);
    }

3.3 散列操作

 	@Autowired
    RedisTemplate<String,Object> redisTemplate;

    // 放入一个 hash ( key value )
    @Test
    public void test1(){
        String key = "zszxz";
        String item = "name";
        String value = "知识追寻者";
        redisTemplate.opsForHash().put(key, item, value);
    }

    // 向hash中存放一个map
    @Test
    public void test2(){
        String key = "feature";
        Map<String, Object> map = new HashMap<>();
        map.put("name", "知识追寻者");
        map.put("age", "18");
        redisTemplate.opsForHash().putAll(key, map);
    }

    // 获取一个hash 的 所有key-value
    @Test
    public void test3(){
        String key = "feature";
        Map<Object, Object> entries = redisTemplate.opsForHash().entries(key);
        // {name=知识追寻者, age=18}
        System.out.println(entries);
    }

    // 获取一个hash 的 指定key 的value
    @Test
    public void test4(){
        String key = "feature";
        String item = "name";
        Object value = redisTemplate.opsForHash().get(key, item);
        // 知识追寻者
        System.out.println(value);
    }

    // 删除指定 hash key 的value
    @Test
    public void test5(){
        String key = "zszxz";
        String item = "name";
        redisTemplate.opsForHash().delete(key, item);
    }

    // 是否存在 指定 hash 的key
    @Test
    public void test6(){
        String key = "zszxz";
        String item = "name";
        Boolean exist = redisTemplate.opsForHash().hasKey(key, item);
        // false
        System.out.println(exist);
    }

3.4 列表操作

 	@Autowired
    RedisTemplate<String,Object> redisTemplate;

    @Test
    public void test(){

    }

    // 列表右推入
    @Test
    public void test1(){
        String key = "zszxz";
        String value = "知识追寻者";
        redisTemplate.opsForList().rightPush(key, value);
    }

    // 列表左推入
    @Test
    public void test2(){
        String key = "zszxz";
        String value = "晴雨天";
        redisTemplate.opsForList().leftPush(key, value);
    }
    // 列表左弹出
    @Test
    public void test3(){
        String key = "zszxz";
        Object value = redisTemplate.opsForList().leftPop(key);
        // 晴雨天
        System.out.println(value);

    }
    // 列表右弹出
    @Test
    public void test4(){
        String key = "zszxz";
        Object value = redisTemplate.opsForList().rightPop(key);
        // 知识追寻者
        System.out.println(value);
    }

    // 将list右推入列表
    @Test
    public void test5(){
        ArrayList<Object> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        String key = "number";
        redisTemplate.opsForList().rightPushAll(key, list);
    }

    // 修改列表指定索引的值
    @Test
    public void test7(){
        String key = "number";
        int index = 0;
        int value = 666;
        redisTemplate.opsForList().set(key, index, value);
    }

    // 获取列表指定索引的值
    @Test
    public void test8(){
        String key = "number";
        int index = 0;
        Object value = redisTemplate.opsForList().index(key, index);
        // 666
        System.out.println(value);
    }

3.5 set 操作

  @Autowired
    RedisTemplate<String,Object> redisTemplate;

    // set 中存储值
    @Test
    public void test1(){
        String key = "zszxz";
        String value1 = "晴雨天";
        String value2 = "公众号:知识追寻者";
        redisTemplate.opsForSet().add(key, value1, value2);
    }

    // 从 set 中取值
    @Test
    public void test2(){
        String key = "zszxz";
        Set<Object> members = redisTemplate.opsForSet().members(key);
        // [晴雨天, 公众号:知识追寻者]
        System.out.println(members);
    }

    // 判定 set 中是否存在 key-value
    @Test
    public void test3(){
        String key = "zszxz";
        String value = "晴雨天";
        Boolean member = redisTemplate.opsForSet().isMember(key, value);
        // true
        System.out.println(member);
    }
posted @ 2020-05-10 22:19  知识追寻者  阅读(488)  评论(0编辑  收藏  举报