前言

Redis 1 是一个由Salvatore Sanfilippo写的key-value存储系统。

edis是一个开源的使用ANSI C语言编写、遵守BSD协议、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。

通常被称为数据结构服务器,因为值(value)可以是 字符串(String), 哈希(Map), 列表(list), 集合(sets) 和 有序集合(sorted sets)等类型。

正文

引入依赖

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

需要注意的是,上面是 Spring Boot 1.5 版本后的名称,1.5版本前是 spring-boot-starter-redis

参数配置

spring:
redis:
host: 192.168.19.200 # host ,默认 localhost
port: 6379 # 端口号,默认6379
pool:
# 设置都是默认值,可以按需求设计
max-active: 8 # 可用连接实例的最大数目,默认值为8;如果赋值为-1,则表示不限制;
max-idle: 8 # 控制一个pool最多有多少个状态为idle(空闲的)的redis实例,默认值也是8。
max-wait: -1 # 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。
min-idle: 0 # 控制一个pool最少有多少个状态为idle(空闲的)的redis实例,默认值为0。
timeout: 0 # 连接超时时间 单位 ms,默认为0
password: master # 密码,根据自己的 redis 设计,默认为空

使用

在 配置类 中注册一个 RedisTemplate 用来支持序列化和反序列化:

@Configuration
public class RedisConfig { @Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
StringRedisTemplate template = new StringRedisTemplate(factory);
// 使用 Jackson2JsonRedisSerializer 进行序列化,它继承 RedisSerializer,
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;
} }

测试使用:

@SpringBootTest
@RunWith(SpringRunner.class)
public class RedisTest { @Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private RedisTemplate redisTemplate; @Test
public void test() {
stringRedisTemplate.opsForValue().set("id", "1");
Assert.assertEquals("1", stringRedisTemplate.opsForValue().get("id"));
} /**
* 测试存储对象,redis 需要对对象进行序列化,取出对象数据后比对,又要进行反序列化
* 所以注册了 RedisTemplate ,专门处理这类情况
*/
@Test
public void test1() {
SysUserEntity sysUserEntity = new SysUserEntity();
sysUserEntity.setId(2L);
sysUserEntity.setEmail("k@wuwii.com");
ValueOperations<String, SysUserEntity> operations = redisTemplate.opsForValue();
operations.set("user1", sysUserEntity);
Assert.assertThat(sysUserEntity, Matchers.equalTo(operations.get("user1")));
} }

Spring Data Redis 使用文档


  1. REmote DIctionary Server

学习Spring Boot:(十七)Spring Boot 中使用 Redis的更多相关文章

  1. salesforce零基础学习(八十七)Apex 中Picklist类型通过Control 字段值获取Dependent List 值

    注:本篇解决方案内容实现转自:http://mysalesforceescapade.blogspot.com/2015/03/getting-dependent-picklist-values-fr ...

  2. 【Java学习笔记之十七】Java中普通代码块,构造代码块,静态代码块区别及代码示例分析

    //执行顺序:(优先级从高到低.)静态代码块>mian方法>构造代码块>构造方法. 其中静态代码块只执行一次.构造代码块在每次创建对象是都会执行. 1 普通代码块 //普通代码块:在 ...

  3. 深入学习微框架:Spring Boot(转)

    转:http://www.infoq.com/cn/articles/microframeworks1-spring-boot/ 相关参考: https://spring.io/guides/gs/s ...

  4. 从.Net到Java学习第四篇——spring boot+redis

    从.Net到Java学习系列目录 “学习java已经十天,有时也怀念当初.net的经典,让这语言将你我相连,怀念你......”接上一篇,本篇使用到的框架redis.FastJSON. 环境准备 安装 ...

  5. Spring Boot(十七):使用Spring Boot上传文件

    Spring Boot(十七):使用Spring Boot上传文件 环境:Spring Boot最新版本1.5.9.jdk使用1.8.tomcat8.0 一.pom包配置 <parent> ...

  6. 基于Spring Boot和Spring Cloud实现微服务架构学习

    转载自:http://blog.csdn.net/enweitech/article/details/52582918 看了几周Spring相关框架的书籍和官方demo,是时候开始总结下这中间的学习感 ...

  7. 基于Spring Boot和Spring Cloud实现微服务架构学习--转

    原文地址:http://blog.csdn.net/enweitech/article/details/52582918 看了几周spring相关框架的书籍和官方demo,是时候开始总结下这中间的学习 ...

  8. Spring Boot 监听 Activemq 中的特定 topic ,并将数据通过 RabbitMq 发布出去

    1.Spring Boot 和 ActiveMQ .RabbitMQ 简介 最近因为公司的项目需要用到 Spring Boot , 所以自学了一下, 发现它与 Spring 相比,最大的优点就是减少了 ...

  9. Spring Boot学习(一)——Spring Boot介绍

    Spring Boot介绍 Spring Boot简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式 ...

  10. 深入学习微框架:Spring Boot - NO

    http://blog.csdn.net/hengyunabc/article/details/50120001 Our primary goals are: Provide a radically ...

随机推荐

  1. Python爬虫爬取贴吧的帖子内容

    最近在看一个大神的博客,从他那里学会了很多关于python爬虫的知识,其实python如果想用在实际应用中,你需要了解许多,比如正则表达式.引入库.过滤字段等等,下面不多说,我下面的程序是爬取Ubun ...

  2. 一个struts2程序

    1.index.jsp 2.struts.xml 3.Loginaction.java package action; import java.io.File; import java.io.File ...

  3. 2017-2018-2 20155203《网络对抗技术》 Exp7:网络欺诈防范

    1.基础问题回答 (1)通常在什么场景下容易受到DNS spoof攻击 连接无线网络,和恶意攻击者处在同一局域网下. (2)在日常生活工作中如何防范以上两攻击方法 首先决不去点击浏览器都认为不安全的网 ...

  4. 20155218 《网络对抗技术》 MAL_恶意代码分析

    20155218 <网络对抗技术> MAL_恶意代码分析 实验内容: 1.使用schtasks指令监控系统运行 1.在C盘下新建一个文本文档,输入一下内容后,更名为netstatlog.b ...

  5. mfc 纯虚函数和抽象类

    纯虚函数 抽像类 一.纯虚函数 虚函数为了重载和多态的需要,有时需要在基类中定义一个纯虚函数,代码部分在子类中加以实现.定义格式如下的函数我们称为纯虚函数: ; 纯虚函数与空虚函数是有区别的; 二.抽 ...

  6. 【HNOI2017】礼物

    题面 题解 显然两个手环只需要一个的亮度增加\(c \in [-m, m]\)和原题是等价的. 于是可以写成这样一个公式: \[ \sum_{i = 1} ^ n(x_i - y_{i+k} + c) ...

  7. libgdx学习记录12——圆角矩形CircleRect

    libgdx提供了ShapeRenderer这个工具,用它可以画点.画线.画圆.画矩形.画椭圆.画扇形,但是没有提供画圆角矩形的方法. 刚开始自己尝试分成8端,4端画直线,4端画扇形,发现多了半径几部 ...

  8. webVR全景图多种方案实现(pannellum,aframe,Krpano,three,jquery-vrview)

    前言 有一篇文章我说了H5实现全景图预览,全景视频播放的原理,有需要的小伙伴可以自行去看一下 今天我就拿出我的实践干货出来,本人实测实测过 需求 老板:我需要可以上传全景图片,然后手机网站上都可以36 ...

  9. 创建并使用maven archetype的随笔

    maven骨架archetype的意义在于一些项目的基础项:如引入的maven组件,例如eureka,ribben等,不希望每次新建项目都重复做一遍,还有例如公司规范的log格式,单元测试工具等,在新 ...

  10. Frida----安装

    介绍 它是本机应用程序的 Greasemonkey,或者更多技术术语,它是一个动态代码检测工具包.它允许您将JavaScript或您自己的库的片段注入Windows,macOS,GNU / Linux ...