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是进程内的缓存框架,虽然它已经提供了集群环境下的缓存同步策略,这种同步仍然需 ...
随机推荐
- The Sultan's Successors UVA - 167
the squares thus selected sum to a number at least as high as one already chosen by the Sultan. (For ...
- d3.js 制作简单的俄罗斯方块
d3.js是一个不错的可视化框架,同时对于操作dom也是十分方便的.今天我们使用d3.js配合es6的类来制作一个童年小游戏--俄罗斯方块.话不多说先上图片. 1. js tetris类 由于方法拆分 ...
- 微信小程序一步一步获取UnionID,实现自动登录
思路: 1.小程序端获取用户ID,发送至后台 2.后台查询用户ID,如果找到了该用户,返回Token,没找到该用户,保存到数据库,并返回Token 小程序端如何获取用户ID: 小程序端 wx.getU ...
- RHEL7破解密码操作步骤
首先查看系统是什么版本 cat /etc/redhat-release 第1步:然后重启Linux系统并出现引导界面时,按下键盘上的e键进入内核编辑界面. 第2步:在Linux16 参数这行的最后面追 ...
- Java的异常:Error与Exception
一. 异常机制的概述 异常机制是指当程序出现错误后,程序如何处理.具体来说,异常机制提供了程序退出的安全通道.当出现错误后,程序执行的流程发生改变,程序的控制权转移到异常处理器. 程序错误分 ...
- Java 并发编程(二):如何保证共享变量的原子性?
线程安全性是我们在进行 Java 并发编程的时候必须要先考虑清楚的一个问题.这个类在单线程环境下是没有问题的,那么我们就能确保它在多线程并发的情况下表现出正确的行为吗? 我这个人,在没有副业之前,一心 ...
- PyTorch在笔记本上实现CUDA加速
最近刚开始学习深度学习,参考了一篇深度学习的入门文章,原文链接:https://medium.freecodecamp.org/everything-you-need-to-know-to-maste ...
- 创建多线程之threading.Thread的使用
1.threading模块 threading模块是众多多线程管理模块的其一,它能确保重要的子线程退出后进程才退出. multiprocess模块的完全模仿了threading模块的接口,二者在使用层 ...
- Termux 高级终端安装使用配置教程
Termux 高级终端安装使用配置教程,这篇文章拖了有小半年.因为网上相关的文章相对来说还是比较少的,恰好今天又刷了机,所以就特意来总结一下,希望本文可以帮助到其他的小伙伴.发挥Android平台更大 ...
- Windows7版本了解
1.win7简介Windows7是微软软件公司研发的标准版本,也就是所说的正版安装版或原版.安装版安装用时较长,安装步骤比较复杂,但是系统运行稳定.正版系统由微软刻成光盘出售,买品牌机,或一般大一点的 ...