Class: CurlyMustache::Adapters::Memcached

Inherits:
Abstract
  • Object
show all
Defined in:
lib/curly_mustache/adapters/memcached.rb

Overview

You can use this adapter with any data store that speaks Memcached. The adapter uses memcache-client. The :servers key in the hash passed to CurlyMustache::Base#establish_connection will be the first argument to MemCache.new and entire hash will be passed as the second argument.

Instance Attribute Summary

Attributes inherited from Abstract

#model_class

Instance Method Summary collapse

Methods inherited from Abstract

#adapter_name

Constructor Details

#initialize(config) ⇒ Memcached

config[:servers] will be passed as the first argument to MemCache.new and config itself will be passed as the second argument.



13
14
15
16
# File 'lib/curly_mustache/adapters/memcached.rb', line 13

def initialize(config)
  config = config.reverse_merge :servers => "localhost:11211"
  @cache = MemCache.new(config[:servers], config)
end

Instance Method Details

#delete(key) ⇒ Object



33
34
35
# File 'lib/curly_mustache/adapters/memcached.rb', line 33

def delete(key)
  @cache.delete(key)
end

#flush_dbObject



37
38
39
# File 'lib/curly_mustache/adapters/memcached.rb', line 37

def flush_db
  @cache.flush_all
end

#get(key) ⇒ Object



18
19
20
# File 'lib/curly_mustache/adapters/memcached.rb', line 18

def get(key)
  @cache.get(key)
end

#lock(key, options = {}) ⇒ Object



41
42
43
44
# File 'lib/curly_mustache/adapters/memcached.rb', line 41

def lock(key, options = {})
  expires_in = options[:expires_in] || 0
  @cache.add(key, Time.now.to_s(:number), expires_in) == "STORED\r\n"
end

#locked?(key) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/curly_mustache/adapters/memcached.rb', line 50

def locked?(key)
  !!@cache.get(key)
end

#mget(keys) ⇒ Object



22
23
24
25
26
27
# File 'lib/curly_mustache/adapters/memcached.rb', line 22

def mget(keys)
  keys = keys.collect(&:to_s)
  results = @cache.get_multi(*keys)
  results = results.collect{ |k, v| [k, v] }
  results.sort.collect{ |result| result[1] }
end

#put(key, value) ⇒ Object



29
30
31
# File 'lib/curly_mustache/adapters/memcached.rb', line 29

def put(key, value)
  @cache.set(key, value)
end

#unlock(key) ⇒ Object



46
47
48
# File 'lib/curly_mustache/adapters/memcached.rb', line 46

def unlock(key)
  delete(key) == "DELETED\r\n"
end