Class: Flipper::Adapters::Memoizable

Inherits:
SimpleDelegator
  • Object
show all
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.

Constant Summary collapse

FeaturesKey =
:flipper_features

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter, cache = nil) ⇒ Memoizable

Public



23
24
25
26
27
28
29
# File 'lib/flipper/adapters/memoizable.rb', line 23

def initialize(adapter, cache = nil)
  super(adapter)
  @adapter = adapter
  @name = :memoizable
  @cache = cache || {}
  @memoize = false
end

Instance Attribute Details

#adapterObject (readonly)

Internal: The adapter this adapter is wrapping.



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

def adapter
  @adapter
end

#cacheObject (readonly)

Internal



14
15
16
# File 'lib/flipper/adapters/memoizable.rb', line 14

def cache
  @cache
end

#nameObject (readonly)

Public: The name of the adapter.



17
18
19
# File 'lib/flipper/adapters/memoizable.rb', line 17

def name
  @name
end

Instance Method Details

#add(feature) ⇒ Object

Public



41
42
43
44
45
# File 'lib/flipper/adapters/memoizable.rb', line 41

def add(feature)
  result = @adapter.add(feature)
  expire_features_set
  result
end

#clear(feature) ⇒ Object

Public



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

def clear(feature)
  result = @adapter.clear(feature)
  expire_feature(feature)
  result
end

#disable(feature, gate, thing) ⇒ Object

Public



101
102
103
104
105
# File 'lib/flipper/adapters/memoizable.rb', line 101

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

#enable(feature, gate, thing) ⇒ Object

Public



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

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

#featuresObject

Public



32
33
34
35
36
37
38
# File 'lib/flipper/adapters/memoizable.rb', line 32

def features
  if memoizing?
    cache.fetch(FeaturesKey) { cache[FeaturesKey] = @adapter.features }
  else
    @adapter.features
  end
end

#get(feature) ⇒ Object

Public



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

def get(feature)
  if memoizing?
    cache.fetch(feature.key) { cache[feature.key] = @adapter.get(feature) }
  else
    @adapter.get(feature)
  end
end

#get_multi(features) ⇒ Object

Public



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/flipper/adapters/memoizable.rb', line 72

def get_multi(features)
  if memoizing?
    uncached_features = features.reject { |feature| cache[feature.key] }

    if uncached_features.any?
      response = @adapter.get_multi(uncached_features)
      response.each do |key, hash|
        cache[key] = hash
      end
    end

    result = {}
    features.each do |feature|
      result[feature.key] = cache[feature.key]
    end
    result
  else
    @adapter.get_multi(features)
  end
end

#memoize=(value) ⇒ Object

Internal: Turns local caching on/off.

value - The Boolean that decides if local caching is on.



110
111
112
113
# File 'lib/flipper/adapters/memoizable.rb', line 110

def memoize=(value)
  cache.clear
  @memoize = value
end

#memoizing?Boolean

Internal: Returns true for using local cache, false for not.

Returns:

  • (Boolean)


116
117
118
# File 'lib/flipper/adapters/memoizable.rb', line 116

def memoizing?
  !!@memoize
end

#remove(feature) ⇒ Object

Public



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

def remove(feature)
  result = @adapter.remove(feature)
  expire_features_set
  expire_feature(feature)
  result
end