Created
July 9, 2020 12:20
-
-
Save gauravat16/2dacf1eae25bd8a350766d52ff41a98b to your computer and use it in GitHub Desktop.
Redisson bloom filter service and data setup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package gaurav.examples.redis.bloomfilter.service.impl; | |
import gaurav.examples.redis.bloomfilter.service.BloomFilterService; | |
import lombok.Synchronized; | |
import lombok.extern.log4j.Log4j2; | |
import org.ajbrown.namemachine.NameGenerator; | |
import org.redisson.api.RBloomFilter; | |
import org.redisson.api.RedissonClient; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.boot.context.event.ApplicationReadyEvent; | |
import org.springframework.context.event.EventListener; | |
import org.springframework.stereotype.Service; | |
@Log4j2 | |
@Service | |
public class BloomFilterServiceImpl implements BloomFilterService { | |
@Autowired | |
private RedissonClient redissonClient; | |
private RBloomFilter<String> userNameBloomFilter; | |
@EventListener(ApplicationReadyEvent.class) | |
@Override | |
public void loadAllDataInBloomFilter() { | |
RBloomFilter<String> stringRBloomFilter = getUserNameBloomFilter(); | |
//Generate random names | |
NameGenerator nameGenerator = new NameGenerator(); | |
nameGenerator.generateNames(9999).forEach(name -> { | |
log.info("Name generated - " + name.getFirstName()); | |
stringRBloomFilter.add(name.getFirstName()); | |
}); | |
} | |
@Override | |
@Synchronized | |
public RBloomFilter<String> getUserNameBloomFilter() { | |
if (null == userNameBloomFilter) { | |
RBloomFilter<String> stringRBloomFilter = redissonClient.getBloomFilter("usernames"); | |
//Set expectedInsertions and falseProbability | |
stringRBloomFilter.tryInit(99999, 0.001); | |
userNameBloomFilter = stringRBloomFilter; | |
} | |
return userNameBloomFilter; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment