Docker Compose
version: '3'
services:
redis:
image: redis/redis-stack
container_name: redis
ports:
- 6379:6379
redis-insight:
image: redislabs/redisinsight
container_name: redis-insight
ports:
- 8001:8001
Run following command:
docker-compose up -d
登录http://localhost:8001/
利用redis的RBloomFilter实现元素的是否存在的判断
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springboot-demo</artifactId>
<groupId>com.et</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>BloomFilter</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.16.1</version>
</dependency>
</dependencies>
</project>
package com.et.config;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
@Configuration
public class RedissonConfig {
@Value("${redisson.redis.address}")
private String address;
@Value("${redisson.redis.password}")
private String password;
@Bean
public Config redissionConfig() {
Config config = new Config();
SingleServerConfig singleServerConfig = config.useSingleServer();
singleServerConfig.setAddress(address);
if (!StringUtils.isEmpty(password)) {
singleServerConfig.setPassword(password);
}
return config;
}
@Bean
public RedissonClient redissonClient() {
return Redisson.create(redissionConfig());
}
}
package com.et;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import java.util.*;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);
RedissonClient redisson = context.getBean(RedissonClient.class);
RBloomFilter bf = redisson.getBloomFilter("test-bloom-filter");
bf.tryInit(100000L, 0.03);
Set<String> set = new HashSet<String>(1000);
List<String> list = new ArrayList<String>(1000);
//Fill the Bloom filter with data. To test the reality, we recorded 1000 uuids and another 9000 as interference data.
for (int i = 0; i < 10000; i++) {
String uuid = UUID.randomUUID().toString();
if(i<1000){
set.add(uuid);
list.add(uuid);
}
bf.add(uuid);
}
int wrong = 0; // Number of false positives by the Bloom filter
int right = 0;// Bloom filter correct times
for (int i = 0; i < 10000; i++) {
String str = i % 10 == 0 ? list.get(i / 10) : UUID.randomUUID().toString();
if (bf.contains(str)) {
if (set.contains(str)) {
right++;
} else {
wrong++;
}
}
}
//right is 1000
System.out.println("right:" + right);
//Because the error rate is 3%, the wrong value of 10,000 data is about 30.
System.out.println("wrong:" + wrong);
//Filter remaining space size
System.out.println(bf.count());
}
}
redisson.redis.address=redis://127.0.0.1:6379 redisson.redis.password=
right:1000
wrong:15
29873