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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Flipper::Adapter

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

Constructor Details

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

Returns a new instance of CacheBase.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/flipper/adapters/cache_base.rb', line 26

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"
  @get_all_cache_key = "#{@namespace}/get_all"
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

#get_all_cache_keyObject (readonly)

Public: The cache key where the set of all features with gates is cached.



21
22
23
# File 'lib/flipper/adapters/cache_base.rb', line 21

def get_all_cache_key
  @get_all_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



61
62
63
64
65
# File 'lib/flipper/adapters/cache_base.rb', line 61

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

#clear(feature) ⇒ Object

Public



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

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

#disable(feature, gate, thing) ⇒ Object

Public



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

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

#enable(feature, gate, thing) ⇒ Object

Public



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

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.



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

def expire_feature_cache(key)
  cache_delete feature_cache_key(key)
  expire_get_all_cache
end

#expire_features_cacheObject

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



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

def expire_features_cache
  cache_delete @features_cache_key
  expire_get_all_cache
end

#expire_get_all_cacheObject

Public: Expire the cache for the set of all features with gates.



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

def expire_get_all_cache
  cache_delete @get_all_cache_key
end

#feature_cache_key(key) ⇒ Object

Public: Generate the cache key for a given feature.

key - The String or Symbol feature key.



118
119
120
# File 'lib/flipper/adapters/cache_base.rb', line 118

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

#featuresObject

Public



56
57
58
# File 'lib/flipper/adapters/cache_base.rb', line 56

def features
  read_feature_keys
end

#get(feature) ⇒ Object

Public



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

def get(feature)
  read_feature(feature)
end

#get_all(**kwargs) ⇒ Object

Public



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

def get_all(**kwargs)
  cache_fetch(@get_all_cache_key) {
    result = read_all_features(**kwargs)
    cache_write @features_cache_key, result.keys.to_set
    result
  }
end

#get_multi(features) ⇒ Object

Public



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

def get_multi(features)
  read_many_features(features)
end

#remove(feature) ⇒ Object

Public



68
69
70
71
72
73
# File 'lib/flipper/adapters/cache_base.rb', line 68

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