Class: Routemaster::Cache
- Inherits:
-
Object
- Object
- Routemaster::Cache
- Includes:
- Wisper::Publisher
- Defined in:
- lib/routemaster/cache.rb
Overview
Caches GET requests.
Emits cache_bust
, cache_hit
, and cache_miss
events.
The requests themselves are handled by APIClient.
Note that Cache-Control
headers are intentionally ignored, as it is
assumed one will call #bust when the cache becomes stale.
This is for instance done automatically by Middleware::Cache upon receiving events from Routemaster.
Defined Under Namespace
Classes: FutureResponse, Pool
Instance Method Summary collapse
-
#_publish(event, url) ⇒ Object
This is because wisper makes broadcasting methods private.
-
#bust(url) ⇒ Object
Bust the cache for a given URL.
-
#fget(url, version: nil, locale: nil) ⇒ FutureResponse
Like #get, but schedules any request in the background using a thread pool.
-
#get(url, version: nil, locale: nil) ⇒ Response
Get the response from a URL, from the cache if possible.
-
#initialize(redis: nil, client: nil) ⇒ Cache
constructor
A new instance of Cache.
Constructor Details
#initialize(redis: nil, client: nil) ⇒ Cache
Returns a new instance of Cache.
63 64 65 66 |
# File 'lib/routemaster/cache.rb', line 63 def initialize(redis:nil, client:nil) @redis = redis || Config.cache_redis @client = client || APIClient.new(listener: self) end |
Instance Method Details
#_publish(event, url) ⇒ Object
This is because wisper makes broadcasting methods private
75 76 77 |
# File 'lib/routemaster/cache.rb', line 75 def _publish(event, url) publish(event, url) end |
#bust(url) ⇒ Object
Bust the cache for a given URL
69 70 71 72 |
# File 'lib/routemaster/cache.rb', line 69 def bust(url) @redis.del("cache:#{url}") _publish(:cache_bust, url) end |
#fget(url, version: nil, locale: nil) ⇒ FutureResponse
Like #get, but schedules any request in the background using a thread pool. Handy to issue lots of requests in parallel.
like [Response].
104 105 106 |
# File 'lib/routemaster/cache.rb', line 104 def fget(url, version: nil, locale: nil) FutureResponse.new { get(url, version: version, locale: locale) } end |
#get(url, version: nil, locale: nil) ⇒ Response
Get the response from a URL, from the cache if possible. Stores to the cache on misses.
Different versions and locales are stored separately in the cache.
header.
89 90 91 92 93 94 95 96 97 |
# File 'lib/routemaster/cache.rb', line 89 def get(url, version: nil, locale: nil) headers = { 'Accept' => version ? "application/json;v=#{version}" : "application/json" } headers['Accept-Language'] = locale if locale @client.get(url, headers: headers) end |