1、SpringBoot集成Redis

1.1 核心依赖

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

1.2 配置文件

# 端口
server:
port: 8008
spring:
application:
# 应用名称
name: node08-boot-redis
# redis 配置
redis:
host: 127.0.0.1
#超时连接
timeout: 1000ms
jedis:
pool:
#最大连接数据库连接数,设 0 为没有限制
max-active: 8
#最大等待连接中的数量,设 0 为没有限制
max-idle: 8
#最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
max-wait: -1ms
#最小等待连接中的数量,设 0 为没有限制
min-idle: 0

这样Redis的环境就配置成功了,已经可以直接使用封装好的API了。

1.3 简单测试案例

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
@RestController
public class RedisController {
@Resource
private StringRedisTemplate stringRedisTemplate ;
@RequestMapping("/setGet")
public String setGet (){
stringRedisTemplate.opsForValue().set("cicada","smile");
return stringRedisTemplate.opsForValue().get("cicada") ;
}
@Resource
private RedisTemplate redisTemplate ;
/**
* 设置 Key 的有效期 10 秒
*/
@RequestMapping("/setKeyTime")
public String setKeyTime (){
redisTemplate.opsForValue().set("timeKey","timeValue",10, TimeUnit.SECONDS);
return "success" ;
}
@RequestMapping("/getTimeKey")
public String getTimeKey (){
// 这里 Key 过期后,返回的是字符串 'null'
return String.valueOf(redisTemplate.opsForValue().get("timeKey")) ;
}
}

1.4 自定义序列化配置

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.io.Serializable;
/**
* Redis 配置
*/
@Configuration
public class RedisConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(RedisConfig.class) ;
/**
* 序列化配置
*/
@Bean
public RedisTemplate<String, Serializable> redisTemplate
(LettuceConnectionFactory redisConnectionFactory) {
LOGGER.info("RedisConfig == >> redisTemplate ");
RedisTemplate<String, Serializable> template = new RedisTemplate<>();
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}

1.5 序列化测试

import com.boot.redis.entity.User;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@RestController
public class SerializeController {
@Resource
private RedisTemplate redisTemplate ;
@RequestMapping("/setUser")
public String setUser (){
User user = new User() ;
user.setName("cicada");
user.setAge(22);
List<String> list = new ArrayList<>() ;
list.add("小学");
list.add("初中");
list.add("高中");
list.add("大学");
user.setEducation(list);
redisTemplate.opsForValue().set("userInfo",user);
return "success" ;
}
@RequestMapping("/getUser")
public User getUser (){
return (User)redisTemplate.opsForValue().get("userInfo") ;
}
}

七:SpringBoot-集成Redis数据库,实现缓存管理的更多相关文章

  1. SpringBoot集成Redis来实现缓存技术方案

    概述 在我们的日常项目开发过程中缓存是无处不在的,因为它可以极大的提高系统的访问速度,关于缓存的框架也种类繁多,今天主要介绍的是使用现在非常流行的NoSQL数据库(Redis)来实现我们的缓存需求. ...

  2. 【springBoot】springBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  3. SpringBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  4. springboot集成redis(mybatis、分布式session)

    安装Redis请参考:<CentOS快速安装Redis> 一.springboot集成redis并实现DB与缓存同步 1.添加redis及数据库相关依赖(pom.xml) <depe ...

  5. Windows环境下springboot集成redis的安装与使用

    一,redis安装 首先我们需要下载Windows版本的redis压缩包地址如下: https://github.com/MicrosoftArchive/redis/releases 连接打开后如下 ...

  6. Spring Boot从入门到精通(七)集成Redis实现Session共享

    单点登录(SSO)是指在多个应用系统中,登录用户只需要登录验证一次就可以访问所有相互信任的应用系统,Redis Session共享是实现单点登录的一种方式.本文是通过Spring Boot框架集成Re ...

  7. springBoot集成Redis遇到的坑(择库)源码分析为什么择库失败

    提示: springboot提供了一套链接redis的api,也就是个jar包,用到的连接类叫做LettuceConnectionConfiguration,所以我们引入pom时是这样的 <de ...

  8. springBoot 集成Mysql数据库

    springBoot 集成Mysql数据库 前一段时间,我们大体介绍过SpringBoot,想必大家还有依稀的印象.我们先来回顾一下:SpringBoot是目前java世界最流行的一个企业级解决方案框 ...

  9. springboot集成redis使用redis作为session报错ClassNotFoundException类RememberMeServices

    springboot 集成redis使用redis作为缓存,会报错的问题. 错误信息: java.lang.IllegalStateException: Error processing condit ...

  10. SpringBoot | 集成Redis

    Windows下安装: https://github.com/MicrosoftArchive/redis/releases zip下就解包到自定义目录下,msi就跟着步骤安装 进入安装目录下运行命令 ...

随机推荐

  1. 容器编排系统K8s之访问控制--准入控制

    前文我们聊到了k8s的访问控制第二关RBAC授权插件的相关话题,回顾请参考:https://www.cnblogs.com/qiuhom-1874/p/14216634.html:今天我们来聊一下k8 ...

  2. CentOS 6或7 启动故障修复及root密码破解

    CentOS 6或7 启动故障修复及root密码破解 目录 CentOS 6或7 启动故障修复及root密码破解 CentOS 6启动流程修复: 实验一:删除initramfs-2.6.32-754. ...

  3. Liunx运维(八)-LIunx磁盘与文件系统管理命令

    文档目录: 一.fdisk:磁盘分区工具 二.partprobe:更新内核的硬盘分区表信息 三.tune2fs:调整ext2/ext3/ext4文件系统参数 四.parted:磁盘分区工具 五.mkf ...

  4. VS2017+qt5.9的安装

    VS2017+qt5.9的安装 运行环境: win7旗舰版+vs2017专业版+qt5.9.2 第一步:安装qt5.9.2 下载qt安装包:下载地址http://mirrors.ustc.edu.cn ...

  5. TextView上下滚动

    public class AutoTextView extends TextSwitcher implements ViewFactory { private float mHeight; priva ...

  6. window10 安装Mysql 8.0.17以及忘记密码重置密码

    一.安装Mysql8.0.17 1:首先去官网下载安装包 下载地址:https://dev.mysql.com/downloads/mysql/ 2:将解压文件解压到你安装的目录:D:\mysql\m ...

  7. 测试如何区分前后端bug

    当我们测试到前后端分离的项目时,可能就会想这个bug我到底应该指给谁,是前端的问题还是后端的呢,为了让自己更专业,分清前后端问题还是很重要的. 1.如图商品详情中显示[件装:1,中包装:2 ]但是在后 ...

  8. 四:WEB源码扩展

    前言:WEB源码在安全测试中是非常重要的信息来源,可以用来进行代码审计漏洞也可以用来做信息突破口,其中WEB源码有很多技术需要简明分析,获取某ASP源码后就可以采用默认数据库下载为突破,获取某其他脚本 ...

  9. 计网Q1:多个方面比较电路交换、报文交换和分组交换的主要优缺点

    网上看到的带佬儿的帖子......膜过来<doge 原文链接: https://blog.csdn.net/njchenyi/article/details/1540657 电路交换: 由于电路 ...

  10. IE浏览器直接在页面中显示7z文件而不是下载问题解决

    IE浏览器中输入7z文件的完整下载URL后,不是保存文件,而是直接在页面中显示(当然是乱码) 这是因为浏览器对不同的资源文件处理的方式不同,例如图片文件,一般会直接打开,所以我们可以不用7z,使用zi ...