项目结构:
pom.xml引用
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>platform-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.example</groupId>
<artifactId>redis-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>redis-client</name>
<description>redis-client</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
RedisBloom
package com.example.redisclient.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
/**
* 布隆过滤器
*/
@Component
public class RedisBloom {
@Autowired
private StringRedisTemplate redisTemplate;
private static RedisScript<Boolean> bfreserveScript = new DefaultRedisScript<>("if redis.call('bf.reserve', KEYS[1], ARGV[1], ARGV[2]) then return true else return false end", Boolean.class);
private static RedisScript<Boolean> bfaddScript = new DefaultRedisScript<>("return redis.call('bf.add', KEYS[1], ARGV[1])", Boolean.class);
private static RedisScript<Boolean> bfexistsScript = new DefaultRedisScript<>("return redis.call('bf.exists', KEYS[1], ARGV[1])", Boolean.class);
private static String bfmaddScript = "return redis.call('bf.madd', KEYS[1], %s)";
private static String bfmexistsScript = "return redis.call('bf.mexists', KEYS[1], %s)";
/**
* 设置错误率和大小(需要在添加元素前调用,若已存在元素,则会报错)
* 错误率越低,需要的空间越大
* @param key
* @param errorRate 错误率,默认0.01
* @param initialSize 默认100,预计放入的元素数量,当实际数量超出这个数值时,误判率会上升,尽量估计一个准确数值再加上一定的冗余空间
* @return
*/
public Boolean bfreserve(String key, double errorRate, int initialSize){
Boolean create = false;
try {
create = redisTemplate.execute(bfreserveScript, Arrays.asList(key), errorRate+"", initialSize+"");
}catch (Exception e){
if (e.getMessage().contains("ERR item exists")){
System.out.println("过滤器已经存在");
}
}
return create;
}
/**
* 添加元素,如果过滤器不存在则新建
* @param key
* @param value
* @return true表示添加成功,false表示添加失败(存在时会返回false)
*/
public Boolean bfadd(String key, String value){
return redisTemplate.execute(bfaddScript, Arrays.asList(key), value);
}
/**
* 查看元素是否存在(判断为存在时有可能是误判,不存在是一定不存在)
* @param key
* @param value
* @return true表示存在,false表示不存在
*/
public Boolean bfexists(String key, String value){
return redisTemplate.execute(bfexistsScript, Arrays.asList(key), value);
}
/**
* 批量添加元素
* @param key
* @param values
* @return 按序 1表示添加成功,0表示添加失败
*/
public List<Integer> bfmadd(String key, String... values){
return (List<Integer>)redisTemplate.execute(this.generateScript(bfmaddScript, values), Arrays.asList(key), values);
}
/**
* 批量检查元素是否存在(判断为存在时有可能是误判,不存在是一定不存在)
* @param key
* @param values
* @return 按序 1表示存在,0表示不存在
*/
public List<Integer> bfmexists(String key, String... values){
return (List<Integer>)redisTemplate.execute(this.generateScript(bfmexistsScript, values), Arrays.asList(key), values);
}
private RedisScript<List> generateScript(String script, String[] values) {
StringBuilder sb = new StringBuilder();
for(int i = 1; i <= values.length; i ++){
if(i != 1){
sb.append(",");
}
sb.append("ARGV[").append(i).append("]");
}
return new DefaultRedisScript<>(String.format(script, sb.toString()), List.class);
}
}
RedisLock
package com.example.redisclient.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Component;
import java.time.Duration;
import java.util.Collections;
@Component
public class RedisLock {
@Autowired
private StringRedisTemplate redisTemplate;
/**
* 加锁
* @param key 锁的key
* @param value 锁的value
* @param time 锁的过期时间(秒)
* @return
*/
public Boolean lock(String key,String value,Long time){
Boolean lockStatus = redisTemplate.opsForValue().setIfAbsent(key,value, Duration.ofSeconds(time));
return lockStatus;
}
/**
* 释放锁
* @param key
* @param value
* @return
*/
public Long unLock(String key,String value){
String luaScript = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
RedisScript<Long> redisScript = new DefaultRedisScript<>(luaScript,Long.class);
Long releaseStatus = redisTemplate.execute(redisScript, Collections.singletonList(key),value);
return releaseStatus;
}
}
RedisUtil
package com.example.redisclient.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Component
public class RedisUtil {
@Autowired
private RedisTemplate redisTemplate;
/**
* 保存字符串,带过期时间
* @param key
* @param value
* @param time
*/
public void set(String key, Object value, long time) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
}
/**
* 保存字符串,不带过期时间
* @param key
* @param value
*/
public void set(String key, Object value) {
redisTemplate.opsForValue().set(key, value);
}
/**
* 重新设置value而不更新过期时间
* @param key
* @param value
* @param offset
*/
public void setRange(String key, Object value,long offset) {
redisTemplate.opsForValue().set(key,value,offset);
}
/**
* 获取字符串值
* @param key
* @return
*/
public Object get(String key) {
return redisTemplate.opsForValue().get(key);
}
/**
* 删除为key的数据
* @param key
* @return
*/
public Boolean del(String key) {
return redisTemplate.delete(key);
}
/**
* 批量删除数据
* @param keys
* @return
*/
public Long del(List<String> keys) {
return redisTemplate.delete(keys);
}
/**
* 设置过期时间
* @param key
* @param time
* @return
*/
public Boolean expire(String key, long time) {
return redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
/**
* 获取过期时间
* @param key
* @return
*/
public Long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/**
* 判断是否有该key
* @param key
* @return
*/
public Boolean hasKey(String key) {
return redisTemplate.hasKey(key);
}
/**
* 按delta递增
* @param key
* @param delta
* @return
*/
public Long incr(String key, long delta) {
return redisTemplate.opsForValue().increment(key, delta);
}
/**
* 按delta递减
* @param key
* @param delta
* @return
*/
public Long decr(String key, long delta) {
return redisTemplate.opsForValue().increment(key, -delta);
}
/**
* 获取Hash结构中的值
* @param key
* @param hashKey
* @return
*/
public Object hGet(String key, String hashKey) {
return redisTemplate.opsForHash().get(key, hashKey);
}
/**
* 向Hash结构中放入一个值,并添加过期时间
* @param key
* @param hashKey
* @param value
* @param time
* @return
*/
public Boolean hSet(String key, String hashKey, Object value, long time) {
redisTemplate.opsForHash().put(key, hashKey, value);
return expire(key, time);
}
/**
* 向Hash结构中放入一个值
* @param key
* @param hashKey
* @param value
*/
public void hSet(String key, String hashKey, Object value) {
redisTemplate.opsForHash().put(key, hashKey, value);
}
/**
* 直接获取整个Hash结构
* @param key
* @return
*/
public Map<Object, Object> hGetAll(String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* 直接设置整个Hash结构,带过期时间
* @param key
* @param map
* @param time
* @return
*/
public Boolean hSetAll(String key, Map<String, Object> map, long time) {
redisTemplate.opsForHash().putAll(key, map);
return expire(key, time);
}
/**
* 直接设置整个Hash结构
* @param key
* @param map
*/
public void hSetAll(String key, Map<String, Object> map) {
redisTemplate.opsForHash().putAll(key, map);
}
/**
* 删除Hash结构中的值
* @param key
* @param hashKey
*/
public void hDel(String key, Object... hashKey) {
redisTemplate.opsForHash().delete(key, hashKey);
}
/**
* 判断Hash结构中是否有该值
* @param key
* @param hashKey
* @return
*/
public Boolean hHasKey(String key, String hashKey) {
return redisTemplate.opsForHash().hasKey(key, hashKey);
}
/**
* Hash结构中值递增
* @param key
* @param hashKey
* @param delta
* @return
*/
public Long hIncr(String key, String hashKey, Long delta) {
return redisTemplate.opsForHash().increment(key, hashKey, delta);
}
/**
* Hash结构中值递减
* @param key
* @param hashKey
* @param delta
* @return
*/
public Long hDecr(String key, String hashKey, Long delta) {
return redisTemplate.opsForHash().increment(key, hashKey, -delta);
}
/**
* 获取Set结构
* @param key
* @return
*/
public Set<Object> sMembers(String key) {
return redisTemplate.opsForSet().members(key);
}
/**
* 向Set结构中添加值
* @param key
* @param values
* @return
*/
public Long sAdd(String key, Object... values) {
return redisTemplate.opsForSet().add(key, values);
}
/**
* 向Set结构中添加值,并设置过期时间
* @param key
* @param time
* @param values
* @return
*/
public Long sAdd(String key, long time, Object... values) {
Long count = redisTemplate.opsForSet().add(key, values);
expire(key, time);
return count;
}
/**
* 是否为Set中的值
* @param key
* @param value
* @return
*/
public Boolean sIsMember(String key, Object value) {
return redisTemplate.opsForSet().isMember(key, value);
}
/**
* 获取Set结构的长度
* @param key
* @return
*/
public Long sSize(String key) {
return redisTemplate.opsForSet().size(key);
}
/**
* 删除Set结构中的属性
* @param key
* @param values
* @return
*/
public Long sRemove(String key, Object... values) {
return redisTemplate.opsForSet().remove(key, values);
}
/**
* 获取List结构中的值
* @param key
* @param start
* @param end
* @return
*/
public List<Object> lRange(String key, long start, long end) {
return redisTemplate.opsForList().range(key, start, end);
}
/**
* 获取List结构的长度
* @param key
* @return
*/
public Long lSize(String key) {
return redisTemplate.opsForList().size(key);
}
/**
* 根据索引获取List中的值
* @param key
* @param index
* @return
*/
public Object lIndex(String key, long index) {
return redisTemplate.opsForList().index(key, index);
}
/**
* 向List结构中添加值
* @param key
* @param value
* @return
*/
public Long lPush(String key, Object value) {
return redisTemplate.opsForList().rightPush(key, value);
}
/**
* 向List结构中添加值,并设置过期时间
* @param key
* @param value
* @param time
* @return
*/
public Long lPush(String key, Object value, long time) {
Long index = redisTemplate.opsForList().rightPush(key, value);
expire(key, time);
return index;
}
/**
* 向List结构中批量添加值
* @param key
* @param values
* @return
*/
public Long lPushAll(String key, Object... values) {
return redisTemplate.opsForList().rightPushAll(key, values);
}
/**
* 向List结构中批量添加值,并设置过期时间
* @param key
* @param time
* @param values
* @return
*/
public Long lPushAll(String key, Long time, Object... values) {
Long count = redisTemplate.opsForList().rightPushAll(key, values);
expire(key, time);
return count;
}
/**
* 从List结构中移除值
* @param key
* @param count
* @param value
* @return
*/
public Long lRemove(String key, long count, Object value) {
return redisTemplate.opsForList().remove(key, count, value);
}
/**
* 增加次数,value变,有效时间不变
* @param key
* @param time
*/
public void incrCount(String key, long time){
Long increment = redisTemplate.opsForValue().increment(key);
if(increment.intValue()==1){
this.set(key,increment,time);
}else {
this.setRange(key,increment,0);
}
}
}
RedisClientAutoConfig
package com.example.redisclient.config;
import com.example.redisclient.util.RedisBloom;
import com.example.redisclient.util.RedisLock;
import com.example.redisclient.util.RedisUtil;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisClientAutoConfig {
@Bean
@ConditionalOnMissingBean //默认的bean类型,具体的Conditional注解还有很多,有兴趣自己玩玩
public RedisLock redisLock(){
return new RedisLock();
}
@Bean
public RedisBloom redisBloom(){
return new RedisBloom();
}
@Bean
public RedisUtil redisUtil(){
return new RedisUtil();
}
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
redisTemplate.setConnectionFactory(redisConnectionFactory);
//key序列化
redisTemplate.setKeySerializer(redisSerializer);
//value序列化
redisTemplate.setValueSerializer(redisSerializer);
//value hashmap序列化
redisTemplate.setHashKeySerializer(redisSerializer);
//key hashmap序列化
redisTemplate.setHashValueSerializer(redisSerializer);
return redisTemplate;
}
}
打包
装载文件
resources下新建META-INF目录,新建spring.factories文件
spring.factories内容
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.redisclient.config.RedisClientAutoConfig
引用
sms项目引用redis-client
封装类的使用
@Autowired
private RedisUtil redisUtil;
@Autowired
private RedisBloom redisBloom;
@Autowired
private RedisLock redisLock;
/**
* redis封装类测试
* key和value请替换实际的键值对
*/
public void test(){
redisBloom.bfadd("key","value");
redisBloom.bfexists("key","value");
redisLock.lock("key","value",5L);
redisLock.unLock("key","value");
redisUtil.set("key","value");
redisUtil.get("key");
}