Merb::Cache::MemcachedStore

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.

Attributes

memcached[RW]
namespace[RW]
servers[RW]

Public Class Methods

new(config = {}) click to toggle source
# 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

Public Instance Methods

clone() click to toggle source
# File lib/merb-cache/stores/fundamental/memcached_store.rb, line 82
def clone
  twin = super
  twin.memcached = @memcached.clone
  twin
end
connect(config = {}) click to toggle source

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

# 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 = {}) click to toggle source

Deletes entry from cached by key.

# 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_all() click to toggle source

Flushes the cache.

# File lib/merb-cache/stores/fundamental/memcached_store.rb, line 78
def delete_all
  @memcached.flush
end
exists?(key, parameters = {}) click to toggle source

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.

# 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 = {}) click to toggle source

Returns expiration timestamp if :expire_in key is given.

# 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) click to toggle source

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.

# 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 = {}) click to toggle source

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

# 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 = {}) click to toggle source

Reads key from the cache.

# 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 = {}) click to toggle source

Memcached store consideres all keys and parameters writable.

# File lib/merb-cache/stores/fundamental/memcached_store.rb, line 20
def writable?(key, parameters = {}, conditions = {})
  true
end
write(key, data = nil, parameters = {}, conditions = {}) click to toggle source

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

# 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

[Validate]

Generated with the Darkfish Rdoc Generator 2.