Class: FastExists::Backends::RedisBloom

Inherits:
Base
  • Object
show all
Defined in:
lib/fast_exists/backends/redis_bloom.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods inherited from Base

#load, #rebuild, #save

Constructor Details

#initialize(options = {}) ⇒ RedisBloom

Returns a new instance of RedisBloom.



8
9
10
11
12
13
# File 'lib/fast_exists/backends/redis_bloom.rb', line 8

def initialize(options = {})
  super
  @redis = options[:redis] || FastExists.configuration.redis
  @filter_key = options[:namespace] ? "fast_exists:bf:#{options[:namespace]}" : "fast_exists:bf:default"
  ensure_filter_reserved
end

Instance Attribute Details

#filter_keyObject (readonly)

Returns the value of attribute filter_key.



6
7
8
# File 'lib/fast_exists/backends/redis_bloom.rb', line 6

def filter_key
  @filter_key
end

Instance Method Details

#add(element) ⇒ Object



15
16
17
18
19
# File 'lib/fast_exists/backends/redis_bloom.rb', line 15

def add(element)
  execute_redis do |client|
    client.call(["BF.ADD", @filter_key, element.to_s]) == 1
  end
end

#capacityObject



40
41
42
# File 'lib/fast_exists/backends/redis_bloom.rb', line 40

def capacity
  @expected_elements
end

#clearObject



27
28
29
30
31
32
33
# File 'lib/fast_exists/backends/redis_bloom.rb', line 27

def clear
  execute_redis do |client|
    client.del(@filter_key)
  end
  ensure_filter_reserved
  true
end

#contains?(element) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
# File 'lib/fast_exists/backends/redis_bloom.rb', line 21

def contains?(element)
  execute_redis do |client|
    client.call(["BF.EXISTS", @filter_key, element.to_s]) == 1
  end
end

#countObject



35
36
37
38
# File 'lib/fast_exists/backends/redis_bloom.rb', line 35

def count
  info = stats
  info[:inserted_items] || 0
end

#statsObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fast_exists/backends/redis_bloom.rb', line 44

def stats
  execute_redis do |client|
    raw_info = client.call(["BF.INFO", @filter_key]) rescue []
    info_hash = Hash[*raw_info] rescue {}
    {
      backend: :redis_bloom,
      filter_key: @filter_key,
      expected_elements: @expected_elements,
      false_positive_rate: @false_positive_rate,
      inserted_items: info_hash["Number of items inserted"] || 0,
      capacity: info_hash["Capacity"] || @expected_elements,
      bytes: info_hash["Size"] || 0
    }
  end
end