Class: HTTP::Features::Caching::InMemoryStore

Inherits:
Object
  • Object
show all
Defined in:
lib/http/features/caching/in_memory_store.rb,
sig/http.rbs

Overview

Simple in-memory cache store backed by a Hash

Cache keys are derived from the request method and URI.

Examples:

store = InMemoryStore.new
store.store(request, entry)
store.lookup(request) # => entry

Instance Method Summary collapse

Constructor Details

#initializeInMemoryStore

Create a new empty in-memory store

Examples:

store = InMemoryStore.new


23
24
25
# File 'lib/http/features/caching/in_memory_store.rb', line 23

def initialize
  @cache = {}
end

Instance Method Details

#cache_key(request) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Compute the cache key for a request

Parameters:

Returns:

  • (String)


57
58
59
# File 'lib/http/features/caching/in_memory_store.rb', line 57

def cache_key(request)
  format("%s %s", request.verb, request.uri)
end

#lookup(request) ⇒ Entry?

Look up a cached entry for a request

Examples:

store.lookup(request) # => Entry or nil

Parameters:

Returns:



35
36
37
# File 'lib/http/features/caching/in_memory_store.rb', line 35

def lookup(request)
  @cache[cache_key(request)]
end

#store(request, entry) ⇒ Entry

Store a cache entry for a request

Examples:

store.store(request, entry)

Parameters:

Returns:



48
49
50
# File 'lib/http/features/caching/in_memory_store.rb', line 48

def store(request, entry)
  @cache[cache_key(request)] = entry
end