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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Flipper::Adapter

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

Constructor Details

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

Public



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

def initialize(adapter, cache, expires_in: nil, write_through: false)
  @adapter = adapter
  @cache = cache
  @write_options = {}
  @write_options[:expires_in] = expires_in if expires_in
  @write_through = write_through

  @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



13
14
15
# File 'lib/flipper/adapters/active_support_cache_store.rb', line 13

def cache
  @cache
end

Instance Method Details

#add(feature) ⇒ Object

Public



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

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

#clear(feature) ⇒ Object

Public



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

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

#disable(feature, gate, thing) ⇒ Object

Public



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/flipper/adapters/active_support_cache_store.rb', line 101

def disable(feature, gate, thing)
  result = @adapter.disable(feature, gate, thing)

  if @write_through
    @cache.write(key_for(feature.key), @adapter.get(feature), @write_options)
  else
    @cache.delete(key_for(feature.key))
  end

  result
end

#enable(feature, gate, thing) ⇒ Object

Public



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/flipper/adapters/active_support_cache_store.rb', line 88

def enable(feature, gate, thing)
  result = @adapter.enable(feature, gate, thing)

  if @write_through
    @cache.write(key_for(feature.key), @adapter.get(feature), @write_options)
  else
    @cache.delete(key_for(feature.key))
  end

  result
end

#featuresObject

Public



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

def features
  read_feature_keys
end

#get(feature) ⇒ Object

Public



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

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

#get_allObject



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

def get_all
  if @cache.write(@get_all_key, 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(@features_key, 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



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

def get_multi(features)
  read_many_features(features)
end

#remove(feature) ⇒ Object

Public



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/flipper/adapters/active_support_cache_store.rb', line 42

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

  if @write_through
    @cache.write(key_for(feature.key), default_config, @write_options)
  else
    @cache.delete(key_for(feature.key))
  end

  result
end