Class: Trebuchet::Backend::RedisCached

Inherits:
Redis
  • Object
show all
Defined in:
lib/trebuchet/backend/redis_cached.rb

Instance Attribute Summary

Attributes inherited from Redis

#namespace

Instance Method Summary collapse

Methods inherited from Redis

#get_all_history, #get_archived_feature_names, #get_feature_names, #get_history, #get_sentinel, #initialize, #remove_feature, #remove_strategy, #set_strategy, #store_history, #unpack_strategy, #update_sentinel

Constructor Details

This class inherits a constructor from Trebuchet::Backend::Redis

Instance Method Details

#append_strategy(feature_name, strategy, options = nil) ⇒ Object



19
20
21
22
23
24
# File 'lib/trebuchet/backend/redis_cached.rb', line 19

def append_strategy(feature_name, strategy, options = nil)
  # though we can't clear the strategy for all active instances
  # this will clear the cache in the console environment to show current settings
  clear_cached_strategies
  super(feature_name, strategy, options)
end

#cache_cleared_atObject



34
35
36
# File 'lib/trebuchet/backend/redis_cached.rb', line 34

def cache_cleared_at
  @cache_cleared_at ||= Time.now
end

#cache_strategy(feature_name, strategy) ⇒ Object



26
27
28
# File 'lib/trebuchet/backend/redis_cached.rb', line 26

def cache_strategy(feature_name, strategy)
  cached_strategies[feature_name] = strategy
end

#cached_strategiesObject



30
31
32
# File 'lib/trebuchet/backend/redis_cached.rb', line 30

def cached_strategies
  @cached_strategies ||= Hash.new
end

#clear_cached_strategiesObject



38
39
40
41
# File 'lib/trebuchet/backend/redis_cached.rb', line 38

def clear_cached_strategies
  @cache_cleared_at = Time.now
  @cached_strategies = nil
end

#get_strategy(feature_name) ⇒ Object

cache strategies in memory until clear_cached_strategies is called



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/trebuchet/backend/redis_cached.rb', line 7

def get_strategy(feature_name)
  if cached_strategies.has_key?(feature_name)
    # use cached if available (even if value is nil)
    cached_strategies[feature_name]
  else
    # or call Trebuchet::Backend::Redis#get_strategy
    # which will fetch from Redis and unpack json
    # and then cache it for next time
    cache_strategy feature_name, super(feature_name)
  end
end

#refreshObject



43
44
45
# File 'lib/trebuchet/backend/redis_cached.rb', line 43

def refresh
  clear_cached_strategies if Time.now > cache_cleared_at + 60.seconds
end