spring boot通过Jedis来操作redis
idea中新建spring boot项目,引入jedis依赖
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.1.0</version>
</dependency>
application.properties中自定义redis配置
#自定义redis配置
myredis.host=localhost
myredis.port=6379
myredis.timeout=10
myredis.pool-max-total=1000
myredis.pool-max-idle=500
myredis.pool-max-wait=500
自定义配置类MyRedisConfig.java读取redis配置文件信息,生成JedisPool类型的bean
package com.example.jedisredis.config; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig; @Configuration
@ConfigurationProperties(prefix = "myredis")
public class MyRedisConfig { private String host;
private Integer port;
private Integer timeout;
private Integer poolMaxTotal;
private Integer poolMaxIdle;
private Integer poolMaxWait; @Bean
public JedisPool jedisPoolFactory(){
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(poolMaxTotal);
jedisPoolConfig.setMaxIdle(poolMaxIdle);
jedisPoolConfig.setMaxWaitMillis(poolMaxWait * 1000);
JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout*1000);
return jedisPool;
} @Override
public String toString() {
return "MyRedisConfig{" +
"host='" + host + '\'' +
", port=" + port +
", timeout=" + timeout +
", poolMaxTotal=" + poolMaxTotal +
", poolMaxIdle=" + poolMaxIdle +
", poolMaxWait=" + poolMaxWait +
'}';
} public String getHost() {
return host;
} public void setHost(String host) {
this.host = host;
} public Integer getPort() {
return port;
} public void setPort(Integer port) {
this.port = port;
} public Integer getTimeout() {
return timeout;
} public void setTimeout(Integer timeout) {
this.timeout = timeout;
} public Integer getPoolMaxTotal() {
return poolMaxTotal;
} public void setPoolMaxTotal(Integer poolMaxTotal) {
this.poolMaxTotal = poolMaxTotal;
} public Integer getPoolMaxIdle() {
return poolMaxIdle;
} public void setPoolMaxIdle(Integer poolMaxIdle) {
this.poolMaxIdle = poolMaxIdle;
} public Integer getPoolMaxWait() {
return poolMaxWait;
} public void setPoolMaxWait(Integer poolMaxWait) {
this.poolMaxWait = poolMaxWait;
}
}
自定义工具类MyReidsUtil.java,注入MyReidsConfig.java中的JedisPool,通过JedisPool生成Jedis,从而构建操作redis的方法
package com.example.jedisredis.util; import com.example.jedisredis.config.MyRedisConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool; @Component
public class MyRedisUtil { @Autowired
JedisPool jedisPool; public String get(String key){
Jedis jedis = jedisPool.getResource();
String value = jedis.get(key);
return value;
} public void set(String key, String value){
Jedis jedis = jedisPool.getResource();
jedis.set(key, value);
}
}
测试
package com.example.jedisredis.controller; import com.example.jedisredis.util.MyRedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
@RequestMapping("/user")
public class UserController { @Autowired
MyRedisUtil myRedisUtil; @RequestMapping("/setname")
@ResponseBody
public String setName(){
myRedisUtil.set("name", "yanguobin");
return "setName";
} @RequestMapping("/getname")
@ResponseBody
public String getName(){
String name = myRedisUtil.get("name");
System.out.println("name:" + name);
return "getName";
}
}
最后,MyRedisUtil.java中的各种自定义的操作redis的方法可以写的丰满些,以方便使用,此处仅简单举例
spring boot通过Jedis来操作redis的更多相关文章
- Spring Boot 2.x 缓存应用 Redis注解与非注解方式入门教程
Redis 在 Spring Boot 2.x 中相比 1.5.x 版本,有一些改变.redis 默认链接池,1.5.x 使用了 jedis,而2.x 使用了 lettuce Redis 接入 Spr ...
- 阿里P7级教你如何在Spring Boot应用程序中使用Redis
在Spring Boot应用程序中使用Redis缓存的步骤: 1.要获得Redis连接,我们可以使用Lettuce或Jedis客户端库,Spring Boot 2.0启动程序spring-boot-s ...
- Spring Boot(二):数据库操作
本文主要讲解如何通过spring boot来访问数据库,本文会演示三种方式来访问数据库,第一种是JdbcTemplate,第二种是JPA,第三种是Mybatis.之前已经提到过,本系列会以一个博客系统 ...
- Jedis API操作redis数据库
1.配置文件 classpath路径下,新建redis.properties配置文件 配置文件内容 # Redis settings redis.host=127.0.0.1 redis.port=6 ...
- Spring Boot 1.5.4集成Redis
本文示例源码,请看这里: 如何安装与配置Redis,请看这里 首先添加起步依赖: <dependency> <groupId>org.springframework.boot& ...
- 【Spring Boot&&Spring Cloud系列】Spring Boot中使用NoSql数据库Redis
github地址:https://github.com/AndyFlower/Spring-Boot-Learn/tree/master/spring-boot-nosql-redis 一.加入依赖到 ...
- Spring Boot (五): Redis缓存使用姿势盘点
1. Redis 简介 Redis 是目前业界使用最广泛的内存数据存储.相比 Memcached,Redis 支持更丰富的数据结构,例如 hashes, lists, sets 等,同时支持数据持久化 ...
- spring boot下JedisCluster方式连接Redis集群的配置
最近在使用springboot做项目,使用redis做缓存.在外网开发的时候redis服务器没有使用集群配置,所有就是用了RedisTemplate的方式进行连接redis服务器.但是项目代码挪到内网 ...
- spring boot 学习(十四)SpringBoot+Redis+SpringSession缓存之实战
SpringBoot + Redis +SpringSession 缓存之实战 前言 前几天,从师兄那儿了解到EhCache是进程内的缓存框架,虽然它已经提供了集群环境下的缓存同步策略,这种同步仍然需 ...
随机推荐
- Heron and His Triangle 2017 沈阳区域赛
A triangle is a Heron’s triangle if it satisfies that the side lengths of it are consecutive integer ...
- 运行sudo apt-get install nginx时报错有几个软件包无法下载,要不运行 apt-get update 或者加上 --fix-missing 的选项再试试?解决
运行sudo apt-get install nginx时报错有几个软件包无法下载,要不运行 apt-get update 或者加上 --fix-missing 的选项再试试?解决办法 第一步:运行s ...
- Git学习-上传项目到github
现在流行把项目代码上传到git上,今天试了好久,终于成功上传到git了,特做点笔记. 准备工作 在github上注册一个账号,创建一个仓库. 创建好仓库,得到它的地址: 开始上传 一.新建一个文件夹, ...
- CAS ABA问题
java.util.concurrent包的最底层基础CAS技术,原理很简单. CAS有3个操作数,内存值V,旧的预期值A,要修改的新值B.当且仅当预期值A和内存值V相同时,将内存值V修改为B,否则什 ...
- Spring Boot 多环境如何配置
Spring Boot 开发环境.测试环境.预生产环境.生产环境多环境配置 通常一个公司的应程序可能在开发环境(dev).测试环境(test).生产环境(prod)中运行.那么是不是需要拷贝不同的安装 ...
- .Net基础篇_学习笔记_第六天_for循环语法_正序输出和倒序输出
for TAB 和 forr TAB using System; using System.Collections.Generic; using System.Linq; using System. ...
- java中的GC
1.GC发生在JVM中的堆区 2.GC是很么? 1.次数上频繁收集Young区 Minor GC 2.次数上较少收集Old区 Full GC 3.基本不动的Perm区 3.G ...
- DevExpress的TextEdit、RadioGroup、ColorPickEdit设置默认值
场景 Winform中实现ZedGraph的多条Y轴(附源码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1001322 ...
- 解决在Filter中读取Request中的流后,后续controller或restful接口中无法获取流的问题
首先我们来描述一下在开发中遇到的问题,场景如下: 比如我们要拦截所有请求,获取请求中的某个参数,进行相应的逻辑处理:比如我要获取所有请求中的公共参数 token,clientVersion等等:这个时 ...
- uboot学习之uboot启动流程简述
一.uboot启动分为了三个阶段BL0.BL1.BL2:BL0表示上电后运行ROM中固化的一段程序,其中ROM中的程序是厂家写进去的,所以具体功能可能根据厂家芯片而有所不同.功能如下: 初始化系统时钟 ...