【第二章】SpringBoot2.x集成Redis

上一篇 【第一章】Centos7.x安装Redis , 以及redis常用命令使用.


Redis简介

redis是一个高性能的键(Key)值(Value)数据库. 是当前最热门的的的NoSql数据库之一.

Redis优势

  1. 基于内存,速度快
  2. 数据持久化的支持,可以将内存中的数据异步写入到硬盘中.
  3. 数据结构丰富
  • string<字符串>
  • list<链表>
  • set<集合>
  • zset<有序集合>
  • Hash<哈希类型>

SpringBoot2.x集成Redis

SpringBoot1.x中 spring-boot-starter-data-redis 默认客户端是Jedis
SpringBoot2.x中 默认客户端是Lettuce

引入依赖

采用commons-pool2 作为redis连接池

<!-- redis starter -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- lettuce pool 缓存连接池 -->
<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-pool2</artifactId>
</dependency>
application.yml配置
spring:
  redis:
    # Redis数据库索引(默认为0)
    database: 0
    # Redis服务器连接端口
    port: 6379
    # Redis服务器地址
    host: 127.0.0.1
    # Redis服务器连接密码(默认为空)
    password: 123456
    # 连接超时时间(毫秒)
    timeout: 5000
    lettuce:
      # 关闭超时时间
      shutdown-timeout: 100
      pool:
        # 连接池最大连接数(使用负值表示没有限制)
        max-active: 8
        # 连接池最大阻塞等待时间(使用负值表示没有限制)
        max-wait: 10000
        # 连接池中的最大空闲连接
        max-idle: 8
        # 连接池中的最小空闲连接
        min-idle: 0
RedisConfig配置key,value序列化
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.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @Title: RedisCacheConfig
 * @Description: TODO(reids缓存配置)
 * @Author 爱飘de小子
 * @Date 2019/6/5下午 11:36
 */
@Configuration
public class RedisConfig {
   
    
    /**
     * RedisTemplate配置
     * 注意: 注入的是LettuceConnectionFactory
     */
    @Bean
    public RedisTemplate<String,Object> redisTemplate(LettuceConnectionFactory factory){
   
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        redisTemplate.setKeySerializer(keySerializer());
        redisTemplate.setHashKeySerializer(keySerializer());
        redisTemplate.setValueSerializer(valueSerializer());
        redisTemplate.setHashValueSerializer(valueSerializer());
        return redisTemplate;
    }

    /**
     * key序列化
     */
    private RedisSerializer<String> keySerializer() {
   
        return new StringRedisSerializer();
    }

    /**
     * value序列化
     */
    private RedisSerializer<Object> valueSerializer() {
   
        return new GenericJackson2JsonRedisSerializer();
    }
}

至此,springboot已经集成redis. 项目中可以使用RedisTemplate泛型类工具类操作redis数据库.




附: redis操作工具类(基于String,list,Map操作)

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @Title: RedisService
 * @Description: TODO(reids工具类)
 * @Author 爱飘de小子
 * @Date 2019/6/5
 */
@Service
public class RedisService {
   

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    
    // ========================&#
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值