Class: Flipper::Adapters::ActiveSupportCacheStore

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

Overview

Public: Adapter that wraps another adapter with the ability to cache adapter calls in ActiveSupport::ActiveSupportCacheStore caches.

Constant Summary collapse

Version =
'v1'.freeze
Namespace =
"flipper/#{Version}".freeze
FeaturesKey =
"#{Namespace}/features".freeze
GetAllKey =
"#{Namespace}/get_all".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter, cache, expires_in: nil) ⇒ ActiveSupportCacheStore

Public



26
27
28
29
30
31
32
# File 'lib/flipper/adapters/active_support_cache_store.rb', line 26

def initialize(adapter, cache, expires_in: nil)
  @adapter = adapter
  @name = :active_support_cache_store
  @cache = cache
  @write_options = {}
  @write_options[:expires_in] = expires_in if expires_in
end

Instance Attribute Details

#cache ⇒ Object (readonly)

Internal



20
21
22
# File 'lib/flipper/adapters/active_support_cache_store.rb', line 20

def cache
  @cache
end

#name ⇒ Object (readonly)

Public: The name of the adapter.



23
24
25
# File 'lib/flipper/adapters/active_support_cache_store.rb', line 23

def name
  @name
end

Class Method Details

.key_for(key) ⇒ Object

Private



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

def self.key_for(key)
  "#{Namespace}/feature/#{key}"
end

Instance Method Details

#add(feature) ⇒ Object

Public



40
41
42
43
44
# File 'lib/flipper/adapters/active_support_cache_store.rb', line 40

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

#clear(feature) ⇒ Object

Public



55
56
57
58
59
# File 'lib/flipper/adapters/active_support_cache_store.rb', line 55

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

#disable(feature, gate, thing) ⇒ Object

Public



94
95
96
97
98
# File 'lib/flipper/adapters/active_support_cache_store.rb', line 94

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



87
88
89
90
91
# File 'lib/flipper/adapters/active_support_cache_store.rb', line 87

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

#features ⇒ Object

Public



35
36
37
# File 'lib/flipper/adapters/active_support_cache_store.rb', line 35

def features
  read_feature_keys
end

#get(feature) ⇒ Object

Public



62
63
64
65
66
# File 'lib/flipper/adapters/active_support_cache_store.rb', line 62

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

#get_all ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/flipper/adapters/active_support_cache_store.rb', line 72

def get_all
  if @cache.write(GetAllKey, Time.now.to_i, @write_options.merge(unless_exist: true))
    response = @adapter.get_all
    response.each do |key, value|
      @cache.write(key_for(key), value, @write_options)
    end
    @cache.write(FeaturesKey, response.keys.to_set, @write_options)
    response
  else
    features = read_feature_keys.map { |key| Flipper::Feature.new(key, self) }
    read_many_features(features)
  end
end

#get_multi(features) ⇒ Object



68
69
70
# File 'lib/flipper/adapters/active_support_cache_store.rb', line 68

def get_multi(features)
  read_many_features(features)
end

#remove(feature) ⇒ Object

Public



47
48
49
50
51
52
# File 'lib/flipper/adapters/active_support_cache_store.rb', line 47

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