class Flipper::Adapters::ActiveSupportCacheStore

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

Constants

FeaturesKey
GetAllKey
Namespace
Version

Attributes

cache[R]

Internal

name[R]

Public: The name of the adapter.

Public Class Methods

key_for(key) click to toggle source

Private

# File lib/flipper/adapters/active_support_cache_store.rb, line 15
def self.key_for(key)
  "#{Namespace}/feature/#{key}"
end
new(adapter, cache, expires_in: nil) click to toggle source

Public

# 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

Public Instance Methods

add(feature) click to toggle source

Public

# 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) click to toggle source

Public

# 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) click to toggle source

Public

# 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) click to toggle source

Public

# 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() click to toggle source

Public

# File lib/flipper/adapters/active_support_cache_store.rb, line 35
def features
  read_feature_keys
end
get(feature) click to toggle source

Public

# 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() click to toggle source
# 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) click to toggle source
# File lib/flipper/adapters/active_support_cache_store.rb, line 68
def get_multi(features)
  read_many_features(features)
end
remove(feature) click to toggle source

Public

# 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

Private Instance Methods

key_for(key) click to toggle source
# File lib/flipper/adapters/active_support_cache_store.rb, line 102
def key_for(key)
  self.class.key_for(key)
end
read_feature_keys() click to toggle source

Internal: Returns an array of the known feature keys.

# File lib/flipper/adapters/active_support_cache_store.rb, line 107
def read_feature_keys
  @cache.fetch(FeaturesKey, @write_options) { @adapter.features }
end
read_many_features(features) click to toggle source

Internal: Given an array of features, attempts to read through cache in as few network calls as possible.

# File lib/flipper/adapters/active_support_cache_store.rb, line 113
def read_many_features(features)
  keys = features.map { |feature| key_for(feature.key) }
  cache_result = @cache.read_multi(*keys)
  uncached_features = features.reject { |feature| cache_result[key_for(feature)] }

  if uncached_features.any?
    response = @adapter.get_multi(uncached_features)
    response.each do |key, value|
      @cache.write(key_for(key), value, @write_options)
      cache_result[key_for(key)] = value
    end
  end

  result = {}
  features.each do |feature|
    result[feature.key] = cache_result[key_for(feature.key)]
  end
  result
end