Class: Merb::Cache::MemcachedStore

Inherits:
AbstractStore show all
Defined in:
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.



11
12
13
14
15
16
# File 'lib/merb-cache/stores/fundamental/memcached_store.rb', line 11

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 '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 '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 'lib/merb-cache/stores/fundamental/memcached_store.rb', line 9

def servers
  @servers
end

Instance Method Details

#cloneObject



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

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

#connect(config = {}) ⇒ Object

Establishes connection to Memcached.

Use :buffer_requests option to use bufferring, :no_block to use non-blocking async I/O. :support_cas to support CAS



93
94
95
# File 'lib/merb-cache/stores/fundamental/memcached_store.rb', line 93

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.



69
70
71
72
73
74
75
# File 'lib/merb-cache/stores/fundamental/memcached_store.rb', line 69

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

#delete_allObject

Flushes the cache.



78
79
80
# File 'lib/merb-cache/stores/fundamental/memcached_store.rb', line 78

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.

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
66
# File 'lib/merb-cache/stores/fundamental/memcached_store.rb', line 58

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.



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

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.



48
49
50
# File 'lib/merb-cache/stores/fundamental/memcached_store.rb', line 48

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.



99
100
101
# File 'lib/merb-cache/stores/fundamental/memcached_store.rb', line 99

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

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

Reads key from the cache.



25
26
27
28
29
30
31
# File 'lib/merb-cache/stores/fundamental/memcached_store.rb', line 25

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.

Returns:

  • (Boolean)


20
21
22
# File 'lib/merb-cache/stores/fundamental/memcached_store.rb', line 20

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

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

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



34
35
36
37
38
39
40
41
42
43
# File 'lib/merb-cache/stores/fundamental/memcached_store.rb', line 34

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