SpringBoot学习:整合Redis
项目下载地址:http://download.csdn.NET/detail/aqsunkai/9805821
pom.xml添加对redis的依赖:
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.4.5.RELEASE</version>
</dependency>
application.yml配置redis启动参数:
spring:
redis:
# Redis服务器地址
host: localhost
# Redis服务器连接端口
port: 6379
# Redis服务器连接密码(默认为空)
password:
pool:
# 连接池最大连接数(使用负值表示没有限制)
max-active: 8
# 连接池中的最大空闲连接
min-idle: 0
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1
# 连接池中的最小空闲连接
max-idle: 8
# 连接超时时间(毫秒)
timeout: 20
Redis的启动类如下:
package com.sun.configuration; import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.log4j.Logger;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.lang.reflect.Method; /**
* 项目启动加载redis
*/
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport implements EnvironmentAware { private final Logger logger = Logger.getLogger(RedisConfig.class); private RelaxedPropertyResolver propertyResolver; public void setEnvironment(Environment env) {
this.propertyResolver = new RelaxedPropertyResolver(env, "spring.redis.");
} @Bean("jedisPool")
public JedisPool redisPoolFactory() {
logger.info("redis地址:" + propertyResolver.getProperty("host") +
":" + propertyResolver.getProperty("port"));
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(Integer.valueOf(propertyResolver.getProperty("pool.max-idle")));
jedisPoolConfig.setMaxWaitMillis(Integer.valueOf(propertyResolver.getProperty("pool.max-wait"))); JedisPool jedisPool = new JedisPool(jedisPoolConfig, propertyResolver.getProperty("host"),
Integer.valueOf(propertyResolver.getProperty("port")),
Integer.valueOf(propertyResolver.getProperty("timeout")));
logger.info("jedisPool注入成功!!");
return jedisPool;
}
/**
* 生成key的策略
* 缓存注解没有配置key参数走此方法
* @return
*/
@Bean
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append(method.getName());
for (Object obj : params) {
sb.append(obj.toString());
}
return sb.toString();
}
};
}
/**
* RedisTemplate配置
* @param factory
* @return
*/
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
StringRedisTemplate template = new StringRedisTemplate(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
/**
* 管理缓存
*
* @param redisTemplate
* @return
*/
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
//设置缓存过期时间--一天
rcm.setDefaultExpiration(86400);//秒
return rcm;
} }
在ServiceImpl业务处理类上使用注解:
@Cacheable(value="common",key="'id_'+#id")
public User selectByPrimaryKey(Integer id) {
return userMapper.selectByPrimaryKey(id);
}
前台输入地址调用后台可以看到第一个打印出执行的sql,第二次不打印
项目启动前需要启动redis服务器,不知道的可以参考博客:http://blog.csdn.net/aqsunkai/article/details/51324176
SpringBoot学习:整合Redis的更多相关文章
- SpringBoot简单整合redis
Jedis和Lettuce Lettuce 和 Jedis 的定位都是Redis的client,所以他们当然可以直接连接redis server. Jedis在实现上是直接连接的redis serve ...
- SpringBoot学习(七)—— springboot快速整合Redis
目录 Redis缓存 简介 引入redis缓存 代码实战 Redis缓存 @ 简介 redis是一个高性能的key-value数据库 优势 性能强,适合高度的读写操作(读的速度是110000次/s,写 ...
- SpringBoot之整合Redis分析和实现-基于Spring Boot2.0.2版本
背景介绍 公司最近的新项目在进行技术框架升级,基于的Spring Boot的版本是2.0.2,整合Redis数据库.网上基于2.X版本的整个Redis少之又少,中间踩了不少坑,特此把整合过程记录,以供 ...
- springBoot(8)---整合redis
Springboot整合redis 步骤讲解 1.第一步jar导入: <dependency> <groupId>org.springframework.boot</gr ...
- 【SpringBoot】整合Redis实战
========================9.SpringBoot2.x整合Redis实战 ================================ 1.分布式缓存Redis介绍 简介: ...
- 完整SpringBoot Cache整合redis缓存(二)
缓存注解概念 名称 解释 Cache 缓存接口,定义缓存操作.实现有:RedisCache.EhCacheCache.ConcurrentMapCache等 CacheManager 缓存管理器,管理 ...
- SpringBoot中整合Redis、Ehcache使用配置切换 并且整合到Shiro中
在SpringBoot中Shiro缓存使用Redis.Ehcache实现的两种方式实例 SpringBoot 中配置redis作为session 缓存器. 让shiro引用 本文是建立在你是使用这sh ...
- SpringBoot之整合Redis
一.SpringBoot整合单机版Redis 1.在pom.xml文件中加入redis的依赖 <dependency> <groupId>org.springframework ...
- springboot下整合redis使用redisTemplate模板
pom <!-- 引入 redis 依赖 --> <dependency> <groupId>org.springframework.boot</groupI ...
- springboot+JPA 整合redis
1.导入redis依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifact ...
随机推荐
- Python Django 分页
Python Django 分页 http://www.360doc.com/content/14/0721/17/16044571_396090985.shtml
- web页面显示当前系统时间并定时刷新
function showCurrentDate(){ var today,hour,second,minute,year,month,date; var strDate ; today=new Da ...
- ADF系列-2.EO的高级属性
在上一篇博客 ADF系列-1.EO的各个属性初探 中介绍了EO的一些常用简单属性.本次将介绍EO中一些比较常用的一些高级属性 一.基于Sequence创建EO,一下介绍三种方式(以HR用户的Emplo ...
- 汇编试验十四:访问CMOS RAM
CMOS RAM 芯片的特征: 包含一个时钟和一个有128个存储单元的RAM存储器. 该芯片靠电池供电.所以,关机后其内部的时钟仍可正常工作,RAM中的信息不丢失. 128个字节的RAM中,内部时钟占 ...
- SQL 查询函数
1.abs函数取值(绝对值) select ABS(-20) 结果是20 2.ceiling函数取大于等于指定表达式的最小整数 select CEILING(40.5) 结果是41 3.floor函数 ...
- PAT——1055. 集体照 (比较comparable和comparator的区别)
拍集体照时队形很重要,这里对给定的N个人K排的队形设计排队规则如下: 每排人数为N/K(向下取整),多出来的人全部站在最后一排: 后排所有人的个子都不比前排任何人矮: 每排中最高者站中间(中间位置为m ...
- 如何编写及运行JS
JS也是一种脚本语言,他可以有两种方式在HTML页面进行引入,一种是外联,一种是内部. 外联JS的写法为: <script src="相对路径"></ ...
- C++笔记008:C++对C的扩展——命名空间 namespace基础
原创笔记,转载请注明出处! 点击[关注],关注也是一种美德~ 第一, 命名空间的意义 命名空间是ANSIC++引入的可以由用户命名的作用域,用来处理程序中常见的同名冲突. 我认识两位叫“A”的朋友,一 ...
- Filebeat使用模块收集日志
1.先决条件 在运行Filebeat模块之前: 安装并配置Elastic stack 完成Filebeat的安装 检查Elasticsearch和Kibana是否正在运行,以及Elasticsearc ...
- 安装psutil时提示缺少python.h头文件(作记录)
通过pip或者源码安装psutil,都会提示缺少python.h头文件,错误提示如下: ... psutil/_psutil_common.c:9:20: fatal error: Python.h: ...