Class: Flipper::Adapters::CacheBase

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

Overview

Base class for caching adapters. Inherit from this and then override cache_fetch, cache_read_multi, cache_write, and cache_delete.

Direct Known Subclasses

ActiveSupportCacheStore, Dalli, RedisCache

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Flipper::Adapter

#default_config, #export, #import, included, #name, #read_only?

Constructor Details

#initialize(adapter, cache, ttl = 300, prefix: nil) ⇒ CacheBase

Returns a new instance of CacheBase.



23
24
25
26
27
28
29
30
31
32
# File 'lib/flipper/adapters/cache_base.rb', line 23

def initialize(adapter, cache, ttl = 300, prefix: nil)
  @adapter = adapter
  @cache = cache
  @ttl = ttl

  @cache_version = 'v1'.freeze
  @namespace = "flipper/#{@cache_version}"
  @namespace = @namespace.prepend(prefix) if prefix
  @features_cache_key = "#{@namespace}/features"
end

Instance Attribute Details

#adapterObject (readonly)

Public: The adapter being cached.



9
10
11
# File 'lib/flipper/adapters/cache_base.rb', line 9

def adapter
  @adapter
end

#cacheObject (readonly)

Public: The ActiveSupport::Cache::Store to cache with.



12
13
14
# File 'lib/flipper/adapters/cache_base.rb', line 12

def cache
  @cache
end

#features_cache_keyObject (readonly)

Public: The cache key where the set of known features is cached.



18
19
20
# File 'lib/flipper/adapters/cache_base.rb', line 18

def features_cache_key
  @features_cache_key
end

#ttlObject (readonly) Also known as: expires_in

Public: The ttl for all cached data.



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

def ttl
  @ttl
end

Instance Method Details

#add(feature) ⇒ Object

Public



50
51
52
53
54
# File 'lib/flipper/adapters/cache_base.rb', line 50

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

#clear(feature) ⇒ Object

Public



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

def clear(feature)
  result = @adapter.clear(feature)
  expire_feature_cache(feature.key)
  result
end

#disable(feature, gate, thing) ⇒ Object

Public



95
96
97
98
99
# File 'lib/flipper/adapters/cache_base.rb', line 95

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

#enable(feature, gate, thing) ⇒ Object

Public



88
89
90
91
92
# File 'lib/flipper/adapters/cache_base.rb', line 88

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

#expire_feature_cache(key) ⇒ Object

Public: Expire the cache for a given feature.



40
41
42
# File 'lib/flipper/adapters/cache_base.rb', line 40

def expire_feature_cache(key)
  cache_delete feature_cache_key(key)
end

#expire_features_cacheObject

Public: Expire the cache for the set of known feature names.



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

def expire_features_cache
  cache_delete @features_cache_key
end

#feature_cache_key(key) ⇒ Object

Public: Generate the cache key for a given feature.

key - The String or Symbol feature key.



104
105
106
# File 'lib/flipper/adapters/cache_base.rb', line 104

def feature_cache_key(key)
  "#{@namespace}/feature/#{key}"
end

#featuresObject

Public



45
46
47
# File 'lib/flipper/adapters/cache_base.rb', line 45

def features
  read_feature_keys
end

#get(feature) ⇒ Object

Public



72
73
74
# File 'lib/flipper/adapters/cache_base.rb', line 72

def get(feature)
  read_feature(feature)
end

#get_allObject

Public



82
83
84
85
# File 'lib/flipper/adapters/cache_base.rb', line 82

def get_all
  features = read_feature_keys.map { |key| Flipper::Feature.new(key, self) }
  read_many_features(features)
end

#get_multi(features) ⇒ Object

Public



77
78
79
# File 'lib/flipper/adapters/cache_base.rb', line 77

def get_multi(features)
  read_many_features(features)
end

#remove(feature) ⇒ Object

Public



57
58
59
60
61
62
# File 'lib/flipper/adapters/cache_base.rb', line 57

def remove(feature)
  result = @adapter.remove(feature)
  expire_features_cache
  expire_feature_cache(feature.key)
  result
end