Class: Flipper::Adapters::Memoizable
- Inherits:
-
Object
- Object
- Flipper::Adapters::Memoizable
- Includes:
- Flipper::Adapter
- Defined in:
- lib/flipper/adapters/memoizable.rb
Overview
Internal: Adapter that wraps another adapter with the ability to memoize adapter calls in memory. Used by flipper dsl and the memoizer middleware to make it possible to memoize adapter calls for the duration of a request.
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Internal: The adapter this adapter is wrapping.
-
#cache ⇒ Object
readonly
Internal.
Instance Method Summary collapse
-
#add(feature) ⇒ Object
Public.
-
#clear(feature) ⇒ Object
Public.
-
#disable(feature, gate, thing) ⇒ Object
Public.
-
#enable(feature, gate, thing) ⇒ Object
Public.
- #export(format: :json, version: 1) ⇒ Object
-
#features ⇒ Object
Public.
-
#get(feature) ⇒ Object
Public.
- #get_all ⇒ Object
-
#get_multi(features) ⇒ Object
Public.
- #import(source) ⇒ Object
-
#initialize(adapter, cache = nil) ⇒ Memoizable
constructor
Public.
-
#memoize=(value) ⇒ Object
Internal: Turns local caching on/off.
-
#memoizing? ⇒ Boolean
Internal: Returns true for using local cache, false for not.
- #method_missing(name, *args, &block) ⇒ Object
-
#read_only? ⇒ Boolean
Public.
-
#remove(feature) ⇒ Object
Public.
Methods included from Flipper::Adapter
#default_config, included, #name
Constructor Details
#initialize(adapter, cache = nil) ⇒ Memoizable
Public
18 19 20 21 22 23 24 |
# File 'lib/flipper/adapters/memoizable.rb', line 18 def initialize(adapter, cache = nil) @adapter = adapter @cache = cache || {} @memoize = false @features_key = :flipper_features @get_all_key = :all_memoized end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
147 148 149 |
# File 'lib/flipper/adapters/memoizable.rb', line 147 def method_missing(name, *args, **kwargs, &block) @adapter.send name, *args, **kwargs, &block end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Internal: The adapter this adapter is wrapping.
15 16 17 |
# File 'lib/flipper/adapters/memoizable.rb', line 15 def adapter @adapter end |
#cache ⇒ Object (readonly)
Internal
12 13 14 |
# File 'lib/flipper/adapters/memoizable.rb', line 12 def cache @cache end |
Instance Method Details
#add(feature) ⇒ Object
Public
36 37 38 |
# File 'lib/flipper/adapters/memoizable.rb', line 36 def add(feature) @adapter.add(feature).tap { expire_features_set } end |
#clear(feature) ⇒ Object
Public
49 50 51 |
# File 'lib/flipper/adapters/memoizable.rb', line 49 def clear(feature) @adapter.clear(feature).tap { expire_feature(feature) } end |
#disable(feature, gate, thing) ⇒ Object
Public
116 117 118 |
# File 'lib/flipper/adapters/memoizable.rb', line 116 def disable(feature, gate, thing) @adapter.disable(feature, gate, thing).tap { expire_feature(feature) } end |
#enable(feature, gate, thing) ⇒ Object
Public
111 112 113 |
# File 'lib/flipper/adapters/memoizable.rb', line 111 def enable(feature, gate, thing) @adapter.enable(feature, gate, thing).tap { expire_feature(feature) } end |
#export(format: :json, version: 1) ⇒ Object
129 130 131 |
# File 'lib/flipper/adapters/memoizable.rb', line 129 def export(format: :json, version: 1) @adapter.export(format: format, version: version) end |
#features ⇒ Object
Public
27 28 29 30 31 32 33 |
# File 'lib/flipper/adapters/memoizable.rb', line 27 def features if memoizing? cache.fetch(@features_key) { cache[@features_key] = @adapter.features } else @adapter.features end end |
#get(feature) ⇒ Object
Public
54 55 56 57 58 59 60 |
# File 'lib/flipper/adapters/memoizable.rb', line 54 def get(feature) if memoizing? cache.fetch(key_for(feature.key)) { cache[key_for(feature.key)] = @adapter.get(feature) } else @adapter.get(feature) end end |
#get_all ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/flipper/adapters/memoizable.rb', line 84 def get_all if memoizing? response = nil if cache[@get_all_key] response = {} cache[@features_key].each do |key| response[key] = cache[key_for(key)] end else response = @adapter.get_all response.each do |key, value| cache[key_for(key)] = value end cache[@features_key] = response.keys.to_set cache[@get_all_key] = true end # Ensures that looking up other features that do not exist doesn't # result in N+1 adapter calls. response.default_proc = ->(memo, key) { memo[key] = default_config } response else @adapter.get_all end end |
#get_multi(features) ⇒ Object
Public
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/flipper/adapters/memoizable.rb', line 63 def get_multi(features) if memoizing? uncached_features = features.reject { |feature| cache[key_for(feature.key)] } if uncached_features.any? response = @adapter.get_multi(uncached_features) response.each do |key, hash| cache[key_for(key)] = hash end end result = {} features.each do |feature| result[feature.key] = cache[key_for(feature.key)] end result else @adapter.get_multi(features) end end |
#import(source) ⇒ Object
125 126 127 |
# File 'lib/flipper/adapters/memoizable.rb', line 125 def import(source) @adapter.import(source).tap { cache.clear if memoizing? } end |
#memoize=(value) ⇒ Object
Internal: Turns local caching on/off.
value - The Boolean that decides if local caching is on.
136 137 138 139 |
# File 'lib/flipper/adapters/memoizable.rb', line 136 def memoize=(value) cache.clear @memoize = value end |
#memoizing? ⇒ Boolean
Internal: Returns true for using local cache, false for not.
142 143 144 |
# File 'lib/flipper/adapters/memoizable.rb', line 142 def memoizing? !!@memoize end |
#read_only? ⇒ Boolean
Public
121 122 123 |
# File 'lib/flipper/adapters/memoizable.rb', line 121 def read_only? @adapter.read_only? end |
#remove(feature) ⇒ Object
Public
41 42 43 44 45 46 |
# File 'lib/flipper/adapters/memoizable.rb', line 41 def remove(feature) @adapter.remove(feature).tap do expire_features_set expire_feature(feature) end end |