Class: Hiera::Backend::Cache
- Inherits:
-
Object
- Object
- Hiera::Backend::Cache
- Defined in:
- lib/hiera/backend/cloudformation_backend.rb
Overview
Cache class that hides Redis vs. TimedCache implementation
Instance Method Summary collapse
-
#format_key(key) ⇒ Object
If key is Enumerable convert to a json string.
-
#format_value(value) ⇒ Object
Marshal values into sensible JSON form, assumes all arrays contain values of same type.
- #get(key) ⇒ Object
-
#initialize(cache_ttl = 60) ⇒ Cache
constructor
A new instance of Cache.
- #put(key, value) ⇒ Object
Constructor Details
#initialize(cache_ttl = 60) ⇒ Cache
Returns a new instance of Cache.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/hiera/backend/cloudformation_backend.rb', line 25 def initialize(cache_ttl = 60) @cache_ttl = cache_ttl if Config.include?(:cloudformation) && !Config[:cloudformation].nil? if Config[:cloudformation].include?(:redis_host) @redis_host = Config[:cloudformation][:redis_host] end if Config[:cloudformation].include?(:redis_port) @redis_port = Config[:cloudformation][:redis_port] else @redis_port = 6379 end if Config[:cloudformation].include?(:redis_db) @redis_db = Config[:cloudformation][:redis_db] else @redis_db = 0 end end if @redis_host @redis = Redis.new(:host => @redis_host, :port => @redis_port, :db => @redis_db) else @timedcache = TimedCache.new end end |
Instance Method Details
#format_key(key) ⇒ Object
If key is Enumerable convert to a json string
88 89 90 91 92 93 94 |
# File 'lib/hiera/backend/cloudformation_backend.rb', line 88 def format_key(key) if key.is_a? Enumerable key.to_json else key end end |
#format_value(value) ⇒ Object
Marshal values into sensible JSON form, assumes all arrays contain values of same type
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/hiera/backend/cloudformation_backend.rb', line 97 def format_value(value) if value.is_a? Array if value.first.is_a? AWS::CloudFormation::StackOutput stack_outputs = value.collect do |stack_output| { :description => stack_output.description, :key => stack_output.key, :value => stack_output.value } end JSON.generate(stack_outputs) else JSON.generate(value) end elsif value.is_a? Hash JSON.generate(value) else value end end |
#get(key) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/hiera/backend/cloudformation_backend.rb', line 53 def get(key) formatted_key = format_key(key) if @redis Hiera.debug("Attempting to fetch #{formatted_key} from Redis") result = @redis.get(formatted_key) else Hiera.debug("Attempting to fetch #{formatted_key} from TimedCache") result = @timedcache.get formatted_key end JSON.parse(result) unless result.nil? end |
#put(key, value) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/hiera/backend/cloudformation_backend.rb', line 67 def put(key, value) formatted_key = format_key(key) formatted_value = format_value(value) if @redis if @cache_ttl < 1 Hiera.debug("Attempting to set #{formatted_key} in Redis") @redis.set(formatted_key, formatted_value) else Hiera.debug("Attempting to setex #{formatted_key} in Redis with TTL of #{@cache_ttl}") @redis.setex(formatted_key, @cache_ttl, formatted_value) end else Hiera.debug("Attempting to set #{formatted_key} in TimedCache with TTL of #{@cache_ttl}") @timedcache.put(formatted_key, formatted_value, @cache_ttl) end formatted_value end |