springboot2 redis
2021-07-11 03:06
阅读:645
标签:image context 代码 prope 超时 缓存 value efault show
本项目使用jar包:
parent>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-parentartifactId>
version>2.0.4.RELEASEversion>
relativePath/>
parent>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-data-redisartifactId>
dependency>
- 在application.properties中加入redis的配置信息
#redis spring.redis.host=127.0.0.1 spring.redis.port=6379 # 数据库连接超时时间,2.0 中该参数的类型为Duration,这里在配置的时候需要指明单位 spring.redis.timeout=60s # 连接池配置,2.0中直接使用jedis或者lettuce配置连接池 # 最大活跃连接数,负数为不限制 spring.redis.lettuce.pool.max-active=500 # 等待可用连接的最大时间,负数为不限制 spring.redis.lettuce.pool.max-wait=-1ms # 最大空闲连接数 spring.redis.lettuce.pool.max-idle=100 # 最小空闲连接数 spring.redis.lettuce.pool.min-idle=20
- 新增RedisConfig.java文件,其中注释掉的两行代码不能打开。打开的话会使value使序例化,序例化了以后会跟后面要使用的@Cacheable不一致,导致取不到值
package com.chenyingjun.springboot2.config; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.cache.annotation.EnableCaching; 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.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; /** * RedisConfig工具类配置 * * @author chenyingjun * @since 1.0 * @version 2018年8月15日 chenyingjun */ @Configuration @EnableCaching public class RedisConfig { /** * RedisTemplate配置 */ @Bean public RedisTemplate
redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) { // 设置序列化 Jackson2JsonRedisSerializer - 在service层中加入@Cacheable(key="‘userId_‘+#id", value="user")来缓存信息,这样println只会打印一次
@Cacheable(key="‘userId_‘+#id", value="user") public SystemUserVo info(String id) { System.out.println("-------------------------------------------------------"); SystemUserVo user = systemUserMapper.info(id); return user; }
- 缓存后数据如下图
- 使用RedisTemplate和StringRedisTemplate来获取缓存中的值,其中obj1也已经获取到了redis中的数据。效果如下
springboot2 redis
标签:image context 代码 prope 超时 缓存 value efault show
原文地址:https://www.cnblogs.com/cnJun/p/9552411.html
上一篇:Python 编码规范
下一篇:Python环境搭建
文章来自:搜素材网的编程语言模块,转载请注明文章出处。
文章标题:springboot2 redis
文章链接:http://soscw.com/index.php/essay/103521.html
文章标题:springboot2 redis
文章链接:http://soscw.com/index.php/essay/103521.html
评论
亲,登录后才可以留言!