Spring Boot (23) Lettuce Redis

Spring Boot除了支持常见的ORM框架外,更是对常用的中间件提供了非常好的封装,随着SpringBoot2.x的到来,支持的组件也越来越丰富,也越来越成熟,其中对Redis的支持不仅仅是它丰富的API,更是替换掉了底层Jedis的依赖,取而代之的是Lettuce。

Redis

  Redis是一个开源的使用ANSI c语言编写、支持网络、可基于内存也可持久化的日执行、key-value数据库,并提供多种语言的API,相比memcached它支持更多类型存储(字符串、哈希、集合、有续集合、列表),同时Redis是线程安全的。

Lettuce

  Lettuce和Jedis都是连接Redis Server的客户端程序,Jedis在实现上是直连redis server,多线程环境下非线程安全,除非使用连接池,为每个Jedis势力增加物理连接。Lettuce基于Netty的势力连接,可以再多个线程间并发访问,且线程安全,满足多线程环境下的并发访问,同时它是可伸缩的设计,一个连接势力不够的情况也可以按需增加连接实例。

导入依赖

  在pom.xml中引入spring-boot-starter-data-redis

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

属性配置

  在application.yml中配置如下内容

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/david2018_db?characterEncoding=utf8
    username: root
    password: 1234
  redis:
    host: 10.211.55.5 #redis服务器地址
    timeout: 10000 #超时时间
    database: 0 #0-15 16个库 默认0
    lettuce:
      pool:
        max-active: 8 #最大连接数
        max-wait: -1 #默认-1 最大连接阻塞等待时间
        max-idle: 8 #最大空闲连接 默认8
        min-idle: 0 #最小空闲连接

实体类

package com.spring.boot.bean;

import java.io.Serializable;

public class User implements Serializable {
    private Integer userId;
    private String userName;

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}

自定义Template

  默认情况下的模板只能支持RedisTemplate<String,String>,只能存字符串。这时需要自定义模板,当自定义模板后又想存储String字符串时,可以使用StringRedisTemplate的方式,他们俩并不冲突。

package com.spring.boot.utils;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.io.Serializable;

@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisCacheAutoConfiguration {

    @Bean
    public RedisTemplate<String,Serializable> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory){
        RedisTemplate<String,Serializable> template = new RedisTemplate<String,Serializable>();
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}

测试redis

    @GetMapping("/redis")
    public String redis(){
        //字符串
        stringRedisTemplate.opsForValue().set("rediskey","redisvalue");
        String rediskey = stringRedisTemplate.opsForValue().get("rediskey");
        System.out.println(rediskey);
        //对象
        User user = new User(1,"username");
        redisCacheTemplate.opsForValue().set("user",user);

        User getUser = (User) redisCacheTemplate.opsForValue().get("user");
        System.out.println(getUser.getUserName());
        return "redis";
    }

下列就是redis其他类型的对应操作方式:

  opsForValue:对应String字符串

  opsForZSet:对应ZSet有序集合

  opsForHash:对应Hash哈希

  opsForList:对应List列表

  opsForSet:对应Set集合

  opsForGeo:对应GEO地理位置

原文地址:https://www.cnblogs.com/baidawei/p/9156410.html