八、springboot整合redis
整合Redis
一. 注解方式实现添加缓存
1.在pom.xml加入依赖
<!-- 配置使用redis启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
2. 修改引导类
修改开启缓存,添加注解@EnableCaching
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching; @SpringBootApplication
@EnableCaching
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
} }
3. 设置实现序列化接口
要缓存到redis中的实体,需要让实体实现序列化接口
public class User implements Serializable {
private Long id;
private String userName;
private String password;
private String name;
。。。。。。
}
4. 实现添加/删除缓存
修改UserServiceImpl,
添加@Cacheable注解实现缓存添加
添加@CacheEvict注解实现缓存删除
@Override
@CacheEvict(value = "userCache", key = "'user.queryAll'")
public List<User> queryUserByName(String name) {
System.out.println("缓存清理了!");
List<User> list = this.userMapper.queryUserByName(name);
return list;
} // 调用使用UserMapper.xml的Mapper
@Override
@Cacheable(value = "userCache", key = "'user.queryAll'")
public List<User> queryAll() {
System.out.println("从MySQL中查询");
List<User> list = this.userMapper.queryAll();
return list;
}
这样设置完成后,执行queryAll()方法就会使用缓存,如果缓存没有就添加缓存,而queryUserByName(String name)方法则是删除缓存
@Cacheable:添加/使用缓存
@CacheEvict:删除缓存
参数value是缓存的名字,在执行的时候,会找叫这个名字的缓存使用/删除
参数key默认情况下是空串””,是Spring的一种表达式语言SpEL,我们这里可以随意指定,但是需要注意一定要加单引号
二. redis的深入使用
1. 直接操作redis
redis除了作为缓存使用,还有很多其他的作用,例如利用redis的单线程获取唯一数,例如使用redis为单点登录系统存储用户登录信息等,我们就需要直接操作redis。
官网提供了三种接口RedisConnectionFactory, StringRedisTemplate 和 RedisTemplate,我们可以直接注入或者自己实现其他的实现类,来直接操作redis。我们这里使用RedisTemplate来操作Redis。
如下所示,我们只需要直接注入RedisTemplate即可使用以下方法操作redis的五种不同的数据类型

测试:
@Autowired
private RedisTemplate<String, String> redisTemplate; @Override
@CacheEvict(value = "userCache", key = "'user.findAll'")
public List<User> queryUserByName(String name) {
// 保存数据
this.redisTemplate.boundValueOps("redis").set("Hello redis !");
// 设置有效时间为100秒
this.redisTemplate.boundValueOps("redis").expire(100l, TimeUnit.SECONDS);
// 给value每次执行加一操作
this.redisTemplate.boundValueOps("count").increment(1l); System.out.println("缓存清理了!");
List<User> list = this.userMapper.queryUserByName(name);
return list;
}
2. 设置redis连接属性
redis单机版
redis启动器默认情况下会找本地的redis服务,端口号默认是6379如果需要访问其他服务器的redis,则需要在application.properties中进行如下配置:
#Redis
spring.redis.host=192.168.37.161
spring.redis.port=6379
这表示会去找ip为192.168.37.161和端口为6379的服务
redis集群版
#Redis
#spring.redis.host=192.168.37.161
#spring.redis.port=6379 #Redis Cluster
spring.redis.cluster.nodes=192.168.37.161:7001,192.168.37.161:7002,192.168.37.161:7003,192.168.37.161:7004,192.168.37.161:7005,192.168.37.161:7006
Sentinel版同cluster配置nodes节点
切换到集群版只需要做以上配置,配置集群版节点信息,注释掉单机版信息
八、springboot整合redis的更多相关文章
- 九、springboot整合redis二之缓冲配置
1.创建Cache配置类 @Configuration @EnableCaching public class RedisCacheConfig extends CachingConfigurerSu ...
- Redis-基本概念、java操作redis、springboot整合redis,分布式缓存,分布式session管理等
NoSQL的引言 Redis数据库相关指令 Redis持久化相关机制 SpringBoot操作Redis Redis分布式缓存实现 Resis中主从复制架构和哨兵机制 Redis集群搭建 Redis实 ...
- SpringBoot整合Redis、ApachSolr和SpringSession
SpringBoot整合Redis.ApachSolr和SpringSession 一.简介 SpringBoot自从问世以来,以其方便的配置受到了广大开发者的青睐.它提供了各种starter简化很多 ...
- SpringBoot整合Redis及Redis工具类撰写
SpringBoot整合Redis的博客很多,但是很多都不是我想要的结果.因为我只需要整合完成后,可以操作Redis就可以了,并不需要配合缓存相关的注解使用(如@Cacheable). ...
- SpringBoot 整合 Redis缓存
在我们的日常项目开发过程中缓存是无处不在的,因为它可以极大的提高系统的访问速度,关于缓存的框架也种类繁多,今天主要介绍的是使用现在非常流行的NoSQL数据库(Redis)来实现我们的缓存需求. Spr ...
- SpringBoot系列十:SpringBoot整合Redis
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合 Redis 2.背景 Redis 的数据库的整合在 java 里面提供的官方工具包:jed ...
- springboot整合redis(注解形式)
springboot整合redis(注解形式) 准备工作 springboot通常整合redis,采用的是RedisTemplate的形式,除了这种形式以外,还有另外一种形式去整合,即采用spring ...
- springboot整合redis(简单整理)
Redis安装与开启 我这里是在windows上练习,所以这里的安装是指在windows上的安装,操作非常简单,点击https://github.com/MicrosoftArchive/redis/ ...
- SpringBoot整合redis哨兵主从服务
前提环境: 主从配置 http://www.cnblogs.com/zwcry/p/9046207.html 哨兵配置 https://www.cnblogs.com/zwcry/p/9134721. ...
- springboot整合redis——redisTemplate的使用
一.概述 相关redis的概述,参见Nosql章节 redisTemplate的介绍,参考:http://blog.csdn.net/ruby_one/article/details/79141940 ...
随机推荐
- 【刷题】BZOJ 1537 [POI2005]Aut- The Bus
Description Byte City 的街道形成了一个标准的棋盘网络 – 他们要么是北南走向要么就是西东走向. 北南走向的路口从 1 到 n编号, 西东走向的路从1 到 m编号. 每个路口用两个 ...
- 【2018CCPC秦皇岛】
递推式的线段树可以用矩阵维护.
- 【HDU5469】Antonidas(点分治,字符串哈希)
[HDU5469]Antonidas(点分治,字符串哈希) 题面 HDU Vjudge 题解 啊哈?什么垃圾一眼点分治+Hash判断,哈哈哈哈哈,让我来码码码. 诶,怎么WA了.改改改改改. 诶,怎么 ...
- WEB入门.九 导航菜单
学习内容 水平导航菜单 垂直导航菜单 下拉式导航菜单 能力目标 制作tab标签导航菜单 制作带箭头的导航菜单 制作带信息提示的导航菜单 制作垂直下拉导航菜单 制作水平下拉导航菜单 本章简介 上一章节中 ...
- adb server version (32) doesn't match this client (36); killing...
http://blog.csdn.net/seaker_/article/details/55107598 FAQ: adb server version (36) doesn't match thi ...
- Android Log详解(Log.v,Log.d,Log.i,Log.w,Log.e)
1.Log.v 的调试颜色为黑色的,任何消息都会输出,这里的v代表verbose啰嗦的意思,平时使用就是Log.v("",""); 2.Log.d的输出颜色是蓝 ...
- 解题:JSOI 2016 最佳团体
题面 0/1分数规划+树形背包检查 要求$\frac{\sum P_i}{\sum S_i}的最大值,$按照0/1分数规划的做法,二分一个mid之后把式子化成$\sum P_i=\sum S_i*mi ...
- HDU 6006 状压dp
Engineer Assignment Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- MySQL搭建环境
一.MySQL安装 Windows下安装参考网址:https://blog.csdn.net/NepalTrip/article/details/79492058 Ubuntu Linux下安装参考网 ...
- Nginx模块Lua-Nginx-Module学习笔记(二)Lua指令详解(Directives)
源码地址:https://github.com/Tinywan/Lua-Nginx-Redis Nginx与Lua编写脚本的基本构建块是指令. 指令用于指定何时运行用户Lua代码以及如何使用结果. 下 ...