module Redcache

Constants

VERSION

Attributes

configuration[W]

Public Class Methods

cache(redis_key, *args, &block) click to toggle source
# File lib/redcache.rb, line 17
def cache(redis_key, *args, &block)
  # return immediately if we shouldn't or can't cache
  return block.call(*args) if skip_cache? || !redis_up?
  with_redis do
    # attempt to read from cache, running and caching the block if cold
    value = read_from_cache(redis_key, *args, &block)
    if value.nil?
      value = block.call(*args) if value.nil?
      write_into_cache(redis_key, value)
    end
    return value
  end
end
cache_time() click to toggle source
# File lib/redcache.rb, line 101
def cache_time
  configuration.cache_time
end
configuration() click to toggle source
# File lib/redcache.rb, line 79
def configuration
  @configuration ||= Configuration.new
end
configure() { |configuration| ... } click to toggle source
# File lib/redcache.rb, line 13
def configure
  yield(configuration)
end
decrypt(value) click to toggle source
# File lib/redcache.rb, line 71
def decrypt(value)
  return nil if value.nil?
  return value unless encrypt?
  verifier = fernet.verifier(secret, value)
  return MultiJson.load(verifier.message) if verifier.valid?
  return nil
end
encrypt(value) click to toggle source
# File lib/redcache.rb, line 66
def encrypt(value)
  return prep_value(value) unless encrypt?
  fernet.generate(secret, prep_value(value))
end
encrypt?() click to toggle source
# File lib/redcache.rb, line 121
def encrypt?
  configuration.encrypt
end
fernet() click to toggle source
# File lib/redcache.rb, line 137
def fernet
  ::Fernet
end
get_value(key) click to toggle source
# File lib/redcache.rb, line 53
def get_value(key)
  decrypt redis.get(key)
end
key_stale?(redis_key) click to toggle source
# File lib/redcache.rb, line 61
def key_stale?(redis_key)
  ttl = redis.ttl(redis_key)
  return  ttl < (configuration.cache_time - configuration.stale_time)
end
log(str, key) click to toggle source
# File lib/redcache.rb, line 129
def log(str, key)
  configuration.logger.log(log_prefix(str) => 1, :key => key) unless configuration.silent
end
log_prefix(str) click to toggle source
# File lib/redcache.rb, line 133
def log_prefix(str)
  [configuration.log_prefix, str].join(".")
end
prep_value(value) click to toggle source
# File lib/redcache.rb, line 117
def prep_value(value)
  MultiJson.encode(value)
end
read_from_cache(redis_key, *args, &block) click to toggle source
# File lib/redcache.rb, line 31
def read_from_cache(redis_key, *args, &block)
  value = get_value(redis_key)
  value.nil? ? log("cache.miss", redis_key) : log("cache.hit", redis_key)
  refresh_cache(redis_key, *args, &block) if key_stale?(redis_key) && !value.nil?
  return value
end
redis() click to toggle source
# File lib/redcache.rb, line 83
def redis
  configuration.redis
end
redis_up?() click to toggle source
# File lib/redcache.rb, line 87
def redis_up?
  begin
    redis.ping
  rescue Redis::CannotConnectError, Timeout::Error
    puts "Redis is DOWN! :shitsonfire:"
    return false
  end
  return true
end
refresh_cache(redis_key, *args, &block) click to toggle source
# File lib/redcache.rb, line 38
def refresh_cache(redis_key, *args, &block)
  log("cache.stale_refresh", redis_key)
  Thread.new(*args) do |*thread_args|
    write_into_cache(redis_key, block.call(*thread_args))
  end
end
secret() click to toggle source
# File lib/redcache.rb, line 125
def secret
  configuration.secret
end
set_value(key, value) click to toggle source
# File lib/redcache.rb, line 57
def set_value(key, value)
  redis.setex key, configuration.cache_time, encrypt(value)
end
skip_cache?() click to toggle source
# File lib/redcache.rb, line 109
def skip_cache?
  configuration.skip_cache
end
stale_time() click to toggle source
# File lib/redcache.rb, line 105
def stale_time
  configuration.stale_time
end
test?() click to toggle source
# File lib/redcache.rb, line 113
def test?
  ENV["RACK_ENV"] == 'test'
end
with_redis(&block) click to toggle source
# File lib/redcache.rb, line 97
def with_redis(&block)
  block.call if redis_up?
end
write_into_cache(redis_key, value) click to toggle source
# File lib/redcache.rb, line 45
def write_into_cache(redis_key, value)
  with_redis do
    log("cache.write", redis_key)
    set_value(redis_key, value)
  end
  value
end