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. JDBC访问数据库的基本步骤是什么?

    1.加载(注册)数据库驱动(到JVM) 2.建立(获取)数据库连接. 3.创建(获取)数据库操作对象. 4.定义操作的SQL语句. 5.执行数据库操作. 6.获取并操作结果集. 7.关闭对象,回收数据 ...

  2. 一行 CSS 代码的魅力

    之前在知乎看到一个很有意思的讨论 一行代码可以做什么? 那么,一行 CSS 代码又能不能搞点事情呢? CSS Battle 首先,这让我想到了,年初的时候沉迷的一个网站 CSS Battle .这个网 ...

  3. SpringMVC的@Validated校验注解使用方法

    validate会对参数进行校验,校验标准为validate后的类中的标准.本例中对User进行校验,User类中设置了校验标准. 在后台开发过程中,对参数的校验成为开发环境不可缺少的一个环节.比如参 ...

  4. Java学习_泛型

    什么是泛型. Java标准库提供的ArrayList内部就是一个Object[]数组,配合存储一个当前分配的长度,就可以充当"可变数组". public class ArrayLi ...

  5. JAVA编程环境与基本数据类型

    <JAVA编程环境与基本数据类型> 随笔目录 # <JAVA编程环境与基本数据类型> 随笔目录 - Java小实例 java的编程环境 java数据类型 Java小实例 jav ...

  6. [Skill] 记一次vue项目热更新失败案例

    病因 修改文件后无法更新界面,感觉留白,没有更新 解决过程 发现问题,第一步搜索别人是怎么解决的 搜索一阵后,发现大部分都是留言路径问题,基本都是组件文件夹大写导致 修改测试后发现问题依旧 然后仔细观 ...

  7. Akka Typed 官方文档之随手记

    ️ 引言 近两年,一直在折腾用FP与OO共存的编程语言Scala,采取以函数式编程为主的方式,结合TDD和BDD的手段,采用Domain Driven Design的方法学,去构造DDDD应用(Dom ...

  8. Lesson_strange_words1

    time-between system 分时操作:分时系统 secondary 辅助的,从属的 establish 已确立的 capability 功能 formerly 之前的 combine 结合 ...

  9. .netcore利用perf分析高cpu使用率

    目录 一 在宿主机运行perf 二 容器内安装perf 1,重新构建镜像 2,下载火焰图生成脚本 3,安装linux-perf 三 CPU占用分析 1,perf record捕获进程 2,生成火焰图 ...

  10. 【老孟Flutter】2021 年 Flutter 官方路线图

    老孟导读:这是官方公布的2021年路线图,向我们展示了2021年 Flutter 的主要工作及计划. 原文地址:https://github.com/flutter/flutter/wiki/Road ...