Class: Rack::Cache::MetaStore::MemCache
- Inherits:
-
Rack::Cache::MetaStore
- Object
- Rack::Cache::MetaStore
- Rack::Cache::MetaStore::MemCache
- 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
-
#cache ⇒ Object
readonly
The Memcached instance used to communicated with the memcached daemon.
Class Method Summary collapse
-
.resolve(uri) ⇒ Object
Create MemCache store for the given URI.
Instance Method Summary collapse
-
#initialize(server = "localhost:11211", options = {}) ⇒ MemCache
constructor
A new instance of MemCache.
- #purge(key) ⇒ Object
- #read(key) ⇒ Object
- #write(key, entries) ⇒ Object
Methods inherited from Rack::Cache::MetaStore
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", ={}) @cache = if server.respond_to?(:stats) server else require 'memcached' Memcached.new(server, ) end end |
Instance Attribute Details
#cache ⇒ Object (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'}" = parse_query(uri.query) .keys.each do |key| value = case value = .delete(key) when 'true' ; true when 'false' ; false else value.to_sym end [k.to_sym] = value end [:namespace] = uri.path.sub(/^\//, '') new server, 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 |