Class: Tml::CacheAdapters::Memcache

Inherits:
Tml::Cache show all
Defined in:
lib/tml/cache_adapters/memcache.rb

Instance Method Summary collapse

Methods inherited from Tml::Cache

#default_cache_path, #download, #enabled?, #extract_version, #info, #namespace, #namespace=, #reset_version, #strip_extensions, #upgrade_version, #version, #versioned_key, #warmup, #warmup_from_cdn, #warmup_from_files, #warn

Constructor Details

#initializeMemcache

Returns a new instance of Memcache.



37
38
39
40
41
# File 'lib/tml/cache_adapters/memcache.rb', line 37

def initialize
  config = Tml.config.cache
  options = { :namespace => config[:namespace] || 'tml', :compress => config[:compress].nil? ? true : config[:compress] }
  @cache = Dalli::Client.new(config[:host], options)
end

Instance Method Details

#cache_nameObject



43
44
45
# File 'lib/tml/cache_adapters/memcache.rb', line 43

def cache_name
  'memcache'
end

#clear(opts = {}) ⇒ Object



100
101
102
103
104
# File 'lib/tml/cache_adapters/memcache.rb', line 100

def clear(opts = {})
  info("Cache clear")
rescue Exception => ex
  warn("Failed to clear cache: #{key}")
end

#delete(key, opts = {}) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/tml/cache_adapters/memcache.rb', line 83

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 = {}) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
95
96
97
98
# File 'lib/tml/cache_adapters/memcache.rb', line 92

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 = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/tml/cache_adapters/memcache.rb', line 51

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("#{ex.message}: #{key}")
  return nil unless block_given?
  yield
end

#read_only?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/tml/cache_adapters/memcache.rb', line 47

def read_only?
  false
end

#store(key, data, opts = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/tml/cache_adapters/memcache.rb', line 73

def store(key, data, opts = {})
  info("Cache store: #{key}")
  ttl = opts[:ttl] || Tml.config.cache[:timeout] || 0
  @cache.set(versioned_key(key, opts), strip_extensions(data), ttl)
  data
rescue Exception => ex
  warn("Failed to store data: #{key}")
  data
end