module Greencache
Constants
- VERSION
Attributes
configuration[W]
Public Class Methods
cache(redis_key, config = {}, &block)
click to toggle source
# File lib/greencache.rb, line 21 def cache(redis_key, config = {}, &block) config = merge_config(config) return block.call if config[:skip_cache] || !redis_up? read_from_cache!(redis_key, config) rescue CacheMiss value = block.call write_into_cache(redis_key, value, config) value end
configuration()
click to toggle source
# File lib/greencache.rb, line 91 def configuration @configuration ||= Configuration.new end
configure() { |configuration| ... }
click to toggle source
# File lib/greencache.rb, line 17 def configure yield(configuration) end
decrypt(value, config)
click to toggle source
# File lib/greencache.rb, line 83 def decrypt(value, config) return nil if value.nil? return MultiJson.load(value) unless config[:encrypt] verifier = fernet.verifier(config[:secret], value) return MultiJson.load(verifier.message) if verifier.valid? return nil end
encrypt(value, config)
click to toggle source
# File lib/greencache.rb, line 78 def encrypt(value, config) return prep_value(value) unless config[:encrypt] fernet.generate(config[:secret], prep_value(value)) end
expire(redis_key)
click to toggle source
# File lib/greencache.rb, line 31 def expire(redis_key) redis.del(redis_key) if redis_up? end
fernet()
click to toggle source
# File lib/greencache.rb, line 129 def fernet ::Fernet end
get_value(key, config)
click to toggle source
# File lib/greencache.rb, line 64 def get_value(key, config) get_value!(key, config) rescue CacheMiss end
log(str, key, config)
click to toggle source
# File lib/greencache.rb, line 121 def log(str, key, config) config[:logger].log(log_prefix(str, config) => 1, :key => key) unless config[:silent] end
log_prefix(str, config)
click to toggle source
# File lib/greencache.rb, line 125 def log_prefix(str, config) [config[:log_prefix], str].join(".") end
memory_cache()
click to toggle source
# File lib/greencache.rb, line 133 def memory_cache @@memory_cache ||= MiniCache::Store.new end
merge_config(config)
click to toggle source
# File lib/greencache.rb, line 74 def merge_config(config) configuration.to_hash.merge(config) end
prep_value(value)
click to toggle source
# File lib/greencache.rb, line 117 def prep_value(value) MultiJson.encode(value) end
read_from_cache()
click to toggle source
# File lib/greencache.rb, line 44 def read_from_cache read_from_cache!(redis_key) rescue CacheMiss end
redis()
click to toggle source
# File lib/greencache.rb, line 95 def redis configuration.redis end
redis_up?()
click to toggle source
# File lib/greencache.rb, line 99 def redis_up? begin redis.ping rescue Redis::CannotConnectError, Timeout::Error puts "Redis is DOWN! :shitsonfire:" return false end return true end
set_value(key, value, config)
click to toggle source
# File lib/greencache.rb, line 69 def set_value(key, value, config) redis.setex key, config[:cache_time], encrypt(value, config) memory_cache.set(key, value, expires_in: 1) end
test?()
click to toggle source
# File lib/greencache.rb, line 113 def test? ENV["RACK_ENV"] == 'test' end
with_redis(&block)
click to toggle source
# File lib/greencache.rb, line 109 def with_redis(&block) block.call if redis_up? end
write_into_cache(redis_key, value, config)
click to toggle source
# File lib/greencache.rb, line 49 def write_into_cache(redis_key, value, config) with_redis do log("cache.write", redis_key, config) set_value(redis_key, value, config) end value end
Private Class Methods
get_value!(key, config)
click to toggle source
# File lib/greencache.rb, line 57 def get_value!(key, config) raise CacheMiss unless redis.exists(key) result = memory_cache.get(key) result = decrypt(redis.get(key), config) if result.nil? result end
read_from_cache!(redis_key, config)
click to toggle source
# File lib/greencache.rb, line 35 def read_from_cache!(redis_key, config) value = get_value!(redis_key, config) log("cache.hit", redis_key, config) value rescue CacheMiss log("cache.miss", redis_key, config) raise end