Skip to content

Commit

Permalink
2024-12-03 18:49:17
Browse files Browse the repository at this point in the history
  • Loading branch information
yingzhuo committed Dec 3, 2024
1 parent 487abd6 commit 30ea75a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import spring.turbo.util.collection.CollectionUtils;
import spring.turbo.util.hash.BloomFilter;
import spring.turbo.util.hash.DigestHashFunction;
import spring.turbo.util.hash.HashFunction;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* 基于BITMAP实现的布隆过滤器
* 分布式布隆过滤器
*
* @author 应卓
* @since 3.4.0
*/
public class BloomFilter implements spring.turbo.util.hash.BloomFilter {
public class DistributedBloomFilter implements BloomFilter {

private static final int DEFAULT_BITMAP_SIZE = 10_0000_0000; // 十亿

Expand All @@ -26,13 +28,43 @@ public class BloomFilter implements spring.turbo.util.hash.BloomFilter {
private final String redisKey;
private final int bitmapSize;

/**
* 创建默认配置的布隆过滤器 <br>
* <ul>
* <li>长度: 10_0000_0000</li>
* <li>哈希函数1: MD5</li>
* <li>哈希函数2: SHA-1</li>
* <li>哈希函数3: SHA-256</li>
* <li>哈希函数4: SHA-384</li>
* <li>哈希函数5: SHA-512</li>
* </ul>
*
* @param redisOperations RedisOperations实例,通常是 {@link StringRedisTemplate}
* @param key Redis键
* @return 布隆过滤器实例
* @see HashFunction
* @see DigestHashFunction
*/
public static DistributedBloomFilter createDefault(
RedisOperations<String, String> redisOperations,
String key) {
return new DistributedBloomFilter(redisOperations, key, DEFAULT_BITMAP_SIZE)
.addHashFunctions(
DigestHashFunction.md5(),
DigestHashFunction.sha1(),
DigestHashFunction.sha256(),
DigestHashFunction.sha384(),
DigestHashFunction.sha512()
);
}

/**
* 构造方法
*
* @param redisOperations RedisOperations实例,通常是 {@link StringRedisTemplate}
* @param redisKey redis的键
*/
public BloomFilter(RedisOperations<String, String> redisOperations, String redisKey) {
public DistributedBloomFilter(RedisOperations<String, String> redisOperations, String redisKey) {
this(redisOperations, redisKey, DEFAULT_BITMAP_SIZE);
}

Expand All @@ -43,7 +75,7 @@ public BloomFilter(RedisOperations<String, String> redisOperations, String redis
* @param redisKey redis的键
* @param bitmapSize 底层bitmap长度
*/
public BloomFilter(RedisOperations<String, String> redisOperations, String redisKey, int bitmapSize) {
public DistributedBloomFilter(RedisOperations<String, String> redisOperations, String redisKey, int bitmapSize) {
Assert.notNull(redisOperations, "redisOperations is null");
Assert.hasText(redisKey, "redisKey is null or empty");
Assert.isTrue(bitmapSize >= 1000_0000, "bitmapSize should >= 10000000");
Expand Down Expand Up @@ -78,7 +110,7 @@ public List<HashFunction> getHashFunctions() {
* @param moreFunctions 多个其他哈希函数器
* @return this
*/
public BloomFilter addHashFunctions(HashFunction first, HashFunction... moreFunctions) {
public DistributedBloomFilter addHashFunctions(HashFunction first, HashFunction... moreFunctions) {
CollectionUtils.nullSafeAdd(hashFunctions, first);
CollectionUtils.nullSafeAddAll(hashFunctions, moreFunctions);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @author 应卓
* @since 3.4.0
*/
public final class ReentrantLock implements Serializable {
public final class DistributedReentrantLock implements Serializable {

/*
* 本工具没有在高并发下严格测试,作者只为了自学与教学。请谨慎在生产环境上使用。
Expand Down Expand Up @@ -53,7 +53,7 @@ public final class ReentrantLock implements Serializable {
* @param lockKey 作为锁的键
* @param ttlInSeconds 锁自动过期时间(秒)
*/
public ReentrantLock(RedisOperations<String, String> redisOperations, String lockKey, long ttlInSeconds) {
public DistributedReentrantLock(RedisOperations<String, String> redisOperations, String lockKey, long ttlInSeconds) {
Assert.notNull(redisOperations, "redisOperations is required");
Assert.hasText(lockKey, "lockKey is required");
Assert.isTrue(ttlInSeconds > 0, "ttlInSeconds must greater than 0");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* 锁栈
*
* @author 应卓
* @see ReentrantLock
* @see DistributedReentrantLock
* @since 3.4.0
*/
public final class LockStack implements Serializable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
*
* @author 应卓
* @see java.util.Timer
* @see ReentrantLock
* @see DistributedReentrantLock
* @since 3.4.0
*/
public final class RenewTask extends TimerTask {
final class RenewTask extends TimerTask {

private static final RedisScript<Boolean> RENEW_TTL =
RedisScript.of(new ClassPathResource("META-INF/Lock#renew-ttl.lua"), Boolean.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.Assert;
import spring.turbo.module.redis.bloomfilter.BloomFilter;
import spring.turbo.util.hash.DigestHashFunction;
import spring.turbo.util.hash.HashFunction;

/**
* Redis相关工具
Expand All @@ -29,58 +26,6 @@ private RedisUtils() {

// -----------------------------------------------------------------------------------------------------------------

/**
* 创建布隆过滤器
*
* @param redisOperations RedisOperations实例,通常是 {@link StringRedisTemplate}
* @param key Redis键
* @param length 布隆过滤器长度
* @param firstHashFunction 哈希函数
* @param moreHashFunctions 其他哈希函数
* @return 布隆过滤器实例
*/
public static BloomFilter createBloomFilter(
RedisOperations<String, String> redisOperations,
String key,
int length,
HashFunction firstHashFunction,
HashFunction... moreHashFunctions) {
return new BloomFilter(redisOperations, key, length)
.addHashFunctions(firstHashFunction, moreHashFunctions);
}

/**
* 创建默认配置的布隆过滤器 <br>
* <ul>
* <li>长度: 10_0000_0000</li>
* <li>哈希函数1: MD5</li>
* <li>哈希函数2: SHA-1</li>
* <li>哈希函数3: SHA-256</li>
* <li>哈希函数4: SHA-384</li>
* <li>哈希函数5: SHA-512</li>
* </ul>
*
* @param redisOperations RedisOperations实例,通常是 {@link StringRedisTemplate}
* @param key Redis键
* @return 布隆过滤器实例
* @see HashFunction
* @see DigestHashFunction
*/
public static BloomFilter createDefaultBloomFilter(
RedisOperations<String, String> redisOperations,
String key) {
return new BloomFilter(redisOperations, key, 10_0000_0000)
.addHashFunctions(
DigestHashFunction.md5(),
DigestHashFunction.sha1(),
DigestHashFunction.sha256(),
DigestHashFunction.sha384(),
DigestHashFunction.sha512()
);
}

// -----------------------------------------------------------------------------------------------------------------

/**
* 按照匹配模式删除 string 类型的数据
*
Expand Down

0 comments on commit 30ea75a

Please sign in to comment.