Installing an expansion https://blog.csdn.net/maoyuanming0806/article/details/102798976
Simple to use in the laravel
Lua scripts up
$sh=<<<LUA return redis.call('bf.add',KEYS[1],KEYS[2]) LUA;
$result=Redis::eval($sh,2,"java","php");
The principle:
It depends on the bit map, resolved to rely Hash collision Hash value calculated a plurality of times
In java an int variable occupy 8 bytes ( 64 -bit machines) is a 64 binary code composed so on storage 64 bits of information, storing one hundred million calculated on the information of 100 million / 64 * 8 / 1024 / 1024 ≈ 11 MB
But it is a certain error rate, reduce the error rate is to increase the space and each additional Hash value calculation
JAVA simple implementation
Package the BF; Import Classes in java.util *. ; class BloomFilter { Private static Final int of DEFAULT_SIZE << 2 = 29; // open space Private static Final int [] = Seeds new new int [] {. 5,. 7,. 11, 13 is, 31 is , 37, 61,22,2,77,3}; // calculate eight different Hash value Private BitSet to bits = new new BitSet to (of DEFAULT_SIZE); // Bit implementation class Private SimpleHash [] = FUNC new new SimpleHash [Seeds. length]; public BloomFilter () { // constructors out prior to simulate individual 8 for ( int I = 0; I <seeds.length; I ++ ) { FUNC [I] = new new SimpleHash (of DEFAULT_SIZE, Seeds [I]); } } public void the Add (String value) { // add a string to the cloth Long vessel for (SimpleHash F: FUNC) { // cycle each calculate a hash value bits.set (f.hash (value), to true ); // the corresponding hash bit is set. 1 } } public Boolean the contains ( value string) { // determines whether the character string adding too iF (value == null ) { return to false ; } Boolean RET = to true ; for (SimpleHash F: FUNC) { // . 8 personal judgment, there is not seen a person does not exist RET RET = && bits.get (f.hash (value)); } return RET ; } // internal class, simpleHash public static class SimpleHash { Private int CAP; // bitmap length Private int SEED; // the hash value calculated public SimpleHash ( int CAP, int SEED) { // constructor assignment the this .cap = CAP; the this .seed = SEED; } public int the hash (String value) { // calculated Hash value int Result = 0 ; int len = value.length (); // calculate the length of string for ( int I = 0; I <len; I ++ ) { Result = Result + value.charAt SEED * (I); // Switch to ascii code } return (CAP -. 1) & Result; // binary aND operation } } public static void main(String[] args) { BloomFilter bf = new BloomFilter(); bf.add("java"); for (int i=0;i<100000000;i++){ bf.add(i+"n"); } int count=0; boolean isOk=false; for (int i=0;i<100000000;i++){ isOk=bf.contains(UUID.randomUUID().toString()+"nphp"); if (isOk){ System.out.println("OK"); count++; } } System.out.println(bf.contains("java")); System.out.println(count); } }
!!!