目录
- 前言
- Spring Boot如何集成Redis
- Spring Boot如何使用Redis
- 小结
前言
Redis全称Remote Dictionary Server(远程字典服务),它是一个基于内存实现的键值型非关系(NoSQL)数据库,由意大利人 Salvatore Sanfilippo 使用C语言编写。
Redis的性能极高,可以用来缓存一些经常被访问的数据或者需要耗费大量资源的内容,把这些内容放到Redis中时,能够让应用程序快速地读取它们,从而降低应用的压力,减少访问的延迟。
Spring Boot如何集成Redis
在项目pom.xml文件中引入Redis,依赖内容如下:
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- redis需要配合commons-pool2 对象池依赖 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
对应application.yml配置文件的配置:
//*******为redis的密码
redis:
host: 127.0.0.1
port: 6379
password: *******
timeout: 5000
lettuce:
pool:
max-active: 8
max-idle: 8
max-wait: 5000
min-idle: 0
默认数据库0,如果要指定数据库,使用配置:
//指定使用数据库1,redis自带16(0—15)个数据库(默认使用0库)
database: 1
Spring Boot如何使用Redis
先准备2个文件:Redis配置文件和Redis工具类。
Redis配置文件,一般放在api项目里面,文件名为RedisConfig,源码如下:
package com.***.api.conf;// ***为自己的项目
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @author /
*/
@Configuration
@EnableCaching
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
// 配置redisTemplate
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
// 配置连接工厂
lettuceConnectionFactory.setValidateConnection(true);
redisTemplate.setConnectionFactory(lettuceConnectionFactory);
// 设置序列化
StringRedisSerializer stringSerializer = new StringRedisSerializer();
GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
redisTemplate.setKeySerializer(stringSerializer);
redisTemplate.setValueSerializer(jsonRedisSerializer);
redisTemplate.setHashKeySerializer(stringSerializer);
redisTemplate.setHashValueSerializer(jsonRedisSerializer);
return redisTemplate;
}
}
Redis工具类,一般放在common项目里面,文件名为RedisUtil,源码如下:
package com.***.common.util;// ***为自己的项目
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* redis 工具类
*
* @author /
*/
@Component
public class RedisUtil {
@Resource
private RedisTemplate<String, Object> redisTemplate;
/**
* 指定缓存失效时间
*
* @param key 键
* @param time 时间(秒)
* @return true / false
*/
public boolean expire(String key, long time) {
try {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 根据 key 获取过期时间
*
* @param key 键
* @return /
*/
public Long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/**
* 判断 key 是否存在
*
* @param key 键
* @return true / false
*/
public Boolean hasKey(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 删除缓存
*
* @param key 键(一个或者多个)忽略类型转换警告
*/
@SuppressWarnings("unchecked")
public void del(String... key) {
if (key != null && key.length > 0) {
if (key.length == 1) {
redisTemplate.delete(key[0]);
} else {
// 传入一个 Collection<String> 集合
redisTemplate.delete(CollectionUtils.arrayToList(key));
}
}
}
/**
* 删除缓存集合
*
* @param keys 传入一个 Collection<String> 集合
*/
public void del(List<String> keys) {
redisTemplate.delete(keys);
}
// ============================== String ==============================
/**
* 普通缓存获取
*
* @param key 键
* @return 值
*/
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
/**
* 普通缓存放入
*
* @param key 键
* @param value 值
* @return true / false
*/
public boolean set(String key, Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 普通缓存放入并设置时间
*
* @param key 键
* @param value 值
* @param time 时间(秒),如果 time < 0 则设置无限时间
* @return true / false
*/
public boolean set(String key, Object value, long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 递增
*
* @param key 键
* @param delta 递增大小
* @return /
*/
public Long incr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递增因子必须大于 0");
}
return redisTemplate.opsForValue().increment(key, delta);
}
/**
* 递减
*
* @param key 键
* @param delta 递减大小
* @return /
*/
public Long decr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递减因子必须大于 0");
}
return redisTemplate.opsForValue().increment(key, delta);
}
// ============================== Map ==============================
/**
* HashGet
*
* @param key 键(no null)
* @param item 项(no null)
* @return 值
*/
public Object hGet(String key, String item) {
return redisTemplate.opsForHash().get(key, item);
}
/**
* 获取 key 对应的 map
*
* @param key 键(no null)
* @return 对应的多个键值
*/
public Map<Object, Object> hmGet(String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* HashSet
*
* @param key 键
* @param map 值
* @return true / false
*/
public boolean hmSet(String key, Map<Object, Object> map) {
try {
redisTemplate.opsForHash().putAll(key, map);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* HashSet 并设置时间
*
* @param key 键
* @param map 值
* @param time 时间
* @return true / false
*/
public boolean hmSet(String key, Map<Object, Object> map, long time) {
try {
redisTemplate.opsForHash().putAll(key, map);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 向一张 Hash表 中放入数据,如不存在则创建
*
* @param key 键
* @param item 项
* @param value 值
* @return true / false
*/
public boolean hSet(String key, String item, Object value) {
try {
redisTemplate.opsForHash().put(key, item, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 向一张 Hash表 中放入数据,并设置时间,如不存在则创建
*
* @param key 键
* @param item 项
* @param value 值
* @param time 时间(如果原来的 Hash表 设置了时间,这里会覆盖)
* @return true / false
*/
public boolean hSet(String key, String item, Object value, long time) {
try {
redisTemplate.opsForHash().put(key, item, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 删除 Hash表 中的值
*
* @param key 键
* @param item 项(可以多个,no null)
*/
public void hDel(String key, Object... item) {
redisTemplate.opsForHash().delete(key, item);
}
/**
* 判断 Hash表 中是否有该键的值
*
* @param key 键(no null)
* @param item 值(no null)
* @return true / false
*/
public boolean hHasKey(String key, String item) {
return redisTemplate.opsForHash().hasKey(key, item);
}
/**
* Hash递增,如果不存在则创建一个,并把新增的值返回
*
* @param key 键
* @param item 项
* @param by 递增大小 > 0
* @return
*/
public Double hIncr(String key, String item, Double by) {
return redisTemplate.opsForHash().increment(key, item, by);
}
/**
* Hash递减
*
* @param key 键
* @param item 项
* @param by 递减大小
* @return
*/
public Double hDecr(String key, String item, Double by) {
return redisTemplate.opsForHash().increment(key, item, -by);
}
// ============================== Set ==============================
/**
* 根据 key 获取 set 中的所有值
*
* @param key 键
* @return 值
*/
public Set<Object> sGet(String key) {
try {
return redisTemplate.opsForSet().members(key);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 从键为 key 的 set 中,根据 value 查询是否存在
*
* @param key 键
* @param value 值
* @return true / false
*/
public boolean sHasKey(String key, Object value) {
try {
return redisTemplate.opsForSet().isMember(key, value);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 将数据放入 set缓存
*
* @param key 键值
* @param values 值(可以多个)
* @return 成功个数
*/
public long sSet(String key, Object... values) {
try {
return redisTemplate.opsForSet().add(key, values);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 将数据放入 set缓存,并设置时间
*
* @param key 键
* @param time 时间
* @param values 值(可以多个)
* @return 成功放入个数
*/
public long sSet(String key, long time, Object... values) {
try {
long count = redisTemplate.opsForSet().add(key, values);
if (time > 0) {
expire(key, time);
}
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 获取 set缓存的长度
*
* @param key 键
* @return 长度
*/
public long sGetSetSize(String key) {
try {
return redisTemplate.opsForSet().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 移除 set缓存中,值为 value 的
*
* @param key 键
* @param values 值
* @return 成功移除个数
*/
public long setRemove(String key, Object... values) {
try {
return redisTemplate.opsForSet().remove(key, values);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
// ============================== List ==============================
/**
* 获取 list缓存的内容
*
* @param key 键
* @param start 开始
* @param end 结束(0 到 -1 代表所有值)
* @return
*/
public List<Object> lGet(String key, long start, long end) {
try {
return redisTemplate.opsForList().range(key, start, end);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 获取 list缓存的长度
*
* @param key 键
* @return 长度
*/
public long lGetListSize(String key) {
try {
return redisTemplate.opsForList().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* 根据索引 index 获取键为 key 的 list 中的元素
*
* @param key 键
* @param index 索引
* 当 index >= 0 时 {0:表头, 1:第二个元素}
* 当 index < 0 时 {-1:表尾, -2:倒数第二个元素}
* @return 值
*/
public Object lGetIndex(String key, long index) {
try {
return redisTemplate.opsForList().index(key, index);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 将值 value 插入键为 key 的 list 中,如果 list 不存在则创建空 list
*
* @param key 键
* @param value 值
* @return true / false
*/
public boolean lSet(String key, Object value) {
try {
redisTemplate.opsForList().rightPush(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 将值 value 插入键为 key 的 list 中,并设置时间
*
* @param key 键
* @param value 值
* @param time 时间
* @return true / false
*/
public boolean lSet(String key, Object value, long time) {
try {
redisTemplate.opsForList().rightPush(key, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 将 values 插入键为 key 的 list 中
*
* @param key 键
* @param values 值
* @return true / false
*/
public boolean lSetList(String key, List<Object> values) {
try {
redisTemplate.opsForList().rightPushAll(key, values);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 将 values 插入键为 key 的 list 中,并设置时间
*
* @param key 键
* @param values 值
* @param time 时间
* @return true / false
*/
public boolean lSetList(String key, List<Object> values, long time) {
try {
redisTemplate.opsForList().rightPushAll(key, values);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 根据索引 index 修改键为 key 的值
*
* @param key 键
* @param index 索引
* @param value 值
* @return true / false
*/
public boolean lUpdateIndex(String key, long index, Object value) {
try {
redisTemplate.opsForList().set(key, index, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 在键为 key 的 list 中删除值为 value 的元素
*
* @param key 键
* @param count 如果 count == 0 则删除 list 中所有值为 value 的元素
* 如果 count > 0 则删除 list 中最左边那个值为 value 的元素
* 如果 count < 0 则删除 list 中最右边那个值为 value 的元素
* @param value
* @return
*/
public long lRemove(String key, long count, Object value) {
try {
return redisTemplate.opsForList().remove(key, count, value);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
}
Redis使用举例:
比如,把用户登录的token信息放入Redis,从而提高登录访问的性能。
只要使用RedisUtil工具中,有关String字符串的函数来处理即可,可以使用放入缓存set和从缓存获取get这2个函数,代码单独摘出来了,如下:
/**
* 普通缓存获取
*
* @param key 键
* @return 值
*/
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
/**
* 普通缓存放入并设置时间
*
* @param key 键
* @param value 值
* @param time 时间(秒),如果 time < 0 则设置无限时间
* @return true / false
*/
public boolean set(String key, Object value, long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
token信息放入Redis:
// 86400L:token过期时间,单位为秒,如设置为1天
redisUtil.set("login:token:" + userId, token, 86400L);
从Redis中获取token信息:
Object obj = redisUtil.get("login:token:" + userId);
再比如,APP端,一般都会使用手机号码进行注册和登录,使用手机号码时,都会对接短信服务获取验证码,获取验证码一般都会使用一些限制,比如,一次短信验证码5分钟内有效,同一个号码每天不能发送超过10次等等。
由于,发送验证码的动作可能非常地频繁,我们也可以选择把获取验证码相关的操作放Redis里面来缓存,从而提高短信业务的性能。
短信验证码信息放入Redis:
// 验证码5分钟之内有效
redisUtil.set("sms:code:" + mobile, code, 300L);
// 1分钟之内不能重发
redisUtil.set("sms:resend:" + mobile, new Date(), 60L);
// 每天最多10次
// 判断是否达到10次 格式 sms:incr:130****8888:20221109
String incrKey = "sms:incr:" + mobile + ":" + DateUtil.format(new Date(), DatePattern.PURE_DATE_FORMAT);
redisUtil.incr(incrKey, 1L);
// 1天之后自动过期
redisUtil.expire(incrKey, 86400L);
从Redis中获取短信验证码信息:
// 获取短信验证码
Object obj = redisUtil.get("sms:code:" + mobile);
// 成功后 删除短信缓存,从而解除重发限制
redisUtil.del("sms:code:" + mobile, "sms:resend:" + mobile);
// 判断是否达到10次
String incrKey = "sms:incr:" + mobile + ":" + DateUtil.format(new Date(), DatePattern.PURE_DATE_FORMAT);
Object obj = redisUtil.get(incrKey);
// 判断是否超过了一分钟
Boolean bResend = redisUtil.hasKey("sms:resend:" + mobile);
小结
Redis使用的好,真的能够帮助我们的项目,解决很多的性能问题,我们要善于去发现能够使用Redis的场景,灵活使用Redis工具类里面的功能。
大家使用起来吧。