Module: ActiveRestClient::Caching::ClassMethods
- Defined in:
- lib/active_rest_client/caching.rb
Constant Summary collapse
- @@perform_caching =
true
Instance Method Summary collapse
- #_reset_caching! ⇒ Object
- #cache_store ⇒ Object
- #cache_store=(value) ⇒ Object
- #perform_caching(value = nil) ⇒ Object
- #perform_caching=(value) ⇒ Object
- #read_cached_response(request) ⇒ Object
- #write_cached_response(request, response, result) ⇒ Object
Instance Method Details
#_reset_caching! ⇒ Object
39 40 41 42 43 |
# File 'lib/active_rest_client/caching.rb', line 39 def _reset_caching! @@perform_caching = false @perform_caching = false @@cache_store = nil end |
#cache_store ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/active_rest_client/caching.rb', line 30 def cache_store rails_cache_store = if Object.const_defined?(:Rails) ::Rails.cache else nil end (@@cache_store rescue nil) || rails_cache_store end |
#cache_store=(value) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/active_rest_client/caching.rb', line 23 def cache_store=(value) raise InvalidCacheStoreException.new("Cache store does not implement #read") unless value.respond_to?(:read) raise InvalidCacheStoreException.new("Cache store does not implement #write") unless value.respond_to?(:write) raise InvalidCacheStoreException.new("Cache store does not implement #fetch") unless value.respond_to?(:fetch) @@cache_store = value end |
#perform_caching(value = nil) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/active_rest_client/caching.rb', line 6 def perform_caching(value = nil) if value.nil? if @perform_caching.nil? @@perform_caching else @perform_caching end else @perform_caching = value end end |
#perform_caching=(value) ⇒ Object
18 19 20 21 |
# File 'lib/active_rest_client/caching.rb', line 18 def perform_caching=(value) @@perform_caching = value @perform_caching = value end |
#read_cached_response(request) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/active_rest_client/caching.rb', line 45 def read_cached_response(request) if cache_store key = "#{request.class_name}:#{request.original_url}" cache_store.read(key) end end |
#write_cached_response(request, response, result) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/active_rest_client/caching.rb', line 52 def write_cached_response(request, response, result) response.headers.keys.select{|h| h.is_a? String}.each do |key| response.headers[key.downcase.to_sym] = response.headers[key] end if cache_store && (response.headers[:etag] || response.headers[:expires]) key = "#{request.class_name}:#{request.original_url}" ActiveRestClient::Logger.debug " \033[1;4;32m#{ActiveRestClient::NAME}\033[0m #{key} - Writing to cache" cached_response = CachedResponse.new(status:response.status, result:result) cached_response.etag = response.headers[:etag] if response.headers[:etag] cached_response.expires = Time.parse(response.headers[:expires]) if response.headers[:expires] cache_store.write(key, cached_response, {}) end end |