Class: Resourceful::MemcacheCacheManager

Inherits:
AbstractCacheManager show all
Defined in:
lib/resourceful/memcache_cache_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(*memcache_servers) ⇒ MemcacheCacheManager

Create a new Memcached backed cache manager

Parameters:

  • memcache_servers (*String)

    list of all Memcached servers this cache manager should use.



12
13
14
# File 'lib/resourceful/memcache_cache_manager.rb', line 12

def initialize(*memcache_servers)
  @memcache = MemCache.new(memcache_servers, :multithread => true)
end

Instance Method Details

#invalidate(uri) ⇒ Object

Invalidates a all cached entries for a uri.

This is used, for example, to invalidate the cache for a resource that gets POSTed to.

Parameters:

  • uri (String)

    The uri of the resource to be invalidated



60
61
62
# File 'lib/resourceful/memcache_cache_manager.rb', line 60

def invalidate(uri)
  @memcache.delete(uri_hash(uri))
end

#lookup(request) ⇒ Resourceful::Response?

Finds a previously cached response to the provided request. The response returned may be stale.

Parameters:

Returns:

  • (Resourceful::Response, nil)

    A (possibly stale) response for the request provided or nil if no matching response is found.



25
26
27
28
29
30
31
32
# File 'lib/resourceful/memcache_cache_manager.rb', line 25

def lookup(request)
  resp = cache_entries_for(request)[request]
  return if resp.nil?

  resp.authoritative = false

  resp
end

#store(request, response) ⇒ Object

Store a response in the cache.

This method is smart enough to not store responses that cannot be cached (Vary: * or Cache-Control: no-cache, private, …)

Parameters:

  • request (Resourceful::Request)

    The request used to obtain the response. This is needed so the values from the response’s Vary header can be stored.

  • response (Resourceful::Response)

    The response to be stored.



44
45
46
47
48
49
50
51
# File 'lib/resourceful/memcache_cache_manager.rb', line 44

def store(request, response)
  return unless response.cachable?

  entries = cache_entries_for(request)
  entries[request] = response

  @memcache[request.to_mc_key] = entries
end