Class: Rack::Cache::EntityStore::MemCached

Inherits:
MemCacheBase show all
Defined in:
lib/rack/cache/entity_store.rb

Overview

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.

Constant Summary

Constants inherited from Rack::Cache::EntityStore

DISK, FILE, GAE, GAECACHE, HEAP, MEM, MEMCACHE, MEMCACHED, NOOP

Instance Attribute Summary

Attributes inherited from MemCacheBase

#cache

Instance Method Summary collapse

Methods inherited from MemCacheBase

#open, resolve

Constructor Details

#initialize(server = "localhost:11211", options = {}) ⇒ MemCached

Returns a new instance of MemCached.



246
247
248
249
250
251
252
253
254
255
# 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

Instance Method Details

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


257
258
259
260
261
262
# File 'lib/rack/cache/entity_store.rb', line 257

def exist?(key)
  cache.append(key, '')
  true
rescue ::Memcached::NotStored
  false
end

#purge(key) ⇒ Object



277
278
279
280
281
282
# File 'lib/rack/cache/entity_store.rb', line 277

def purge(key)
  cache.delete(key)
  nil
rescue ::Memcached::NotFound
  nil
end

#read(key) ⇒ Object



264
265
266
267
268
# 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) ⇒ Object



270
271
272
273
274
275
# 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