在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. 【EasyUI学习-3】Easyui tabs入门实践

    作者:ssslinppp       1. 摘要 一般我们在设计程序主框架的时候,当点击(子)菜单时,希望相应界面都在tabs页中显示: 在显示的时候,如果之前打开过该界面,则希望重新选中对应的tab ...

  2. MySQL主从同步和半同步配置

    mysql主从配置: 1,安装maraidb,使用国内yum镜像站下载:[root@localhost mysql]# cat /etc/yum.repos.d/MairaDB.repo # Mari ...

  3. excel注释怎么添加图片

    批注中加图片:插入批注>点击批注边框线(注意是边线框,不是里面的编辑区域),右键,设置批注格式>颜色与线条>填充,选择颜色,填充效果>图片>选择图片>确定>确 ...

  4. poj 2229 Sumsets(dp)

    Sumsets Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 400000/200000K (Java/Other) Total Sub ...

  5. PHP中的urlencode,rawurlencode和JS中的encodeURI,encodeURIComponent

    PHP中的urlencode,rawurlencode和JS中的encodeURI,encodeURIComponent [PHP中的urlencode和rawurlencode] urlencode ...

  6. Delphi7到Delphi XE2的升级历程

    1.PChar 转为PAnsiChar; 2.第三方控件的安装 SuiPack不能直接点击InStall.exe安装,需要打开DPK文件安装: SuiPack安装之后程序编译会报错,resHandle ...

  7. 【Linux_Unix系统编程】chapter5 深入探究文件IO

    Chapter5 深入探究文件I/O 本章节将介绍另一个与文件操作相关的系统调用:多用途的fcntl(),并展示其应用之一读取和设置打开文件的状态标志. 5.1 原子操作和竞争条件 所有系统调用都是以 ...

  8. 线程使用方法 锁(lock,Rlock),信号了(Semaphore),事件(Event),条件(Ccndition),定时器(timer)

    2线程的使用方法  (1)锁机制       递归锁           RLock()    可以有无止尽的锁,但是会有一把万能钥匙       互斥锁:           Lock()     ...

  9. PyQt5系列教程(九)QInputDialog的使用

    软硬件环境 Ubuntu 15.10 32bit Python 3.5.1 PyQt 5.5.1 前言 输入框是界面开发中非常常见的控件,本文就来看看PyQt5中QInputDialog的使用 实例 ...

  10. 4. java乱码处理

    //返回到jsp页面 //request.setCharacterEncoding("utf-8"); //response.setContentType("text/h ...