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

#lookup, #store

Constructor Details

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

Returns a new instance of MemCache.



232
233
234
235
236
237
238
239
240
# File 'lib/rack/cache/metastore.rb', line 232

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.



230
231
232
# File 'lib/rack/cache/metastore.rb', line 230

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



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/rack/cache/metastore.rb', line 271

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



254
255
256
257
258
259
260
# File 'lib/rack/cache/metastore.rb', line 254

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

#read(key) ⇒ Object



242
243
244
245
246
247
# File 'lib/rack/cache/metastore.rb', line 242

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

#write(key, entries) ⇒ Object



249
250
251
252
# File 'lib/rack/cache/metastore.rb', line 249

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