SpringBoot(十二)-- 整合Redis
1.pom依赖
<!-- 添加redis支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.application.properties配置
#redis数据库名称 从0到15,默认为db0
spring.redis.database=1
#redis服务器名称
spring.redis.host=127.0.0.1
#redis服务器密码
#spring.redis.password=123456
#redis服务器连接端口号
spring.redis.port=6379
#redis连接池设置
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
#spring.redis.sentinel.master=
#spring.redis.sentinel.nodes=
spring.redis.timeout=60000
3.将 五种数据类型 注入到 Srping中
package com.xsjt.redis;
import org.springframework.beans.factory.annotation.Autowired;
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.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* ClassName:RedisConfig Date: 2017年11月14日 下午3:39:34
* 将 五种数据类型 注入到 Spring容器中
* @author Joe
* @version
* @since JDK 1.8
* 参考地址:https://www.cnblogs.com/skyessay/p/6485187.html
*/
@Configuration
public class RedisConfig { // 注入 RedisConnectionFactory
@Autowired
private RedisConnectionFactory redisConnectionFactory; @Bean
public RedisTemplate<String, Object> functionDomainRedisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
initDomainRedisTemplate(redisTemplate, redisConnectionFactory);
return redisTemplate;
} /**
* 设置数据存入 redis 的序列化方式
* @param redisTemplate
* @param factory
*/
private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) {
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.setConnectionFactory(factory);
} /**
* 实例化 HashOperations 对象,可以使用 Hash 类型操作
* @param redisTemplate
* @return
*/
@Bean
public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForHash();
} /**
* 实例化 ValueOperations 对象,可以使用 String 操作
* @param redisTemplate
* @return
*/
@Bean
public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForValue();
} /**
* 实例化 ListOperations 对象,可以使用 List 操作
* @param redisTemplate
* @return
*/
@Bean
public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForList();
} /**
* 实例化 SetOperations 对象,可以使用 Set 操作
* @param redisTemplate
* @return
*/
@Bean
public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForSet();
} /**
* 实例化 ZSetOperations 对象,可以使用 ZSet 操作
* @param redisTemplate
* @return
*/
@Bean
public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForZSet();
}
}
4.封装String数据类型的方法
package com.xsjt.redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
/**
* ClassName: StringRedisUtil
* String 数据类型
* date: 2017年11月14日 下午8:15:07
* @author Joe
* @version
* @since JDK 1.8
*/
@Component("stringRedis")
public class StringRedisUtil { @Autowired
private ValueOperations<String, Object> redisTemplate; /**
* set:(保存数据).
* @author Joe
* Date:2017年11月14日下午8:15:01
* @param key
* @param value
*/
public void set(String key, String value){
redisTemplate.set(key, value);
} /**
* get:(得到数据).
* @author Joe
* Date:2017年11月14日下午8:15:38
* @param key
* @return
*/
public Object get(String key) {
return redisTemplate.get(key);
} // 可自行扩展其他方法
}
5.封装Hash数据类型的方法
package com.xsjt.redis;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component; /**
* ClassName:HashRedisUtil Date: 2017年11月14日 下午8:17:47
* Hash 数据类型
* @author Joe
* @version
* @param <T>
* @since JDK 1.8
*/
@Component("hashRedis")
public class HashRedisUtil<T> { @Autowired
protected RedisTemplate<String, Object> redisTemplate;
@Resource
protected HashOperations<String, String, Object> hashOperations; /**
* put:(添加).
* @param key
* @param hashKey
* @param doamin value
* @param expire 过期时间(单位:秒),传入 -1 时表示不设置过期时间
*/
public void put(String key, String hashKey, T doamin, long expire) {
hashOperations.put(key, hashKey, doamin);
if (expire != -1) {
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
} /**
* remove:( 删除).
* @param key
* @param hashKey
*/
public void remove(String key, String hashKey) {
hashOperations.delete(key, hashKey);
} /**
* get:(查询).
* @param key
* @param hashKey
* @return
*/
public Object get(String key, String hashKey) {
return hashOperations.get(key, hashKey);
} /**
* getAll:(获取当前redis库下所有对象).
* @param key
* @return
*/
public List<Object> getAll(String key) {
return hashOperations.values(key);
} /**
* getKeys:(查询查询当前redis库下所有key).
* @param key
* @return
*/
public Set<String> getKeys(String key) {
return hashOperations.keys(key);
} /**
* isKeyExists:(判断key是否存在redis中).
* @param key
* @param hashKey
* @return
*/
public boolean isKeyExists(String key, String hashKey) {
return hashOperations.hasKey(key, hashKey);
} /**
* count:(查询当前key下缓存数量).
* @param key
* @return
*/
public long count(String key) {
return hashOperations.size(key);
}
}
6.测试类
package com.xsjt.redis;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* ClassName:TestRedis
* Date: 2017年11月14日 下午8:09:54
* @author Joe
* @version
* @since JDK 1.8
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestRedis { /********************************测试String***********************************/ @Autowired
private StringRedisUtil stringRedis; @Test
public void setString() {
stringRedis.set("name", "张三");
} @Test
public void getString() {
Object value = stringRedis.get("name");
System.out.println("value=" + value);
} /**********************************测试Hash************************************/ @Autowired
private HashRedisUtil<Object> hashRedisUtil; @Test
public void setHash() {
hashRedisUtil.put("user", "userName", new Integer(6868), 5);
} @Test
public void getHash() {
Integer a = (Integer) hashRedisUtil.get("user", "userName");
System.out.println("a==" + a);
}
}
7.源码下载
https://gitee.com/xbq168/spring-boot-learn
SpringBoot(十二)-- 整合Redis的更多相关文章
- SpringBoot进阶教程(二十九)整合Redis 发布订阅
SUBSCRIBE, UNSUBSCRIBE 和 PUBLISH 实现了 发布/订阅消息范例,发送者 (publishers) 不用编程就可以向特定的接受者发送消息 (subscribers). Ra ...
- SpringBoot进阶教程(二十八)整合Redis事物
Redis默认情况下,事务支持被禁用,必须通过设置setEnableTransactionSupport(true)为使用中的每个redistplate显式启用.这样做会强制将当前重新连接绑定到触发m ...
- SpringBoot进阶教程(二十六)整合Redis之共享Session
集群现在越来越常见,当我们项目搭建了集群,就会产生session共享问题.因为session是保存在服务器上面的.那么解决这一问题,大致有三个方案,1.通过nginx的负载均衡其中一种ip绑定来实现( ...
- SpringBoot进阶教程(二十五)整合Redis之@Cacheable、@CachePut、@CacheEvict的应用
在上一篇文章(<SpringBoot(二十四)整合Redis>)中,已经实现了Spring Boot对Redis的整合,既然已经讲到Cache了,今天就介绍介绍缓存注解.各家互联网产品现在 ...
- SpringBoot进阶教程(二十四)整合Redis
缓存现在几乎是所有中大型网站都在用的必杀技,合理的利用缓存不仅能够提升网站访问速度,还能大大降低数据库的压力.Redis提供了键过期功能,也提供了灵活的键淘汰策略,所以,现在Redis用在缓存的场合非 ...
- Activiti7整合SpringBoot(十二)
1 SpringBoot 整合 Activiti7 的配置 为了能够实现 SpringBoot 与 Activiti7 整合开发,首先我们要引入相关的依赖支持.所以,我们在工程的 pom.xml 文件 ...
- Spring Boot (二) 整合 Redis
前言 本文将会基于 springboot 2.1.8.RELEASE 简单整合 Redis ,适合新手小白入门 Spring Boot 整合 Redis 入门 1.pom.xml 中引入 redis ...
- 【快学springboot】11.整合redis实现session共享
前言 这里都是基于前面的项目基础上的.springboot整合redis非常的方便,这也是springboot的宗旨,简化配置.这篇文章就教大家如何使用springboot整合redis来实现sess ...
- redis之(十二)redis数据的持久化
[一]redis的数据为什么要持久化 --->redis的存取数据性能高,是由于将所有数据都存储在内存中.当redis重启的时候,存储在内存中的数据就容易丢失. --->把redis作为数 ...
- springboot(十二)-分布式锁(redis)
什么是分布式锁? 要介绍分布式锁,首先要提到与分布式锁相对应的是线程锁.进程锁. 线程锁:主要用来给方法.代码块加锁.当某个方法或代码使用锁,在同一时刻仅有一个线程执行该方法或该代码段.线程锁只在同一 ...
随机推荐
- [maven] settings 文件 本地maven仓库
<?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Soft ...
- Sword pcre库使用
#include <stdlib.h> #include <string.h> #include "regularhelper.h" #include &q ...
- Java虚拟机(JVM)体系结构概述及各种性能参数优化总结
转自:http://blog.csdn.net/zhongwen7710/article/details/39213377 第一部分:相关的概念 数据类型 Java虚拟机中,数据类型可以分为两类:基本 ...
- “System Volume Information”文件夹里的NTFS木马(安全问题)
病毒保护伞 原因:由于NTFS的分区里该目录只有SYSTEM权限,导致杀毒软件没有权限查杀藏匿于该目录的病毒.(现在大多数软件都能查杀) 解决方案:阻止“System Volume Informati ...
- android sqlite blob
BOLB表示二进制大对象,这种数据类型通过用来保存图片,图象,视频等. 使用场景: http://blog.sina.com.cn/s/blog_8cfbb99201012oqn.html publi ...
- Java如何格式化AM-PM格式的时间?
在JAVA中,如何格式化AM-PM格式的时间? 该示例使用SimpleDateFormat(“HH-mm-ss a”)构造函数和SimpleDateFormat类的sdf.format(date)方法 ...
- JDBC事务提交/回滚实例
以下是使用事务教程中描述的提交和回滚的代码示例. 此示例代码是基于前面章节中完成的环境和数据库设置编写的. 复制并将以下示例代码保存到:CommitAndRollback.java 中,编译并运行如下 ...
- (转)TCP连接异常断开检测
TCP是一种面向连接的协议,连接的建立和断开需要通过收发相应的分节来实现.某些时候,由于网络的故障或是一方主机的突然崩溃而另一方无法检测到,以致始终保持着不存在的连接.下面介绍一种方法来检测这种异常断 ...
- 某软件大赛C#版考题整理——【多选题】
二.多选题(20小题共40.0分) 1. 下列选项中,属于HTML按钮元素的是:(). A. <input name="btn" type="button" ...
- mysql 中 时间和日期函数
From: http://www.cnblogs.com/redfox241/archive/2009/07/23/1529092.html 一.MySQL 获得当前日期时间 函数 1.1 获得当前日 ...