Spring Boot中整合Redis - Go语言中文社区

Spring Boot中整合Redis


我相信小伙伴儿们在开发中经常会用到缓存,比如说将用户的登录信息存到缓存中,还有我们也可以根据登录的用户所具有的角色操作不同的菜单项等等这些信息我们都可以放到缓存中去,用户下一次发起请求就可以不用去请求数据库了,而是先去缓存中去找,找到了直接显示对应的主页,找不到才去数据库中找,这在并发访问时具有很大的优势,降低了数据库的压力以及数据响应速度等,下面我们来整理下在spring boot中使用redis

我这里的redis是安装在docker容器中的,很方便,一键就能部署,后面说docker的内容
使用 Java 操作 Redis 的方案很多,Jedis 是目前较为流行的一种方案,除了 Jedis ,还有很多其他解决方案,如下:
在这里插入图片描述
除了这些方案之外,还有一个使用也相当多的方案,就是 Spring Data Redis。

在传统的 SSM 中,需要开发者自己来配置 Spring Data Redis ,这个配置比较繁琐,主要配置 3 个东西:连接池、连接器信息以及 key 和 value 的序列化方案。

在 Spring Boot 中,默认集成的 Redis 就是 Spring Data Redis,默认底层的连接池使用了 lettuce ,可以自行修改为自己的熟悉的,例如 Jedis。

Spring Data Redis 针对 Redis 提供了非常方便的操作模板 RedisTemplate 。这是 Spring Data 擅长的事情,那么接下来我们就来看看 Spring Boot 中 Spring Data Redis 的具体用法。

创建项目

创建项目,引入redis依赖
在这里插入图片描述
创建完成后还需要引入commons-pool2和spring-security-core,因为在spring boot2.1.5以后的版本,如果要远程访问redis,必须要引入spring-security-core依赖,不然就访问不了远程redis

引入依赖

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

接下来就需要在application.properties中配置redis的连接信息以及连接池的配置

redis的配置

spring.redis.database=0 redis的库,默认是0号库,0-15一共16个库
spring.redis.host=10.0.0.13 redis的地址
spring.redis.port=6379   redis的端口
#spring.redis.password=123456 我这里没有设置密码
#spring.redis.timeout=5000ms 超时时间

#配置连接池信息
#spring.redis.lettuce.pool.max-wait=
#spring.redis.lettuce.pool.max-active=
#spring.redis.lettuce.pool.max-idle=
#spring.redis.lettuce.pool.min-idle=
#spring.redis.lettuce.shutdown-timeout=
#spring.redis.lettuce.pool.time-between-eviction-runs=

我们也可以通过redis的桌面连接工具来测试连接
在这里插入图片描述
我们在引入了spring -data-redis依赖以后,在application.properties配置中配置了redis相关的信息,此时自动化配置就会生效,我们可以来看看redis的自动化配置类的源码

Redis的自动化配置类解释

@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })
public class RedisAutoConfiguration {

	@Bean
	@ConditionalOnMissingBean(name = "redisTemplate")
	public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)
			throws UnknownHostException {
		RedisTemplate<Object, Object> template = new RedisTemplate<>();
		template.setConnectionFactory(redisConnectionFactory);
		return template;
	}

	@Bean
	@ConditionalOnMissingBean
	public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory)
			throws UnknownHostException {
		StringRedisTemplate template = new StringRedisTemplate();
		template.setConnectionFactory(redisConnectionFactory);
		return template;
	}

}

首先@Configuration标记这是一个配置类,同时@ConditionalOnClass表示该配置在 RedisOperations 存在的情况下才会生效(即项目中引入了 Spring Data Redis),所以说只要你引入了Spring Data Redis,下面的这一套东西就会生效
然后导入在 application.properties 中配置的属性
然后再导入连接池信息(如果存在的话)
最后,提供了两个 Bean ,RedisTemplate 和 StringRedisTemplate ,其中 StringRedisTemplate 是 RedisTemplate 的子类,两个的方法基本一致,不同之处主要体现在操作的数据类型不同,RedisTemplate 中的两个泛型都是 Object ,意味者存储的 key 和 value 都可以是一个对象,而 StringRedisTemplate 的 两个泛型都是 String ,意味者 StringRedisTemplate 的 key 和 value 都只能是字符串。如果开发者没有提供相关的 Bean ,这两个配置就会生效,否则不会生效。

下面就来看看如何使用redis吧

redis的使用

可以直接在 Controller 中注入 StringRedisTemplate 或者 RedisTemplate 来使用:

package com.zhouym.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 〈〉
 *
 * @author zhouym
 * @create 2019/8/28
 * @since 1.0.0
 */
@RestController
public class RedisController {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @GetMapping("/get")
    public void test(){
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        String name = ops.get("name");
        System.out.println(name);
    }
}

我们这里默认使用get发起请求,获取redis中的值,在控制台输出
在这里插入图片描述
Redis 中的数据操作,大体上来说,可以分为两种:

针对 key 的操作,相关的方法就在 RedisTemplate 中
针对具体数据类型的操作,相关的方法需要首先获取对应的数据类型,获取相应数据类型的操作方法是 opsForXXX
调用该方法就可以将数据存储到 Redis 中去了,如下:
在这里插入图片描述
k1 前面的字符是由于使用了 RedisTemplate 导致的,RedisTemplate 对 key 进行序列化之后的结果。

RedisTemplate 中,key 默认的序列化方案是 JdkSerializationRedisSerializer 。

而在 StringRedisTemplate 中,key 默认的序列化方案是 StringRedisSerializer ,因此,如果使用 StringRedisTemplate ,默认情况下 key 前面不会有前缀。

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/zhouym_/article/details/100119746
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢