iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >springboot2.5.0和redis整合配置详解
  • 381
分享到

springboot2.5.0和redis整合配置详解

2024-04-02 19:04:59 381人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录基本概况为什么使用缓存Redis缓存下载Redis1. pom添加依赖2. application.properties 配置文件3. RedisConfig.java 配置类4

基本概况

为什么使用缓存

缓存是在内存中存储的数据备份,当数据没有发生本质变化时
就可以直接从内存中查询数据,而不用去数据库查询(在磁盘中)
CPU读取内存的速度要比读取磁盘快,可以提高效率

Redis缓存

Remote Dictionnary Server(远程数据服务),是一款内存高速缓存数据库
五种常用数据类型: String(字符串)、List(列表)、Set(集合)、Hash(散列)、ZSet(有序集合)
可持久化:一边运行,一边向硬盘备份一份,防止断电等偶然情况,导致内存中数据丢失

下载Redis

链接: https://pan.baidu.com/s/1BMt4cIxjKTtyL3T0_iSC2w 提取码: rkne

1. pom添加依赖


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

2. application.properties 配置文件


#===========Redis配置===========
# Redis数据库索引(默认为0)  
spring.redis.database=0
# Redis服务器地址  
spring.redis.host=127.0.0.1
# Redis服务器连接端口  
spring.redis.port=6379
# Redis服务器连接密码
spring.redis.passWord=root
# 连接池最大连接数(使用负值表示没有限制)  
spring.redis.pool.max-active=200
# 连接池最大阻塞等待时间(使用负值表示没有限制)  
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接 
spring.redis.pool.max-idle=10
# 连接池中的最小空闲连接  
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=2000ms
spring.redis.jedis.pool.max-wait=-1ms
#===========Redis配置===========

3. RedisConfig.java 配置类


package org.fh.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JSONRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.annotation.JsonAutoDetect;


@Configuration
public class RedisConfig {

	@Bean
	@SuppressWarnings("all")
	public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
		RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
		template.setConnectionFactory(factory);
		Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
		ObjectMapper om = new ObjectMapper();
		om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
		om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance , ObjectMapper.DefaultTyping.NON_FINAL);
		jackson2JsonRedisSerializer.setObjectMapper(om);
		StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
		// key采用String的序列化方式
		template.seTKEySerializer(stringRedisSerializer);
		// hash的key也采用String的序列化方式
		template.setHashKeySerializer(stringRedisSerializer);
		// value序列化方式采用jackson
		template.setValueSerializer(jackson2JsonRedisSerializer);
		// hash的value序列化方式采用jackson
		template.setHashValueSerializer(jackson2JsonRedisSerializer);
		template.afterPropertiesSet();
		return template;
	}

}

4. 调用redis


 @Autowired
   private RedisTemplate<String, Object> redisTemplate;

	
	public Object get(String key) {
		return key == null ? null : redisTemplate.opsForValue().get(key);
	}

	
	public boolean set(String key, Object value) {
		try {
			redisTemplate.opsForValue().set(key, value);
			return true;
		} catch (Exception e) {
			//e.printStackTrace();
			return false;
		}
	}

以上就是SpringBoot2.5.0 整合 redis 配置详解的详细内容,更多关于springboot2.5.0 整合 redis 的资料请关注编程网其它相关文章!

--结束END--

本文标题: springboot2.5.0和redis整合配置详解

本文链接: https://www.lsjlt.com/news/127425.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作