Class: Flipper::Adapters::RedisCache

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

Overview

Public: Adapter that wraps another adapter with the ability to cache adapter calls in Redis

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Flipper::Adapter

#default_config, #export, #import, included, #name

Constructor Details

#initialize(adapter, cache, ttl = 3600) ⇒ RedisCache

Public



15
16
17
18
19
20
21
22
23
24
# File 'lib/flipper/adapters/redis_cache.rb', line 15

def initialize(adapter, cache, ttl = 3600)
  @adapter = adapter
  @cache = cache
  @ttl = ttl

  @version = 'v1'.freeze
  @namespace = "flipper/#{@version}".freeze
  @features_key = "#{@namespace}/features".freeze
  @get_all_key = "#{@namespace}/get_all".freeze
end

Instance Attribute Details

#cacheObject (readonly)

Internal



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

def cache
  @cache
end

Instance Method Details

#add(feature) ⇒ Object

Public



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

def add(feature)
  result = @adapter.add(feature)
  @cache.del(@features_key)
  result
end

#clear(feature) ⇒ Object

Public



47
48
49
50
51
# File 'lib/flipper/adapters/redis_cache.rb', line 47

def clear(feature)
  result = @adapter.clear(feature)
  @cache.del(key_for(feature.key))
  result
end

#disable(feature, gate, thing) ⇒ Object

Public



87
88
89
90
91
# File 'lib/flipper/adapters/redis_cache.rb', line 87

def disable(feature, gate, thing)
  result = @adapter.disable(feature, gate, thing)
  @cache.del(key_for(feature.key))
  result
end

#enable(feature, gate, thing) ⇒ Object

Public



80
81
82
83
84
# File 'lib/flipper/adapters/redis_cache.rb', line 80

def enable(feature, gate, thing)
  result = @adapter.enable(feature, gate, thing)
  @cache.del(key_for(feature.key))
  result
end

#featuresObject

Public



27
28
29
# File 'lib/flipper/adapters/redis_cache.rb', line 27

def features
  read_feature_keys
end

#get(feature) ⇒ Object

Public



54
55
56
57
58
# File 'lib/flipper/adapters/redis_cache.rb', line 54

def get(feature)
  fetch(key_for(feature.key)) do
    @adapter.get(feature)
  end
end

#get_allObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/flipper/adapters/redis_cache.rb', line 64

def get_all
  if @cache.setnx(@get_all_key, Time.now.to_i)
    @cache.expire(@get_all_key, @ttl)
    response = @adapter.get_all
    response.each do |key, value|
      set_with_ttl key_for(key), value
    end
    set_with_ttl @features_key, response.keys.to_set
    response
  else
    features = read_feature_keys.map { |key| Flipper::Feature.new(key, self) }
    read_many_features(features)
  end
end

#get_multi(features) ⇒ Object



60
61
62
# File 'lib/flipper/adapters/redis_cache.rb', line 60

def get_multi(features)
  read_many_features(features)
end

#remove(feature) ⇒ Object

Public



39
40
41
42
43
44
# File 'lib/flipper/adapters/redis_cache.rb', line 39

def remove(feature)
  result = @adapter.remove(feature)
  @cache.del(@features_key)
  @cache.del(key_for(feature.key))
  result
end