package com.fdkankan.redis.util;

import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

@Component
@ConditionalOnProperty(name = "spring.redis.bloom", havingValue = "true")
public class RedisBloomUtils {

    @Autowired
    private RedissonClient redissonClient;

    public void add(String key,String value){
        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(key);
        bloomFilter.tryInit(500000,0.01);
        bloomFilter.add(value);
    }

    public boolean contains(String key,String value){
        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(key);
        bloomFilter.tryInit(500000,0.01);
        return bloomFilter.contains(value);
    }

    public void delete(String key){
        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(key);
        bloomFilter.tryInit(500000,0.01);
        bloomFilter.delete();
    }


}
