SpringBoot2.x整合Redis实战 4节课
1、分布式缓存Redis介绍
简介:讲解为什么要用缓存和介绍什么是Redis,新手练习工具

1、redis官网 https://redis.io/download
2、新手入门redis在线测试工具:http://try.redis.io/
2、源码编译安装Redis4.x
简介:使用源码安装Redis4.x和配置外网访问
1、快速安装 https://redis.io/download#installation
wget http://download.redis.io/releases/redis-4.0.9.tar.gz
tar xzf redis-4.0.9.tar.gz
cd redis-4.0.9
make
启动服务端:src/redis-server
启动客户端:src/redis-cli
2、默认是本地访问的,需要开放外网访问
1)打开redis.conf文件在NETWORK部分修改
注释掉bind 127.0.0.1可以使所有的ip访问redis
修改 protected-mode,值改为no
3、SpringBoot2.x整合redis实战讲解
简介:使用springboot-starter整合reids实战
1、官网:https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-redis
集群文档:https://docs.spring.io/spring-data/data-redis/docs/current/reference/html/#cluster
2、springboot整合redis相关依赖引入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
3、相关配置文件配置
#=========redis基础配置=========
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379
# 连接超时时间 单位 ms(毫秒)
spring.redis.timeout=3000
#=========redis线程池设置=========
# 连接池中的最大空闲连接,默认值也是8。
spring.redis.pool.max-idle=200
#连接池中的最小空闲连接,默认值也是0。
spring.redis.pool.min-idle=200
# 如果赋值为-1,则表示不限制;pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
spring.redis.pool.max-active=2000
# 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时
spring.redis.pool.max-wait=1000
4、常见redistemplate种类讲解和缓存实操(使用自动注入)
1、注入模板
@Autowired
private StirngRedisTemplate strTplRedis
2、类型String,List,Hash,Set,ZSet
对应的方法分别是opsForValue()、opsForList()、opsForHash()、opsForSet()、opsForZSet()
代码示例:
RdisTestController.java:
package net.xdclass.base_project.controller; import net.xdclass.base_project.domain.JsonData; import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("/api/v1/redis")
public class RdisTestController { @Autowired
private StringRedisTemplate redisTpl; //jdbcTemplate @GetMapping(value="add")
public Object add(){ //opsForValue : Returns the operations performed on simple values (or Strings in Redis terminology). redisTpl.opsForValue().set("name", "xdclass2018"); return JsonData.buildSuccess(); } @GetMapping(value="get")
public Object get(){ String value = redisTpl.opsForValue().get("name");
return JsonData.buildSuccess(value); } }
4、Redis工具类封装讲解和实战
简介:高效开发方式 Redis工具类封装讲解和实战
1、常用客户端 https://redisdesktop.com/download
2、封装redis工具类并操作
装入对象:
@GetMapping(value="save_user")
public Object saveUser(){
User user = new User(1,"abc","11",new Date());
//String value = redisTpl.opsForValue().get("name");
String userStr = JsonUtils.obj2String(user);
boolean flag = redis.set("base:user:11", userStr);
return JsonData.buildSuccess(flag); } @GetMapping(value="find_user")
public Object findUser(){ String userStr = redis.get("base:user:11");
User user = JsonUtils.string2Obj(userStr, User.class); return JsonData.buildSuccess(user); }
base:user:11该命名方法的内容在可视化工具中会以文件夹形式展示
浏览器输入:http://localhost:8080/api/v1/redis/save_user

取值:
浏览器输入:http://localhost:8080/api/v1/redis/find_user
返回结果:

SpringBoot2.x整合Redis实战 4节课的更多相关文章
- 小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_39、SpringBoot2.x整合redis实战讲解
笔记 3.SpringBoot2.x整合redis实战讲解 简介:使用springboot-starter整合reids实战 1.官网:https://docs.spring.io/spring-bo ...
- 小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战
笔记 4.Redis工具类封装讲解和实战 简介:高效开发方式 Redis工具类封装讲解和实战 1.常用客户端 https://redisdesktop.com/download ...
- 小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_37、分布式缓存Redis介绍
笔记 1.分布式缓存Redis介绍 简介:讲解为什么要用缓存和介绍什么是Redis,新手练习工具 1.redis官网 https://redis.io/download ...
- 小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_38、源码编译安装Redis4.x
笔记 2.源码编译安装Redis4.x 简介:使用源码安装Redis4.x和配置外网访问 1.快速安装 https://redis.io/download#installation ...
- 【SpringBoot】整合Redis实战
========================9.SpringBoot2.x整合Redis实战 ================================ 1.分布式缓存Redis介绍 简介: ...
- centos mysql 实战 第一节课 安全加固 mysql安装
centos mysql 实战 第一节课 安全加固 mysql安装 percona名字的由来=consultation 顾问+performance 性能=per con a mysql ...
- 11、Logback日志框架介绍和SpringBoot整合实战 2节课
1.新日志框架LogBack介绍 简介:日志介绍和新日志框架Logback讲解 1.常用处理java的日志组件 slf4j,log4j,logback,common-logging 等 ...
- SpringBoot2.0 整合 Redis集群 ,实现消息队列场景
本文源码:GitHub·点这里 || GitEE·点这里 一.Redis集群简介 1.RedisCluster概念 Redis的分布式解决方案,在3.0版本后推出的方案,有效地解决了Redis分布式的 ...
- Springboot2.0整合Redis(注解开发)
一. pom.xm文件引入对应jar包 <dependency> <groupId>org.springframework.boot</groupId> <a ...
随机推荐
- BZOJ1283 序列(费用流)
不妨看做是先用k个指针指向被选择的前k个元素,然后每次将选中当前第一个元素的指针移到最后,并且需要满足位置变化量>=m.显然这样可以构造出所有的合法方案.那么可以以此建立费用流模型,以一条流量k ...
- [USACO 2018 December Contest]作业总结
t1 Convention 题目大意 每一头牛都有一个来的时间,一共有\(n\)辆车,求出等待时间最长的那头牛等待的最小时间. 解法 第一眼看到这道题还以为是\(2018noip\)普及组的t3魔鬼题 ...
- Linux中使用Electronic WeChat客户端
微信推出了Windows版和Mac版,一直没有推出也不会推出Linux版本,这就催生了Electronic WeChat(https://github.com/geeeeeeeeek/electron ...
- Mac上配置idea的项目上传到GitHub
1.安装git,Mac默认已经安装了Git,可以通过命令git —version查询一下. 2.创建SSH KEY(如果已经创建过,则不用再次创建.查看~/.ssh/id_rsa.pub是否存在) 生 ...
- java:取屏幕大小(去掉任务栏的高度部分)
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize(); Insets screenInsets = Toolkit.getD ...
- IntelliJ IDEA的使用操作链接
一:IntelliJ IDEA导入多个eclipse项目到同一个workspace下: http://www.cnblogs.com/ThinkVenus/p/6783961.html?utm_sou ...
- ref实现输入框聚焦
关于ref我们是怎么理解的呢? 我们可以通过React.createRef()创建一个 ref节点,并将其打印出来. 代码如下: import React,{Component} from 'reac ...
- Object,equals,toString
一.Object类 说明:Object类是Java中所有的类的直接或者间接的父类(基类). 该类中定义的是所有的类中的都有的的功能. 位置:可以从API中查找. 二.Object类之 equals 方 ...
- MySQL基本命令行
登陆:mysql –h localhost –u 用户名 –p mysql –u 用户名 –p (默认连接localhost服务器) 服务器中可以有多个库,库中可以有多个表.数据库的名字无法修改 ...
- Study 6 —— while循环
语法while 条件: 执行代码... 1. #从0打印到100,每循环一次 +1 count = 0 while count <= 100 : print('Loop: ', count) c ...