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.
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) ⇒ ResponsePromise
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, client_options: {}) ⇒ Cache
constructor
A new instance of Cache.
- #invalidate(url) ⇒ Object
Constructor Details
#initialize(redis: nil, client: nil, client_options: {}) ⇒ Cache
Returns a new instance of Cache.
21 22 23 24 |
# File 'lib/routemaster/cache.rb', line 21 def initialize(redis: nil, client: nil, client_options: {}) @redis = redis || Config.cache_redis @client = client || APIClient.new(.merge(listener: self)) end |
Instance Method Details
#_publish(event, url) ⇒ Object
This is because wisper makes broadcasting methods private
37 38 39 |
# File 'lib/routemaster/cache.rb', line 37 def _publish(event, url) publish(event, url) end |
#bust(url) ⇒ Object
Bust the cache for a given URL
27 28 29 30 |
# File 'lib/routemaster/cache.rb', line 27 def bust(url) @redis.del(Routemaster::CacheKey.url_key(url)) _publish(:cache_bust, url) end |
#fget(url, version: nil, locale: nil) ⇒ ResponsePromise
Like #get, but schedules any request in the background using a thread pool. Handy to issue lots of requests in parallel.
like [Response].
60 61 62 |
# File 'lib/routemaster/cache.rb', line 60 def fget(url, version: nil, locale: nil) @client.fget(url, headers: headers(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.
51 52 53 |
# File 'lib/routemaster/cache.rb', line 51 def get(url, version: nil, locale: nil) @client.get(url, headers: headers(version: version, locale: locale)) end |
#invalidate(url) ⇒ Object
32 33 34 |
# File 'lib/routemaster/cache.rb', line 32 def invalidate(url) EventIndex.new(url, cache: @redis).increment end |