Class: Rack::Cache::MetaStore::MemCache

Inherits:
Rack::Cache::MetaStore show all
Extended by:
Utils
Defined in:
lib/rack/cache/metastore.rb

Overview

Stores request/response pairs in memcached. Keys are not stored directly since memcached has a 250-byte limit on key names. Instead, the SHA1 hexdigest of the key is used.

Constant Summary

Constants inherited from Rack::Cache::MetaStore

DISK, FILE, HEAP, MEM, MEMCACHE, MEMCACHED

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Rack::Cache::MetaStore

#cache_key, #invalidate, #lookup, #store

Constructor Details

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

Returns a new instance of MemCache.



268
269
270
271
272
273
274
275
276
# File 'lib/rack/cache/metastore.rb', line 268

def initialize(server="localhost:11211", options={})
  @cache =
    if server.respond_to?(:stats)
      server
    else
      require 'memcached'
      Memcached.new(server, options)
    end
end

Instance Attribute Details

#cacheObject (readonly)

The Memcached instance used to communicated with the memcached daemon.



266
267
268
# File 'lib/rack/cache/metastore.rb', line 266

def cache
  @cache
end

Class Method Details

.resolve(uri) ⇒ Object

Create MemCache store for the given URI. The URI must specify a host and may specify a port, namespace, and options:

memcached://example.com:11211/namespace?opt1=val1&opt2=val2

Query parameter names and values are documented with the memcached library: tinyurl.com/4upqnd



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/rack/cache/metastore.rb', line 307

def self.resolve(uri)
  server = "#{uri.host}:#{uri.port || '11211'}"
  options = parse_query(uri.query)
  options.keys.each do |key|
    value =
      case value = options.delete(key)
      when 'true' ; true
      when 'false' ; false
      else value.to_sym
      end
    options[k.to_sym] = value
  end
  options[:namespace] = uri.path.sub(/^\//, '')
  new server, options
end

Instance Method Details

#purge(key) ⇒ Object



290
291
292
293
294
295
296
# File 'lib/rack/cache/metastore.rb', line 290

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

#read(key) ⇒ Object



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

def read(key)
  key = hexdigest(key)
  cache.get(key)
rescue Memcached::NotFound
  []
end

#write(key, entries) ⇒ Object



285
286
287
288
# File 'lib/rack/cache/metastore.rb', line 285

def write(key, entries)
  key = hexdigest(key)
  cache.set(key, entries)
end