Class: Flipper::Adapters::Dalli

Inherits:
Object
  • Object
show all
Includes:
Flipper::Adapter
Defined in:
lib/flipper/adapters/dalli.rb

Overview

Public: Adapter that wraps another adapter with the ability to cache adapter calls in Memcached using the Dalli gem.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Flipper::Adapter

#default_config, #export, #import, included, #name

Constructor Details

#initialize(adapter, cache, ttl = 0) ⇒ Dalli

Public



18
19
20
21
22
23
24
25
26
27
# File 'lib/flipper/adapters/dalli.rb', line 18

def initialize(adapter, cache, ttl = 0)
  @adapter = adapter
  @cache = cache
  @ttl = ttl

  @cache_version = 'v1'.freeze
  @namespace = "flipper/#{@cache_version}".freeze
  @features_key = "#{@namespace}/features".freeze
  @get_all_key = "#{@namespace}/get_all".freeze
end

Instance Attribute Details

#cacheObject (readonly)

Internal



12
13
14
# File 'lib/flipper/adapters/dalli.rb', line 12

def cache
  @cache
end

#ttlObject (readonly)

Public: The ttl for all cached data.



15
16
17
# File 'lib/flipper/adapters/dalli.rb', line 15

def ttl
  @ttl
end

Instance Method Details

#add(feature) ⇒ Object

Public



35
36
37
38
39
# File 'lib/flipper/adapters/dalli.rb', line 35

def add(feature)
  result = @adapter.add(feature)
  @cache.delete(@features_key)
  result
end

#clear(feature) ⇒ Object

Public



50
51
52
53
54
# File 'lib/flipper/adapters/dalli.rb', line 50

def clear(feature)
  result = @adapter.clear(feature)
  @cache.delete(key_for(feature.key))
  result
end

#disable(feature, gate, thing) ⇒ Object

Public



89
90
91
92
93
# File 'lib/flipper/adapters/dalli.rb', line 89

def disable(feature, gate, thing)
  result = @adapter.disable(feature, gate, thing)
  @cache.delete(key_for(feature.key))
  result
end

#enable(feature, gate, thing) ⇒ Object

Public



82
83
84
85
86
# File 'lib/flipper/adapters/dalli.rb', line 82

def enable(feature, gate, thing)
  result = @adapter.enable(feature, gate, thing)
  @cache.delete(key_for(feature.key))
  result
end

#featuresObject

Public



30
31
32
# File 'lib/flipper/adapters/dalli.rb', line 30

def features
  read_feature_keys
end

#get(feature) ⇒ Object

Public



57
58
59
60
61
# File 'lib/flipper/adapters/dalli.rb', line 57

def get(feature)
  @cache.fetch(key_for(feature.key), @ttl) do
    @adapter.get(feature)
  end
end

#get_allObject



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/flipper/adapters/dalli.rb', line 67

def get_all
  if @cache.add(@get_all_key, Time.now.to_i, @ttl)
    response = @adapter.get_all
    response.each do |key, value|
      @cache.set(key_for(key), value, @ttl)
    end
    @cache.set(@features_key, response.keys.to_set, @ttl)
    response
  else
    features = read_feature_keys.map { |key| Flipper::Feature.new(key, self) }
    read_many_features(features)
  end
end

#get_multi(features) ⇒ Object



63
64
65
# File 'lib/flipper/adapters/dalli.rb', line 63

def get_multi(features)
  read_many_features(features)
end

#remove(feature) ⇒ Object

Public



42
43
44
45
46
47
# File 'lib/flipper/adapters/dalli.rb', line 42

def remove(feature)
  result = @adapter.remove(feature)
  @cache.delete(@features_key)
  @cache.delete(key_for(feature.key))
  result
end