在SpringBoot中使用Redis,思路如下:

查询时先查Redis缓存,如果缓存中存在信息,就直接从缓存中获取。

如果缓存中没有相关信息,就去数据库中查找,查完顺便将信息存放进缓存里,以便下一次查询。

另外,更新或者删除数据库数据时,记得删除相关的缓存。

在SpringBoot中使用Redis的步骤如下:

1.首先,添加依赖包:

 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.3.2.RELEASE</version>
</dependency>

2.在application.properties中添加Redis配置:

## Redis 配置
## Redis数据库索引(默认为0)
spring.redis.database=0
## Redis服务器地址
spring.redis.host=127.0.0.1
## Redis服务器连接端口
spring.redis.port=6379
## Redis服务器连接密码(默认为空)
spring.redis.password=
## 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
## 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
## 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
## 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
## 连接超时时间(毫秒)
spring.redis.timeout=0

3.对象序列化后才能存储到Redis。我们需要将对象类实现序列化接口 RedisSerializer,并生成serialVersionUID。

如果不实现RedisSerializer接口,会报异常:

IllegalArgumentException: DefaultSerializer requires a Serializable payload but received an object of type

serialVersionUID在eclipse和Intellij Idea中都可以自动生成。详情百度查找。

public class User implements Serializable {
private static final long serialVersionUID = 4798316249512579846L; private String uid; private String userName; private String name; private String password; //省略其他的构造方法、getter()、setter()等 }

4.在服务层进行缓存操作,如下所示:

@Service
public class UserServiceImpl implements UserSerevice { @Autowired
private UserDao userDao; //redisTemplate在注入时,无需声明泛型
@Autowired
private RedisTemplate redisTemplate;
private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class); @Override
public User showUsers(String uid){
//从缓存中获取信息
ValueOperations<String,User> valueOperations=redisTemplate.opsForValue();
//判断缓存是否有记录,如果有记录就直接从缓存中获取信息
boolean hasKey=redisTemplate.hasKey(uid);
if(hasKey) {
User user= valueOperations.get(uid);
logger.debug("====================>从缓存中获取了用户id为"+uid+"的用户信息.");
return user;
}
User user =userDao.selectByPrimaryKey(uid);
//如果缓存中不存在信息,在查询过后,就将信息插入缓存中
if(user!=null) {
valueOperations.set(uid,user);
logger.debug("====================>将用户id为"+uid+"的用户信息插入缓存中.");
}
return user;
}
}

SpringBoot中使用Redis的更多相关文章

  1. 由浅入深学习springboot中使用redis

    很多时候,我们会在springboot中配置redis,但是就那么几个配置就配好了,没办法知道为什么,这里就详细的讲解一下 这里假设已经成功创建了一个springboot项目. redis连接工厂类 ...

  2. (一)由浅入深学习springboot中使用redis

    很多时候,我们会在springboot中配置redis,但是就那么几个配置就配好了,没办法知道为什么,这里就详细的讲解一下 这里假设已经成功创建了一个springboot项目. redis连接工厂类 ...

  3. 在springboot中使用redis缓存,将缓存序列化为json格式的数据

    背景 在springboot中使用redis缓存结合spring缓存注解,当缓存成功后使用gui界面查看redis中的数据 原因 springboot缓存默认的序列化是jdk提供的 Serializa ...

  4. SpringBoot中整合Redis、Ehcache使用配置切换 并且整合到Shiro中

    在SpringBoot中Shiro缓存使用Redis.Ehcache实现的两种方式实例 SpringBoot 中配置redis作为session 缓存器. 让shiro引用 本文是建立在你是使用这sh ...

  5. 在SpringBoot中引入Redis

    前言 之前我们只是在Spring中加入Redis用于session的存放,并没有对redis进行主动的存放,这次我们需要加入redis工具类来方便我们在实际使用过程中操作redis 已经加入我的git ...

  6. SpringBoot中集成redis

    转载:https://www.cnblogs.com/zeng1994/p/03303c805731afc9aa9c60dbbd32a323.html 不是使用注解而是代码调用 需要在springbo ...

  7. 你知道如何在springboot中使用redis吗

    特别说明:本文针对的是新版 spring boot 2.1.3,其 spring data 依赖为 spring-boot-starter-data-redis,且其默认连接池为 lettuce ​  ...

  8. SpringBoot学习笔记(10)-----SpringBoot中使用Redis/Mongodb和缓存Ehcache缓存和redis缓存

    1. 使用Redis 在使用redis之前,首先要保证安装或有redis的服务器,接下就是引入redis依赖. pom.xml文件如下 <dependency> <groupId&g ...

  9. [SpringBoot]SpringBoot中使用redis事务

    本文基于SpringBoot 2.X 事务在关系型数据库的开发中经常用到,其实非关系型数据库,比如redis也有对事务的支持,本文主要探讨在SpringBoot中如何使用redis事务. 事务的相关介 ...

随机推荐

  1. 查看selenium api的方法

    首先打开命令行,在doc窗口输入: python -m pydoc -p 4567简单解释一下: python -m pydoc表示打开pydoc模块,pydoc是查看python文档的首选工具:-p ...

  2. 模拟估算器:scikit-learn Estimator

    转载:https://www.toutiao.com/i6606193174010397187/ 当一个数据科学项目刚刚开始时,关键是要尽可能快地走向一个最小可行的产品(MVP).这个MVP将包含最终 ...

  3. Web API 源码剖析之默认消息处理程序链之路由分发器(HttpRoutingDispatcher)

    Web API 源码剖析之默认消息处理程序链-->路由分发器(HttpRoutingDispatcher) 我们在上一节讲述了默认的DefaultServer(是一个类型为HttpServer的 ...

  4. php while循环控制实例讲解

    while循环是PHP中最简单的循环,其基本格式为: while (expr){ statement } 或者 while (expr): statement endwhile; 该语法表示,只要ex ...

  5. Convolutional Neural Networks

    卷积神经网络(Convolutional Neural Networks/ CNN/ConvNets) 卷积神经网络和普通神经网络十分相似: 组成它们的神经元都具有可学习的权重(weights)和偏置 ...

  6. OpenCL + OpenCV 图像旋转

    ▶ 使用 OpenCV 从文件读取彩色的 png 图像,旋转一定角度以后写回文件 ● 代码,核函数 // rotate.cl //__constant sampler_t sampler = CLK_ ...

  7. 《GPU高性能编程CUDA实战》第八章 图形互操作性

    ▶ OpenGL与DirectX,等待填坑. ● basic_interop #include <stdio.h> #include "cuda_runtime.h" ...

  8. leetcode504

    public class Solution { public string ConvertToBase7(int num) { ? "" : "-"; var ...

  9. SQL2014还原到2008

    请使用with move选项来标识该文件的有效位置 sqlserver用命令还原数据库 restore   database   TT     from   disk='E:\test.bak'    ...

  10. jsfl 巧用获取jsfl绝对路径,导入配置文件,注意配置文件无法改变舞台宽高

    //获取jsfl下的AS3.xml配置文件的路径 var jsflURL_arr=fl.scriptURI.split("/"); jsflURL_arr.splice(jsflU ...