失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > SpringBoot学习——springboot整合Redis实现数据缓存

SpringBoot学习——springboot整合Redis实现数据缓存

时间:2022-07-28 13:39:58

相关推荐

SpringBoot学习——springboot整合Redis实现数据缓存

版权声明:转载请注明来源 /qq_24598601/article/details/89284040

SpringBoot 整合 Redis 数据库实现数据缓存的本质是整合 Redis 数据库,通过对需要“缓存”的数据存入 Redis 数据库中,下次使用时先从 Redis 中获取,Redis 中没有再从数据库中获取,这样就实现了 Redis 做数据缓存。

按照惯例,下面一步一步的实现 Springboot 整合 Redis 来存储数据,读取数据。

一、POM 文件添加 Redis 环境

首页第一步还是在项目添加 Redis 的环境, Jedis。

<!-- Redis -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-redis</artifactId>

</dependency>

<dependency>

<groupId>redis.clients</groupId>

<artifactId>jedis</artifactId>

</dependency>

二、在 application.properties 添加自定义配置的 Redis 参数

第二步需要将一些 Redis 的配置信息配置到 application.properties 文件中

spring.redis.pool.max-idle=10

spring.redis.pool.min-idle=5

spring.redis.pool.max-total=20

spring.redis.hostName=127.0.0.1

spring.redis.port=6379

使用 yml 的配置如下:

spring:

### Redis Configuration

redis:

pool:

max-idle: 10

min-idle: 5

max-total: 20

hostName: 127.0.0.1

port: 6379

这里的配置信息模仿 datasource 的配置,除了键(max-idle、min-idle、hostName、port 等)的名称必须和所对应的对象属性一致外,其他可根据自己喜好命名,例如下面配置:

springboot.redis.pool.max-idle=10

springboot.redis.pool.min-idle=5

springboot.redis.pool.max-total=20

springboot.redis.hostName=127.0.0.1

springboot.redis.port=6379

1

2

3

4

5

这样也是可以的,其实质就是这里的这些 Redis 参数是自定义的参数,SpringBoot 没有对其有规定,关于自定义参数的获取可参考文章:SpringBoot学习——yml 文件中自定义参数解析对象 ,properties 文件也是一样的方式获取。

三、编写一个 RedisConfig 注册到 Spring 容器

编写一个 RedisConfig 注册到 Spring 容器,主要步骤有:

创建并注册 JedisPoolConfig 对象,通过 @ConfigurationProperties 来获取配置文件中的连接池配置信息;

创建并注册 JedisConnectionFactory 对象并注入上一步的 JedisPoolConfig 对象,同时使用 @ConfigurationProperties 来获取配置文件中的连接配置信息;

创建并注册 RedisTemplate 对象并注入上一步的 JedisConnectionFactory 对象;

通过 @Autowired private RedisTemplate<String, Object> redisTemplate; 使用 RedisTemplate 对象;

具体的 RedisConfig 对象代码如下:

package com.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.data.redis.serializer.StringRedisSerializer;

import redis.clients.jedis.JedisPoolConfig;

/**

* 描述:Redis Config 注册

* @author lytao123

*/

@Configuration

public class RedisConfig {

/**

* 1.创建 JedisPoolConfig 对象。在该对象中完成一些连接池的配置

*/

@Bean

@ConfigurationProperties(prefix="spring.redis.pool")

public JedisPoolConfig jedisPoolConfig() {

JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();

return jedisPoolConfig;

}

/**

* 2.创建 JedisConnectionFactory:配置 redis 连接信息

*/

@Bean

@ConfigurationProperties(prefix="spring.redis")

public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {

JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(jedisPoolConfig);

return jedisConnectionFactory;

}

/**

* 3.创建 RedisTemplate:用于执行 Redis 操作的方法

*/

@Bean

public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory jedisConnectionFactory) {

RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();

// 关联

redisTemplate.setConnectionFactory(jedisConnectionFactory);

// 为 key 设置序列化器

redisTemplate.setKeySerializer(new StringRedisSerializer());

// 为 value 设置序列化器

redisTemplate.setValueSerializer(new StringRedisSerializer());

return redisTemplate;

}

}

四、使用 RedisTemplate

第四步就是使用 RedisTemplate ,使用方法如下所示,但不局限于下面的使用方式,可以是数据库的各种使用方式。

package com.controller;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.bean.Users;

/**

* @Description 整合 Redis 测试Controller

* @author 欧阳

* @since 4月13日 下午3:20:08

* @version V1.0

*/

@RestController

public class RedisController {

@Autowired

private RedisTemplate<String, Object> redisTemplate;

@RequestMapping("/redishandle")

public String redishandle() {

//添加字符串

redisTemplate.opsForValue().set("author", "欧阳");

//获取字符串

String value = (String)redisTemplate.opsForValue().get("author");

System.out.println("author = " + value);

//添加对象

//重新设置序列化器

redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());

redisTemplate.opsForValue().set("users", new Users("1" , "张三"));

//获取对象

//重新设置序列化器

redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());

Users user = (Users)redisTemplate.opsForValue().get("users");

System.out.println(user);

//以json格式存储对象

//重新设置序列化器

redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class));

redisTemplate.opsForValue().set("usersJson", new Users("2" , "李四"));

//以json格式获取对象

//重新设置序列化器

redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Users.class));

user = (Users)redisTemplate.opsForValue().get("usersJson");

System.out.println(user);

return "home";

}

}

例如上述的具体使用方法是运行启动类,访问 http://localhost:8080/redishandle 后, Redis 数据库中将存入三个数据,其key分别是 author、users、usersJson。使用命令 get key 可验证 Redis 中是否有这三个 key 的数据。

--------------------- ---------------------------------------------

我就想问一下,RedisTemplate,这个已经封装了还要手动实例化它,干啥?

作者:lytao123

来源:CSDN

原文:/qq_24598601/article/details/89284040

版权声明:本文为博主原创文章,转载请附上博文链接!

如果觉得《SpringBoot学习——springboot整合Redis实现数据缓存》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。