springboot(七).springboot整合jedis实现redis缓存
我们在使用springboot搭建微服务的时候,在很多时候还是需要redis的高速缓存来缓存一些数据,存储一些高频率访问的数据,如果直接使用redis的话又比较麻烦,在这里,我们使用jedis来实现redis缓存来达到高效缓存的目的,接下来,让我们一起来使用jedis来实现redis缓存
1.在pom.xml文件中添加依赖
<!-- jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.2</version>
</dependency>
2. 在springboot的配置文件中加入redis的配置信息
#redis jedis配置
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=10.100.140.84
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
#spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=200
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0
#spring-session 使用
spring.session.store-type=none
3.创建jedis配置文件,配置文件的作用是在项目启动的时候将jedis注入,接着我们就可以在其他类中获取到JedisPool类的信息
@Configuration
public class JedisConfig extends CachingConfigurerSupport{
private Logger logger = LoggerFactory.getLogger(JedisConfig.class); /**
* SpringSession 需要注意的就是redis需要2.8以上版本,然后开启事件通知,在redis配置文件里面加上
* notify-keyspace-events Ex
* Keyspace notifications功能默认是关闭的(默认地,Keyspace 时间通知功能是禁用的,因为它或多或少会使用一些CPU的资源)。
* 或是使用如下命令:
* redis-cli config set notify-keyspace-events Egx
* 如果你的Redis不是你自己维护的,比如你是使用阿里云的Redis数据库,你不能够更改它的配置,那么可以使用如下方法:在applicationContext.xml中配置
* <util:constant static-field="org.springframework.session.data.redis.config.ConfigureRedisAction.NO_OP"/>
* @return
*/ @Value("${spring.redis.host}")
private String host; @Value("${spring.redis.port}")
private int port; @Value("${spring.redis.timeout}")
private int timeout; @Value("${spring.redis.pool.max-active}")
private int maxActive; @Value("${spring.redis.pool.max-idle}")
private int maxIdle; @Value("${spring.redis.pool.min-idle}")
private int minIdle; @Value("${spring.redis.pool.max-wait}")
private long maxWaitMillis; @Bean
public JedisPool redisPoolFactory(){
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
jedisPoolConfig.setMaxTotal(maxActive);
jedisPoolConfig.setMinIdle(minIdle);
JedisPool jedisPool = new JedisPool(jedisPoolConfig,host,port,timeout,null); logger.info("JedisPool注入成功!");
logger.info("redis地址:" + host + ":" + port);
return jedisPool;
} }
接下来,我们启动项目,gogogo

Jedis注入成功了!!!
然后我们可以定义一个工具类,用于对redis进行各种操作

这里我们通过@Autowired将JedisPool注入到工具类中,其他两个是命名、序列化的类
这里,我们列举一下平常最常用的几个方法
//给某个key设值
public void set(String key, Object value) {
Jedis client = getJedis();
try {
byte[] keyBytes = serializer.serializeKey(keyNamingPolicy.getKeyName(key));
byte[] valueBytes = serializer.serializeValue(value);
client.set(keyBytes, valueBytes);
} finally {
returnJedis(client);
} } //根据key获取value
public Object get(String key) {
Jedis client = getJedis();
try {
byte[] keyBytes = serializer.serializeKey(keyNamingPolicy.getKeyName(key));
byte[] valueBytes = client.get(keyBytes);
return serializer.deserializeValue(valueBytes);
} finally {
returnJedis(client);
}
} //根据键值获取value
public Object hashGet(String key, String field) {
Jedis client = getJedis();
try {
byte[] keyBytes = serializer.serializeKey(keyNamingPolicy.getKeyName(key));
byte[] fieldBytes = serializer.serializeKey(field);
byte[] valueBytes = client.hget(keyBytes, fieldBytes);
return serializer.deserializeValue(valueBytes);
} finally {
returnJedis(client);
} } public void hashSet(String key, String field, Object value) {
Jedis client = getJedis();
try {
byte[] keyBytes = serializer.serializeKey(keyNamingPolicy.getKeyName(key));
byte[] fieldBytes = serializer.serializeKey(field);
byte[] valueBytes = serializer.serializeValue(value);
client.hset(keyBytes, fieldBytes, valueBytes);
} finally {
returnJedis(client);
} } public Map<String, Object> hashAllGet(String key) {
Jedis client = getJedis();
try {
byte[] keyBytes = serializer.serializeKey(keyNamingPolicy.getKeyName(key));
Map<byte[], byte[]> map = client.hgetAll(keyBytes);
Map<String, Object> valueMap = Maps.newHashMap();
Set<Map.Entry<byte[], byte[]>> valueSet = map.entrySet();
for (Map.Entry<byte[], byte[]> entry : valueSet) {
valueMap.put(serializer.deserializeKey(entry.getKey()), serializer.deserializeValue(entry.getValue()));
}
return valueMap;
} finally {
returnJedis(client);
} } //判断key是否存在
public boolean existKey(String key) {
Jedis client = getJedis();
try {
byte[] keyBytes = serializer.serializeKey(keyNamingPolicy.getKeyName(key));
return client.exists(keyBytes);
} finally {
returnJedis(client);
}
}
在该项目中,我们发送短信验证码后会将验证码存在redis中去,然后校验的时候会判断用户输入的验证码和缓存中的验证码是否一致,达到目的。


springboot(七).springboot整合jedis实现redis缓存的更多相关文章
- spring-boot的spring-cache中的扩展redis缓存的ttl和key名
原文地址:spring-boot的spring-cache中的扩展redis缓存的ttl和key名 前提 spring-cache大家都用过,其中使用redis-cache大家也用过,至于如何使用怎么 ...
- SpringBoot(七)-SpringBoot JPA-Hibernate
步骤 1.在pom.xml添加mysql,spring-data-jpa依赖2.在application.properties文件中配置mysql连接配置文件3.在application.proper ...
- 使用Jedis操作redis 缓存
案例:http://blog.csdn.net/linlzk/article/details/41801391 Redis是一个开源的Key-Value数据缓存,和Memcached类似. Redis ...
- Java通过jedis操作redis缓存
package com.wodexiangce.util; import java.util.Set; import redis.clients.jedis.Jedis; /** * redis工具类 ...
- SpringBoot(七) SpringBoot中的缓存机制
随着时间的积累,应用的使用用户不断增加,数据规模也越来越大,往往数据库查询操作会成为影响用户使用体验的瓶颈,此时使用缓存往往是解决这一问题非常好的手段之一.Spring 3开始提供了强大的基于注解的缓 ...
- SpringBoot(七):SpringBoot中如何使用过滤器(Filter)?
方式一: 通过注解方式实现: 1.编写一个Servlet3的注解过滤器(和上一章Servlet相似) 贴代码: package com.example.springbootweb.filter; im ...
- redis集群配置,spring整合jedis,缓存同步
前台的商品数据(图片等加载缓慢)查询,先从redis缓存查询数据. redis是一个nosql数据库,内存版数据库,读取速度11w/s.本身具有内存淘汰机制,是单线程服务器(分时操作系统),线程安全. ...
- SpringBoot系列: SpringBoot Web项目中使用Shiro
注意点有:1. 不要启用 spring-boot-devtools, 如果启用 devtools 后, 不管是热启动还是手工重启, devtools总是试图重新恢复之前的session数据, 很有可能 ...
- 转:Redis 缓存策略
转:http://api.crap.cn/index.do#/web/article/detail/web/ARTICLE/7754a002-6400-442d-8dc8-e76e72d948ac 目 ...
随机推荐
- MySQL 聚合函数(四)检测功能依赖
源自MySQL 5.7 官方手册:12.20.4 Detection of Functional Dependence 本节提供了MySQL检测功能依赖的方式的几个示例.这些示例使用此表示法: {X} ...
- nginx反向代理服务器以及负载均衡,从安装到配置
nginx的具体作用不用细说,很强大,做负载均衡.反向代理服务器解决前端跨域问题等等.下面是nginx的安装过程 首先nginx主要的依赖: pcre. pcre-devel zlib zlib-de ...
- image的路径写法格式
if (MapGrid.Visibility == Visibility.Visible) { this.MapGrid.Visibility = Visibility.Collapsed; ...
- RedHat7 配置yum源
今天需要搭建一个测试环境,没办法只能找了个Linux服务器,但是之前的其他同事弄过是其他系统的,不是centos的,所以只能自己搞. 合计直接百度,怎么安装docker,结果一直报错,下载失败之类的 ...
- 选项卡TAB
一.基础信息 关键class名:nav 写法: (1)头部选中状态:class="active" (2)头部按钮进行切换:<a>加data-toggle="t ...
- LEANGOO卡片
转自:https://www.leangoo.com/leangoo_guide/leangoo_cards.html#toggle-id-10 Leangoo的卡片可以是需求.目标.任务.问题.缺陷 ...
- 如何成为优秀的技术Leader
技术主管,又叫技术经理,英文一般是 Tech Leader ,简称 TL.随着工作经验的不断积累,能力的不断提升,每个人都有机会成为 Team Leader. 然而在机会到来前,我们必须提前做好准备, ...
- Chrome开发者工具中Elements(元素)断点的用途
SAP Engagement Center UI的这个按钮会每秒钟刷新一次,显示页面已经打开了多长时间. 需求:需要找到哪行JavaScript代码不断刷新的按钮文字. 按照经验判断,这个文字肯定是一 ...
- JavaSpring【三、Bean】
配置项 id bean的标识 class bean的类全名 scope bean的作用域 constructor-arg 构造注入 properties 设值注入 autowire 装配模式 lazy ...
- HTML5学习:缩略图
HTML代码 <div> <img src="img/pic.png" alt="This is a pic" /> </div& ...