整合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的更多相关文章

  1. 九、springboot整合redis二之缓冲配置

    1.创建Cache配置类 @Configuration @EnableCaching public class RedisCacheConfig extends CachingConfigurerSu ...

  2. Redis-基本概念、java操作redis、springboot整合redis,分布式缓存,分布式session管理等

    NoSQL的引言 Redis数据库相关指令 Redis持久化相关机制 SpringBoot操作Redis Redis分布式缓存实现 Resis中主从复制架构和哨兵机制 Redis集群搭建 Redis实 ...

  3. SpringBoot整合Redis、ApachSolr和SpringSession

    SpringBoot整合Redis.ApachSolr和SpringSession 一.简介 SpringBoot自从问世以来,以其方便的配置受到了广大开发者的青睐.它提供了各种starter简化很多 ...

  4. SpringBoot整合Redis及Redis工具类撰写

            SpringBoot整合Redis的博客很多,但是很多都不是我想要的结果.因为我只需要整合完成后,可以操作Redis就可以了,并不需要配合缓存相关的注解使用(如@Cacheable). ...

  5. SpringBoot 整合 Redis缓存

    在我们的日常项目开发过程中缓存是无处不在的,因为它可以极大的提高系统的访问速度,关于缓存的框架也种类繁多,今天主要介绍的是使用现在非常流行的NoSQL数据库(Redis)来实现我们的缓存需求. Spr ...

  6. SpringBoot系列十:SpringBoot整合Redis

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合 Redis 2.背景 Redis 的数据库的整合在 java 里面提供的官方工具包:jed ...

  7. springboot整合redis(注解形式)

    springboot整合redis(注解形式) 准备工作 springboot通常整合redis,采用的是RedisTemplate的形式,除了这种形式以外,还有另外一种形式去整合,即采用spring ...

  8. springboot整合redis(简单整理)

    Redis安装与开启 我这里是在windows上练习,所以这里的安装是指在windows上的安装,操作非常简单,点击https://github.com/MicrosoftArchive/redis/ ...

  9. SpringBoot整合redis哨兵主从服务

    前提环境: 主从配置 http://www.cnblogs.com/zwcry/p/9046207.html 哨兵配置 https://www.cnblogs.com/zwcry/p/9134721. ...

  10. springboot整合redis——redisTemplate的使用

    一.概述 相关redis的概述,参见Nosql章节 redisTemplate的介绍,参考:http://blog.csdn.net/ruby_one/article/details/79141940 ...

随机推荐

  1. 【CF938G】Shortest Path Queries(线段树分治,并查集,线性基)

    [CF938G]Shortest Path Queries(线段树分治,并查集,线性基) 题面 CF 洛谷 题解 吼题啊. 对于每个边,我们用一个\(map\)维护它出现的时间, 发现询问单点,边的出 ...

  2. uoj132/BZOJ4200/洛谷P2304 [Noi2015]小园丁与老司机 【dp + 带上下界网络流】

    题目链接 uoj132 题解 真是一道大码题,,,肝了一个上午 老司机的部分是一个\(dp\),观察点是按\(y\)分层的,而且按每层点的上限来看可以使用\(O(nd)\)的\(dp\),其中\(d\ ...

  3. 【spoj SUBLEX】 Lexicographical Substring Search

    http://www.spoj.com/problems/SUBLEX/ (题目链接) 题意 给出一个字符串,询问其中字典序第K小的子串. Solution 后缀自动机例题. 构出后缀自动机以后,对每 ...

  4. Android proguard-rules.pro 混淆模板

    在../sdk/tools/proguard/目录下,其中包含了android最基本的混淆 ..\proguard-rules.pro  混淆文件配置模板: ############# 混淆模板 ## ...

  5. spark 性能调优(一) 性能调优的本质、spark资源使用原理、调优要点分析

    转载:http://www.cnblogs.com/jcchoiling/p/6440709.html 一.大数据性能调优的本质 编程的时候发现一个惊人的规律,软件是不存在的!所有编程高手级别的人无论 ...

  6. Linux查看动态库.so导出函数列表

    https://blog.csdn.net/chrisnotfound/article/details/80662923

  7. C++析构函数的自动调用(用于父类指针指向子类对象,内存泄漏问题)

    class A {public:A() { printf("A \n"); }~A() { printf(" ~A \n"); } // 这里不管写不写virt ...

  8. 使用nginx的ngx_upstream_jdomain模块实现k8s容器的负载均衡

    使用背景最近一直在准备k8s上线事宜,目前已经在测试环境中全面部署并通过压力测试环境检验.离正式上线基本只剩下时间问题.我们目前测试环境中的容器负载均衡大量使用到了nginx,就是借助了ngx_ups ...

  9. Hadoop生态圈-注册并加载协处理器(coprocessor)的三种方式

    Hadoop生态圈-注册并加载协处理器(coprocessor)的三种方式 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 到目前为止,大家已经掌握了如何使用过滤器来减少服务器端通过 ...

  10. java基础-Map集合

    java基础-Map集合 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Map集合概述 我们通过查看Map接口描述,发现Map接口下的集合与Collection接口下的集合,它 ...