Class: RestCore::Cache
- Inherits:
-
Object
- Object
- RestCore::Cache
- Includes:
- Middleware, Wrapper
- Defined in:
- lib/rest-core/middleware/cache.rb
Constant Summary
Constants included from RestCore
ASYNC, CLIENT, DRY, FAIL, HIJACK, LOG, PROMISE, REQUEST_HEADERS, REQUEST_METHOD, REQUEST_PATH, REQUEST_PAYLOAD, REQUEST_QUERY, REQUEST_URI, RESPONSE_BODY, RESPONSE_HEADERS, RESPONSE_KEY, RESPONSE_SOCKET, RESPONSE_STATUS, Simple, TIMER, Universal, VERSION
Constants included from Middleware
Instance Attribute Summary
Attributes included from Wrapper
#default_engine, #middles, #wrapped
Class Method Summary collapse
Instance Method Summary collapse
- #cache_get(env) ⇒ Object
- #cache_key(env) ⇒ Object
- #call(env, &k) ⇒ Object
-
#initialize(app, cache, expires_in, &block) ⇒ Cache
constructor
A new instance of Cache.
Methods included from Wrapper
included, #members, partial_deep_copy, #run, #to_app, #use
Methods included from RestCore
Methods included from Middleware
contain_binary?, #contain_binary?, escape, #escape, #fail, #id, included, #log, #percent_encode, percent_encode, request_uri, #request_uri, #run, #string_keys, string_keys
Constructor Details
#initialize(app, cache, expires_in, &block) ⇒ Cache
Returns a new instance of Cache.
13 14 15 16 |
# File 'lib/rest-core/middleware/cache.rb', line 13 def initialize app, cache, expires_in, &block super(&block) @app, @cache, @expires_in = app, cache, expires_in end |
Class Method Details
.members ⇒ Object
9 |
# File 'lib/rest-core/middleware/cache.rb', line 9 def self.members; [:cache, :expires_in]; end |
Instance Method Details
#cache_get(env) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/rest-core/middleware/cache.rb', line 44 def cache_get env return unless cache(env) return unless cache_for?(env) start_time = Time.now return unless data = cache(env)[cache_key(env)] log(env, Event::CacheHit.new(Time.now - start_time, request_uri(env))). merge(data_extract(data)) end |
#cache_key(env) ⇒ Object
39 40 41 42 |
# File 'lib/rest-core/middleware/cache.rb', line 39 def cache_key env "rest-core:cache:#{Digest::MD5.hexdigest(env['cache.key'] || cache_key_raw(env))}" end |
#call(env, &k) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rest-core/middleware/cache.rb', line 18 def call env, &k e = if env['cache.update'] cache_clear(env) else env end if cached = cache_get(e) e[TIMER].cancel if e[TIMER] wrapped.call(cached, &k) else app.call(e){ |res| wrapped.call(res){ |res_wrapped| k.call(if (res_wrapped[FAIL] || []).empty? cache_for(res).merge(res_wrapped) else res_wrapped end)}} end end |