Redisson Bloom filter

RedissonClient configuration is the same as before, first use Redis stand-alone mode,

1. Define an API interface,

@RestController
public class BloomController {
    @Resource
    private BloomService bloomService;

    @GetMapping("ifExists")
    public String ifExists(String word) {
        bloomService.ifExists(word);
        return "ifExists";
    }
}

2. Service interface:

public interface BloomService {

    public void ifExists(String word);

}

3. Service implementation class:

@Slf4j
@Service
public class BloomServiceImpl implements BloomService {
    @Resource
    private RedissonClient redissonClient;

    @Override
    public void ifExists(String word) {
        RBloomFilter<String> bloomFilter = redissonClient.getBloomFilter("wordList");
        bloomFilter.tryInit(100000000L,0.03);
        bloomFilter.add(word);
        boolean result = bloomFilter.contains("Cindy");
        log.info(String.valueOf(result));
        result = bloomFilter.contains(word);
        log.info(String.valueOf(result));
    }
}

4. Call the API interface, http://localhost:8080/ifExists?word=Blair

Console output:

c.e.r.d.Service.impl.BloomServiceImpl    : false
c.e.r.d.Service.impl.BloomServiceImpl    : true

 

 

Guess you like

Origin blog.csdn.net/suoyx/article/details/114433607