Module: Flexirest::Caching::ClassMethods

Defined in:
lib/flexirest/caching.rb

Constant Summary collapse

@@perform_caching =
true

Instance Method Summary collapse

Instance Method Details

#_reset_caching!Object



42
43
44
45
46
# File 'lib/flexirest/caching.rb', line 42

def _reset_caching!
  @@perform_caching = false
  @perform_caching = false
  @@cache_store = nil
end

#cache_storeObject



33
34
35
36
37
38
39
40
# File 'lib/flexirest/caching.rb', line 33

def cache_store
  rails_cache_store = if Object.const_defined?(:Rails)
    ::Rails.try(:cache)
  else
    nil
  end
  (@@cache_store rescue nil) || rails_cache_store
end

#cache_store=(value) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/flexirest/caching.rb', line 25

def cache_store=(value)
  @@cache_store = nil if value.nil? and return
  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
17
18
# File 'lib/flexirest/caching.rb', line 6

def perform_caching(value = nil)
  @perform_caching ||= nil
  @@perform_caching ||= nil
  if value.nil?
    if @perform_caching.nil?
      @@perform_caching
    else
      @perform_caching
    end
  else
    @perform_caching = value
  end
end

#perform_caching=(value) ⇒ Object



20
21
22
23
# File 'lib/flexirest/caching.rb', line 20

def perform_caching=(value)
  @@perform_caching = value
  @perform_caching = value
end

#read_cached_response(request) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/flexirest/caching.rb', line 48

def read_cached_response(request)
  if cache_store && perform_caching
    key = "#{request.class_name}:#{request.original_url}"
    Flexirest::Logger.debug "  \033[1;4;32m#{Flexirest.name}\033[0m #{key} - Trying to read from cache"
    value = cache_store.read(key)
    value = Marshal.load(value) rescue value
  end
end

#write_cached_response(request, response, result) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/flexirest/caching.rb', line 57

def write_cached_response(request, response, result)
  return if result.is_a? Symbol
  return unless perform_caching
  return unless !result.respond_to?(:_status) || [200, 304].include?(result._status)
  headers = response.response_headers

  headers.keys.select{|h| h.is_a? String}.each do |key|
    headers[key.downcase.to_sym] = headers[key]
  end

  if cache_store && (headers[:etag] || headers[:expires])
    key = "#{request.class_name}:#{request.original_url}"
    Flexirest::Logger.debug "  \033[1;4;32m#{Flexirest.name}\033[0m #{key} - Writing to cache"
    cached_response = CachedResponse.new(status:response.status, result:result)
    cached_response.etag = headers[:etag] if headers[:etag]
    cached_response.expires = Time.parse(headers[:expires]) rescue nil if headers[:expires]
    cache_store.write(key, Marshal.dump(cached_response), {}) if cached_response.etag.present? || cached_response.expires
  end
end