Class: Merb::Cache::MemcachedStore

Inherits:
AbstractStore show all
Defined in:
merb-cache/lib/merb-cache/stores/fundamental/memcached_store.rb

Overview

Memcached store uses one or several Memcached servers for caching. It's flexible and can be used for fragment caching, action caching, page caching or object caching.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractStore

#delete_all!, #write_all

Constructor Details

#initialize(config = {}) ⇒ MemcachedStore

Returns a new instance of MemcachedStore

Options Hash (config):

  • :namespace (String)
  • :servers (Array<String>) — default: ["127.0.0.1:11211"]

    Memcache servers.



15
16
17
18
19
20
# File 'merb-cache/lib/merb-cache/stores/fundamental/memcached_store.rb', line 15

def initialize(config = {})
  @namespace = config[:namespace]
  @servers = config[:servers] || ["127.0.0.1:11211"]

  connect(config)
end

Instance Attribute Details

#memcachedObject

Returns the value of attribute memcached



9
10
11
# File 'merb-cache/lib/merb-cache/stores/fundamental/memcached_store.rb', line 9

def memcached
  @memcached
end

#namespaceObject

Returns the value of attribute namespace



9
10
11
# File 'merb-cache/lib/merb-cache/stores/fundamental/memcached_store.rb', line 9

def namespace
  @namespace
end

#serversObject

Returns the value of attribute servers



9
10
11
# File 'merb-cache/lib/merb-cache/stores/fundamental/memcached_store.rb', line 9

def servers
  @servers
end

Instance Method Details

#cloneObject



85
86
87
88
89
# File 'merb-cache/lib/merb-cache/stores/fundamental/memcached_store.rb', line 85

def clone
  twin = super
  twin.memcached = @memcached.clone
  twin
end

#connect(config = {}) ⇒ Object

Establishes connection to Memcached.

Options Hash (config):

  • :buffer_requests (Boolean) — default: false

    Use bufferring

  • :no_block (Boolean) — default: false

    Use non-blocking async I/O.

  • :support_cas (Boolean) — default: false

    Support CAS.

See Also:



103
104
105
# File 'merb-cache/lib/merb-cache/stores/fundamental/memcached_store.rb', line 103

def connect(config = {})
  @memcached = ::Memcached.new(@servers, config.only(:buffer_requests, :no_block, :support_cas).merge(:namespace => @namespace))
end

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

Deletes entry from cached by key.



72
73
74
75
76
77
78
# File 'merb-cache/lib/merb-cache/stores/fundamental/memcached_store.rb', line 72

def delete(key, parameters = {})
  begin
    @memcached.delete(normalize(key, parameters))
  rescue Memcached::NotFound
    nil
  end
end

#delete_allObject

Flushes the cache.



81
82
83
# File 'merb-cache/lib/merb-cache/stores/fundamental/memcached_store.rb', line 81

def delete_all
  @memcached.flush
end

#exists?(key, parameters = {}) ⇒ Boolean

returns true/false/nil based on if data identified by the key & parameters is persisted in the store.

With Memcached 1.2 protocol the only way to find if key exists in the cache is to read it. It is very fast and shouldn't be a concern.



61
62
63
64
65
66
67
68
69
# File 'merb-cache/lib/merb-cache/stores/fundamental/memcached_store.rb', line 61

def exists?(key, parameters = {})
  begin
    @memcached.get(normalize(key, parameters)) && true
  rescue  Memcached::Stored
    true
  rescue Memcached::NotFound
    nil
  end
end

#expire_time(conditions = {}) ⇒ Object

Returns expiration timestamp if :expire_in key is given.



114
115
116
117
118
119
120
# File 'merb-cache/lib/merb-cache/stores/fundamental/memcached_store.rb', line 114

def expire_time(conditions = {})
  if t = conditions[:expire_in]
    Time.now + t
  else
    0
  end
end

#fetch(key, parameters = {}, conditions = {}, &blk) ⇒ Object

Fetches cached data by key if it exists. If it does not, uses passed block to get new cached value and writes it using given key.



52
53
54
# File 'merb-cache/lib/merb-cache/stores/fundamental/memcached_store.rb', line 52

def fetch(key, parameters = {}, conditions = {}, &blk)
  read(key, parameters) || (writable?(key, parameters, conditions) && write(key, value = blk.call, parameters, conditions) && value)
end

#normalize(key, parameters = {}) ⇒ Object

Returns cache key calculated from base key and SHA2 hex from parameters.



109
110
111
# File 'merb-cache/lib/merb-cache/stores/fundamental/memcached_store.rb', line 109

def normalize(key, parameters = {})
  parameters.empty? ? "#{key}" : "#{key}--#{parameters.to_sha2}"
end

#read(key, parameters = {}) ⇒ Object

Reads key from the cache.



29
30
31
32
33
34
35
# File 'merb-cache/lib/merb-cache/stores/fundamental/memcached_store.rb', line 29

def read(key, parameters = {})
  begin
    @memcached.get(normalize(key, parameters))
  rescue Memcached::NotFound, Memcached::Stored
    nil
  end
end

#writable?(key, parameters = {}, conditions = {}) ⇒ Boolean

Memcached store consideres all keys and parameters writable.



24
25
26
# File 'merb-cache/lib/merb-cache/stores/fundamental/memcached_store.rb', line 24

def writable?(key, parameters = {}, conditions = {})
  true
end

#write(key, data = nil, parameters = {}, conditions = {}) ⇒ Object

Writes data to the cache using key, parameters and conditions.



38
39
40
41
42
43
44
45
46
47
# File 'merb-cache/lib/merb-cache/stores/fundamental/memcached_store.rb', line 38

def write(key, data = nil, parameters = {}, conditions = {})
  if writable?(key, parameters, conditions)
    begin
      @memcached.set(normalize(key, parameters), data, expire_time(conditions))
      true
    rescue
      nil
    end
  end
end