class Rack::Cache::EntityStore::MemCached

Uses the memcached client library. The ruby based memcache-client is used in preference to this store unless the memcached library has already been required.

Public Class Methods

new(server="localhost:11211", options={}) click to toggle source
# File lib/rack/cache/entity_store.rb, line 246
def initialize(server="localhost:11211", options={})
  options[:prefix_key] ||= options.delete(:namespace) if options.key?(:namespace)
  @cache =
    if server.respond_to?(:stats)
      server
    else
      require 'memcached'
      ::Memcached.new(server, options)
    end
end

Public Instance Methods

exist?(key) click to toggle source
# File lib/rack/cache/entity_store.rb, line 257
def exist?(key)
  cache.append(key, '')
  true
rescue ::Memcached::NotStored
  false
end
purge(key) click to toggle source
# File lib/rack/cache/entity_store.rb, line 277
def purge(key)
  cache.delete(key)
  nil
rescue ::Memcached::NotFound
  nil
end
read(key) click to toggle source
# File lib/rack/cache/entity_store.rb, line 264
def read(key)
  cache.get(key, false)
rescue ::Memcached::NotFound
  nil
end
write(body, ttl=0) click to toggle source
# File lib/rack/cache/entity_store.rb, line 270
def write(body, ttl=0)
  buf = StringIO.new
  key, size = slurp(body){|part| buf.write(part) }
  cache.set(key, buf.string, ttl, false)
  [key, size]
end