class Tr8n::CacheAdapters::Redis

Public Class Methods

new() click to toggle source
# File lib/tr8n/cache_adapters/redis.rb, line 38
def initialize
  cache_host, cache_port = Tr8n.config.cache[:host].split(':') if Tr8n.config.cache[:host]
  cache_host ||= 'localhost'
  cache_port ||= 6379

  @cache = ::Redis.new(host: cache_host, port: cache_port)
end

Public Instance Methods

cache_name() click to toggle source
# File lib/tr8n/cache_adapters/redis.rb, line 46
def cache_name
  "redis"
end
clear(opts = {}) click to toggle source
# File lib/tr8n/cache_adapters/redis.rb, line 104
def clear(opts = {})
  info("Cache clear has no effect")
end
delete(key, opts = {}) click to toggle source
# File lib/tr8n/cache_adapters/redis.rb, line 88
def delete(key, opts = {})
  info("Cache delete: #{key}")
  @cache.del(versioned_key(key, opts))
rescue Exception => ex
  warn("Failed to delete data: #{ex.message}")
  key
end
exist?(key, opts = {}) click to toggle source
# File lib/tr8n/cache_adapters/redis.rb, line 96
def exist?(key, opts = {})
  data = @cache.exist(versioned_key(key, opts))
  not data.nil?
rescue Exception => ex
  warn("Failed to check if key exists: #{ex.message}")
  false
end
fetch(key, opts = {}) { || ... } click to toggle source
# File lib/tr8n/cache_adapters/redis.rb, line 54
def fetch(key, opts = {})
  data = @cache.get(versioned_key(key, opts))
  if data
    info("Cache hit: #{key}")
    return data
  end

  info("Cache miss: #{key}")

  return nil unless block_given?

  data = yield

  store(key, data)

  data
rescue Exception => ex
  warn("Failed to retrieve data: #{ex.message}")
  return nil unless block_given?
  yield
end
read_only?() click to toggle source
# File lib/tr8n/cache_adapters/redis.rb, line 50
def read_only?
  false
end
store(key, data, opts = {}) click to toggle source
# File lib/tr8n/cache_adapters/redis.rb, line 76
def store(key, data, opts = {})
  info("Cache store: #{key}")
  ttl = opts[:ttl] || Tr8n.config.cache[:timeout]
  versioned_key = versioned_key(key, opts)

  @cache.set(versioned_key, data)
  @cache.expire(versioned_key, ttl) if ttl and ttl > 0
rescue Exception => ex
  warn("Failed to store data: #{ex.message}")
  data
end