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的依赖 --> < ...
随机推荐
- Spring4.2 集成ActiveMQ5.14
1:libs 2:web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app versio ...
- spring mvc mongoDb
http://www.cnblogs.com/dennisit/p/3372568.html 系统环境: 操作系统: windows xp 数 据 库: mongodb2.0.6 驱 动 包: S ...
- canvas语法糖
- Spring Boot 启动载入数据 CommandLineRunner
实际应用中,我们会有在项目服务启动的时候就去载入一些数据或做一些事情这种需求. 为了解决这种问题.Spring Boot 为我们提供了一个方法.通过实现接口 CommandLineRunner 来实现 ...
- Software development --daily scrum team
History[edit] Scrum was first defined as "a flexible, holistic product development strategy whe ...
- 如何处理Entity Framework中的DbUpdateConcurrencyException异常
1. Concurrency的作用 场景 有个修改用户的页面功能,我们有一条数据User, ID是1的这个User的年龄是20, 性别是female(数据库中的原始数据) 正确的该User的年龄是25 ...
- codevs 1862 最长公共子序列(求最长公共子序列长度并统计最长公共子序列的个数)
题目描述 Description 字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字符序列X=“x0,x1,…,xm-1”,序列Y ...
- Tomcat无法访问中文路径的解决办法
来源于:http://sccassiel.blog.51cto.com/5398709/1141821/ 修改tomcat下的conf/server.xml文件下的 <Connector por ...
- (原)luarocks install 提示 failed fetching manifest
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6400169.html 参考网址: https://github.com/torch/torch7/is ...
- apache2.2 虚拟主机配置(转)
转自:http://blog.csdn.net/zm2714/article/details/8351342 一.改动httpd.conf 打开appserv的安装文件夹,找到httpd.conf文件 ...