class Tr8n::CacheAdapters::Memcache

Public Class Methods

new() click to toggle source
# File lib/tr8n/cache_adapters/memcache.rb, line 37
def initialize
  options = { :namespace => Tr8n.config.cache[:namespace] || "tr8n", :compress => Tr8n.config.cache[:compress].nil? ? true : Tr8n.config.cache[:compress]}
  @cache = Dalli::Client.new(Tr8n.config.cache[:host], options)
end

Public Instance Methods

cache_name() click to toggle source
# File lib/tr8n/cache_adapters/memcache.rb, line 42
def cache_name
  "memcache"
end
clear(opts = {}) click to toggle source
# File lib/tr8n/cache_adapters/memcache.rb, line 99
def clear(opts = {})
  info("Cache clear")
rescue Exception => ex
  warn("Failed to clear cache: #{key}")
end
delete(key, opts = {}) click to toggle source
# File lib/tr8n/cache_adapters/memcache.rb, line 82
def delete(key, opts = {})
  info("Cache delete: #{key}")
  @cache.delete(versioned_key(key, opts))
  key
rescue Exception => ex
  warn("Failed to delete data: #{key}")
  key
end
exist?(key, opts = {}) click to toggle source
# File lib/tr8n/cache_adapters/memcache.rb, line 91
def exist?(key, opts = {})
  data = @cache.get(versioned_key(key, opts))
  not data.nil?
rescue Exception => ex
  warn("Failed to check if key exists: #{key}")
  false
end
fetch(key, opts = {}) { || ... } click to toggle source
# File lib/tr8n/cache_adapters/memcache.rb, line 50
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: #{key}")
  return nil unless block_given?
  yield
end
read_only?() click to toggle source
# File lib/tr8n/cache_adapters/memcache.rb, line 46
def read_only?
  false
end
store(key, data, opts = {}) click to toggle source
# File lib/tr8n/cache_adapters/memcache.rb, line 72
def store(key, data, opts = {})
  info("Cache store: #{key}")
  ttl = opts[:ttl] || Tr8n.config.cache[:timeout]
  @cache.set(versioned_key(key, opts), data, ttl)
  data
rescue Exception => ex
  warn("Failed to store data: #{key}")
  data
end