SpringBoot学习- 5、整合Redis
SpringBoot项目中访问Redis主要有两种方式:JedisPool和RedisTemplate,本文使用JedisPool
1、pom.xml添加dependency
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
<scope>compile</scope>
</dependency>
2、下面封装一个工具类,utils新建JedisPoolUtil,先只封装读写字符串
package com.jgui.utils; import com.jgui.config.RedisConfig;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig; /**
* @Author: zhaogaojian
* @Description:
* @Date: Created in 2020/1/821:54
*/
@Component
public class JedisPoolUtil {
private JedisPool jedisPool=null;
@Autowired
private RedisConfig redisConfig;
private JedisPool getJedisPool() {
if(jedisPool==null)
{
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(redisConfig.getPoolMaxTotal());
jedisPoolConfig.setMaxIdle(redisConfig.getPoolMaxIdle());
jedisPoolConfig.setMaxWaitMillis(redisConfig.getPoolMaxWait());
jedisPool=new JedisPool(jedisPoolConfig, redisConfig.getHost(),redisConfig.getPort(),redisConfig.getTimeout()*1000,redisConfig.getPassword(),0);
}
return jedisPool;
}
public String set(String key,String value, int cacheSeconds)
{
String strRet = null;
Jedis jedis =null;
try {
jedis = getJedisPool().getResource();
strRet=jedis.set(key,value);
if (cacheSeconds != 0) {
jedis.expire(key, cacheSeconds);
}
} catch (Exception e) {
//logger.error(e.getMessage(), e);
} finally {
if (jedis != null)
jedis.close(); //注意这里不是关闭连接,在JedisPool模式下,Jedis会被归还给资源池。
}
return strRet;
}
public String get(String key)
{
String strRet=null;
Jedis jedis =null;
try {
jedis = getJedisPool().getResource();
strRet=jedis.get(key);
} catch (Exception e) {
//logger.error(e.getMessage(), e);
} finally {
if (jedis != null)
jedis.close(); //注意这里不是关闭连接,在JedisPool模式下,Jedis会被归还给资源池。
}
return strRet;
}
}
3、Config文件新建RedisConfig
package com.jgui.config; import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component; /**
* @Author: zhaogaojian
* @Description:
* @Date: Created in 2020/1/822:51
*/
@Data
@Component
@ConfigurationProperties(prefix = "redis") //把同类的配置信息自动封装成实体类
public class RedisConfig {
private String host="127.0.0.1";
private Integer port=6379;
private Integer timeout=3; //秒
private String password="123456"; private Integer poolMaxTotal=10;
private Integer poolMaxIdle=10;
private Integer poolMaxWait=3;//秒
}
4、application.properties增加配置节
#--------------------Jedis配置-------------------
redis.host=127.0.0.1
redis.port=6379
#秒
redis.timeout=5
redis.password=123456
redis.poolMaxTotal=10
redis.poolMaxIdle=10
#秒
redis.poolMaxWait=3
5、修改hello进行测试
@Autowired
private JedisPoolUtil jedisPoolUtil;
@RequestMapping("/hello")
public String hello() { String ret=jedisPoolUtil.set("111","123",60);
String ret1=jedisPoolUtil.get("111");
return ret1;
//return userDao.selectByPrimaryKey(1).getRealname();
//return "Hello World11";
}
6、拦截器增加hello排除
public void addInterceptors(InterceptorRegistry registry){
List<String> excludePath = new ArrayList<>();
excludePath.add("/login"); //登录
excludePath.add("/hello"); //测试
registry.addInterceptor(tokenInterceptor)
.addPathPatterns("/**")
.excludePathPatterns(excludePath);
WebMvcConfigurer.super.addInterceptors(registry); }
7、运行测试,返回123,工作正常
以上参考
https://www.jianshu.com/p/df57fefe0ab7
https://www.jdon.com/51938
https://blog.csdn.net/MRA__S__/article/details/82051538
https://blog.csdn.net/damanchen/article/details/103770222
等
SpringBoot学习- 5、整合Redis的更多相关文章
- SpringBoot学习:整合Redis
项目下载地址:http://download.csdn.NET/detail/aqsunkai/9805821 pom.xml添加对redis的依赖: <!-- https://mvnrepos ...
- SpringBoot学习笔记:Redis缓存
SpringBoot学习笔记:Redis缓存 关于Redis Redis是一个使用ANSI C语言编写的免费开源.支持网络.可基于内存亦可以持久化的日志型.键值数据库.其支持多种存储类型,包括Stri ...
- SpringBoot学习之整合Mybatis
本博客使用IDEA开发工具,通过Maven构建SpringBoot项目,初始化项目添加的依赖有:spring-boot-starter-jdbc.spring-boot-starter-web.mys ...
- 【快学springboot】11.整合redis实现session共享
前言 这里都是基于前面的项目基础上的.springboot整合redis非常的方便,这也是springboot的宗旨,简化配置.这篇文章就教大家如何使用springboot整合redis来实现sess ...
- SpringBoot学习之整合Druid的简单应用
一.Druid介绍 Druid简介 Druid是目前Java语言中最好的数据库连接池之一.结合了 C3P0.DBCP 等 DB 池的优点,同时加入了日志监控.Druid 是一个分布式的.支持实时多维 ...
- SpringBoot缓存篇Ⅱ --- 整合Redis以及序列化机制
一.Redis环境搭建 系统默认是使用ConcurrentMapCacheManager,然后获取和创建ConcurrentMapCache类型的缓存组件,再将数据保存在ConcurrentMap中 ...
- SpringBoot学习之整合Swagger
Swagger介绍 1.什么是Swagger 作为后端程序开发,我们多多少少写过几个后台接口项目,不管是编写手机端接口,还是目前比较火热的前后端分离项目,前端与后端都是由不同的工程师进行开发,那么这之 ...
- redis(Springboot中封装整合redis,java程序如何操作redis的5种基本数据类型)
平常测试redis操作命令,可能用的是cmd窗口 操作redis,记录一下 java程序操作reids, 操作redis的方法 可以用Jedis ,在springboot 提供了两种 方法操作 Red ...
- 25、springboot与缓存整合Redis
默认使用ConcurrentMapCacheManager 将数据保存在下面的Map中 docker: 安装Redis: 查看官方文档: 添加约束 <dependency> <gro ...
- SpringBoot学习:整合Mybatis,使用HikariCP超高性能数据源
一.添加pom依赖jar包: <!--整合mybatis--> <dependency> <groupId>org.mybatis.spring.boot</ ...
随机推荐
- RadioButton改写的开关按钮
先上效果图: 这个控件其实是俩个RadioButton,通过样式就可以实现. 样式资源: <Style x:Key="Tong_Yong_RadioButtonStyle" ...
- 聊聊spring之贯穿全局的重要对象BeanDefinition
BeanDefinition 在 spring 中贯穿始终,spring 要根据 BeanDefinition 对象来实 例化 bean,只要把解析的标签,扫描的注解类封装成 BeanDefiniti ...
- 部署Nexus作为docker的私有仓库
目录 Docker搭建Nexus私有仓库... 1 一.安装部署... 1 1.安装... 2 2.访问网页端... 2 二.配置使用... 2 1.创建本地仓库... 2 2.docker配置... ...
- PHP0001:PHP环境搭建
1,本机域名解析 网站域名访问流程 配置阿帕奇服务器 的 路径 阿帕奇中添加 PHP 支持 一个简单的PHP 代码 检测PHP apache 语法 httpd -t apache 的启动 获取网站 ...
- JN_0013:win10快速回桌面
4.最后一种方法是最为实用的方法.按快捷键[windows键+D键],如下图所示,两键同时按,或者先按住windows键不放再按D键.这种方法在任何时候都是有用的,并且熟练使用后可以达到非常快的速度: ...
- MyBatis的延迟加载和缓存机制
延迟加载: 什么是延迟加载: MyBatis中的延迟加载,也称为懒加载,是指在进行关联查询时,按照设置延迟规则推迟对关联对象的select查询.延迟加载可以有效的减少数据库压力. MyBatis根据对 ...
- Sass环境安装-Sass sublime 编辑器插件编译方法
首先官网(http://www.ruby-lang.org/en/downloads/)下载 ruby (1)打开链接进入到下载页面,点击如下位置进行下载 (2)下载页面 (3)进入到各个版本的列表页 ...
- LAMP环境搭建与配置(3)
PHP配置 查看PHP配置文件的位置 # /usr/local/php/bin/php -i |grep -i "loaded configuration file" ...
- 报表平台发行说明(V0.0.0.1)
开发周期:共20天(2019-11-04~2019-11-23) 发布日期:2019-11-23 主要功能说明: 1 整体功能技术选型,前端(html+CSS+javascript)+Web API ...
- C++使用taskkill 命令强制结束进程
一:查看 taskkill 命令和参数的方法 window系统下,快捷键win + R 打开运行 ,输入cmd回车,在 cmd 里面输入: taskkill /? 二:语法: taskkill [/ ...