springboot 整合 Redis 方法二
方法一请参考之前博文 spring boot 整合 redis
自己的版本 java8 + redis3.0 + springboot 2.0.0
1 spring boot已经支持集成 redis,在 mvn 中只需添加依赖即可。pom 配置片段如下
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0..RELEASE</version>
</parent> <!-- 添加 redis 缓存支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
1.2 对 redis 进行配置,修改配置文件 application.properties
## Redis 配置
## Redis数据库索引(默认为0)
spring.redis.database=
## Redis服务器地址
spring.redis.host=127.0.0.1
## Redis服务器连接端口
spring.redis.port=
## Redis服务器连接密码(默认为空)
spring.redis.password=
2.1 创建 RedisConfig 配置类
@Configuration
public class RedisConfig { @Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory redisConnectionFactory){
StringRedisTemplate redisTemplate = new StringRedisTemplate(redisConnectionFactory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
/**
* 通用的序列化和反序列化设置
* ObjectMapper类是Jackson库的主要类。它提供一些功能将转换成Java对象匹配JSON结构,反之亦然。
*/
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
} }
2.2 创建 RedisService 泛型服务方法
@Service
public class RedisService <T> { @Autowired
private RedisTemplate redisTemplate; /**
* 一天有多少分钟,默认时间是一天
*/
private static final long MINUTES_OF_ONE_DAY = * ; /**
* 将 key,value 存放到redis数据库中,默认设置过期时间为一天
*
* @param key
* @param value
*/
public void set(String key, T value) {
set(key, value, MINUTES_OF_ONE_DAY);
} /**
* 将 key,value 存放到redis数据库中,设置过期时间单位是分钟
*
* @param key
* @param value
* @param expireTime 单位是秒
*/
public void set(String key, T value, long expireTime) {
ValueOperations<String, T> valueOperations = redisTemplate.opsForValue();
valueOperations.set(key,value,expireTime,TimeUnit.MINUTES);
} /**
* 判断 key 是否在 redis 数据库中
*
* @param key
* @return
*/
public boolean exists(final String key) {
return redisTemplate.hasKey(key);
} /**
* 获取 key 对应的字符串
* @param key
* @return
*/
public T get(String key) {
ValueOperations<String, T> valueOperations = redisTemplate.opsForValue();
return valueOperations.get(key);
} /**
* 获得 key 对应的键值,并更新缓存时间,时间长度为默认值
* @param key
* @return
*/
public T getAndUpdateTime(String key) {
T result = get(key);
if (result != null) {
set(key, result);
}
return result;
} /**
* 删除 key 对应的 value
* @param key
*/
public void delete(String key) {
redisTemplate.delete(key);
} }
3 接下来就可以使用 RedisService 进行缓存使用了。需要开启Redis服务,并没有设置密码
springboot 整合 Redis 方法二的更多相关文章
- 九、springboot整合redis二之缓冲配置
1.创建Cache配置类 @Configuration @EnableCaching public class RedisCacheConfig extends CachingConfigurerSu ...
- SpringBoot整合Redis、ApachSolr和SpringSession
SpringBoot整合Redis.ApachSolr和SpringSession 一.简介 SpringBoot自从问世以来,以其方便的配置受到了广大开发者的青睐.它提供了各种starter简化很多 ...
- Redis-基本概念、java操作redis、springboot整合redis,分布式缓存,分布式session管理等
NoSQL的引言 Redis数据库相关指令 Redis持久化相关机制 SpringBoot操作Redis Redis分布式缓存实现 Resis中主从复制架构和哨兵机制 Redis集群搭建 Redis实 ...
- SpringBoot整合Redis及Redis工具类撰写
SpringBoot整合Redis的博客很多,但是很多都不是我想要的结果.因为我只需要整合完成后,可以操作Redis就可以了,并不需要配合缓存相关的注解使用(如@Cacheable). ...
- springboot整合redis(注解形式)
springboot整合redis(注解形式) 准备工作 springboot通常整合redis,采用的是RedisTemplate的形式,除了这种形式以外,还有另外一种形式去整合,即采用spring ...
- springboot整合redis——redisTemplate的使用
一.概述 相关redis的概述,参见Nosql章节 redisTemplate的介绍,参考:http://blog.csdn.net/ruby_one/article/details/79141940 ...
- SpringBoot整合Redis集群
一.环境搭建 Redis集群环境搭建:https://www.cnblogs.com/zwcry/p/9174233.html 二.Spring整合Redis集群 1.pom.xml <proj ...
- SpringBoot整合Redis使用Restful风格实现CRUD功能
前言 本篇文章主要介绍的是SpringBoot整合Redis,使用Restful风格实现的CRUD功能. Redis 介绍 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-valu ...
- 【SpringBoot | Redis】SpringBoot整合Redis
SpringBoot整合Redis 1. pom.xml中引入Redis相关包 请注意,这里我们排除了lettuce驱动,采用了jedis驱动 <!-- redis的依赖 --> < ...
随机推荐
- spring boot 运行提示:Process finished with exit code 1
spring boot 运行提示:Process finished with exit code 1 经检查发现是由于在application.properties配置文件中将某些自定义配置项移除了, ...
- canvas转图片
<script> var canvas, context2D, canvasimg, context2Dimg ,fontSize = 100; window.onload ...
- Linux--U盘安装Ubuntu12.04[转]
http://www.cnblogs.com/plokmju/p/linux_installubuntu.html 最近一直在研究Android内核驱动开发的相关事宜,使用VMware虚拟机虽然可以更 ...
- 《Android开发艺术探索》图书勘误
第一章 在13页提到"系统仅仅在Activity异常终止的时候才会调用onSaveInstanceState与onRestoreInstanceState来储存和恢复数据.其它情况不会触发这 ...
- 算法笔记_202:第三届蓝桥杯软件类决赛真题(Java高职)
目录 1 填算式 2 提取子串 3 机器人行走 4 地址格式转换 5 排日程 前言:以下代码仅供参考,若有错误欢迎指正哦~ 1 填算式 [结果填空] (满分11分) 看这个算式: ☆☆☆ + ☆☆ ...
- Android学习笔记六:六大布局
六大界面布局方式包括: 线性布局(LinearLayout).帧布局(FrameLayout).表格布局(TableLayout).相对布局(RelativeLayout).绝对布局(Absolute ...
- Can't connect to MySQL server on 'ip' (13)
解决方法1.:setsebool -P httpd_can_network_connect_db=1 解决方法2.:修改/etc/selinux/config SELINUX=enforcing 为 ...
- 【超精简JS模版库/前端模板库】原理简析 和 XSS防范
使用jsp.php.asp或者后来的struts等等的朋友,不一定知道什么是模版,但一定很清楚这样的开发方式: <div class="m-carousel"> < ...
- ORA-01950: no privileges on tablespace xxx ORA-01950: 对表空间 'xxx'无权限
场景: 创建用户,在用户scheme下新建了一张表,插入数据时报错 ORA-01950: 对表空间 'xxx'无权限 创建用户 /*第1步:创建临时表空间 */ create temporary ta ...
- 神经网络:caffe特征可视化的代码例子
caffe特征可视化的代码例子 不少读者看了我前面两篇文章 总结一下用caffe跑图片数据的研究流程 deep learning实践经验总结2--准确率再次提升,到达0.8.再来总结一下 之后.想知道 ...