The scripts are to be executed using the EVAL command in Redis.
These scripts will probably not work on Redis cluster since the keys used inside the script aren't all passed as arguments!
The layered filter has a maximum number of 32 layers. You can modify this in the source.
add.lua, cas.lua and layer-add.lua
The add.lua script adds a new element to the filter. It will create the filter when it doesn't exist yet.
cas.lua does a Check And Set, this will not add the element if it already exist.
cas.lua will return 0 if the element is added, or 1 if the element was already in the filter.
Since we use a scaling filter adding an element using add.lua might cause the element
to exist in multiple parts of the filter at the same time. cas.lua prevents this.
Using only cas.lua the :count key of the filter will accurately count the number of elements added to the filter.
Only using cas.lua will also lower the number of false positives by a small amount (less duplicates in the filter means less bits set).
layer-add.lua does a similar thing to cas.lua since this is necessary for the layer part to work
(need to check all the filters in a layer to see if it already exists in the layer).
layer-add.lua will return the layer number the element was added to.
These scripts expects 4 arguments.
The base name of the keys to use.
The initial size of the bloom filter (in number of elements).
The probability of false positives.
The element to add to the filter.
For example the following call would add "something" to a filter named test
which will initially be able to hold 10000 elements with a probability of false positives of 1%.
eval "add.lua source here" 0 test 10000 0.01 something
check.lua and layer-check.lua
The check.lua and layer-check.lua scripts check if an element is contained in the bloom filter.
layer-check.lua returns the layer the element was found in.
These scripts expects 4 arguments.
The base name of the keys to use.
The initial size of the bloom filter (in number of elements).
The probability of false positives.
The element to check for.
For example the following call would check if "something" is part of the filter named test
which will initially be able to hold 10000 elements with a probability of false positives of 1%.
eval "check.lua source here" 0 test 10000 0.01 something
请发表评论