Skip to content

Commit

Permalink
When fetching a feed, if the cached response is still valid, skip pro…
Browse files Browse the repository at this point in the history
…cessing the response.

This should improve performance: scheduled feed updates now don't parse and attempt to save entries which we know are already in the database, because the response is cached and there are no new entries.

There are two possible ways that a cached response may still be valid:

- the response has headers that indicate it will be valid until a certain time in the future ("fresh"). In this case the feed server is not called, we already know the response is valid.
- the response does not have headers indicating that it is still valid ("stale"). However it does include conditional GET headers (last-modified, etag). Those headers are sent to the feed server and an HTTP 304 is returned ("valid").

The determination of whether the cache is "fresh" or "valid" is made looking at the X-Rack-Cache HTTP header. Rack-cache writes in this header the trace of cache operations it performs on the response.
  • Loading branch information
amatriain committed Mar 23, 2015
1 parent 3cfc3a6 commit ed1aa26
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/clients/feed_client.rb
Expand Up @@ -87,6 +87,13 @@ def self.fetch_valid_feed(feed, perform_autodiscovery)

feed_response = RestClient.get url, user_agent: user_agent

# If the response was retrieved from the cache, do not process it (entries are already in the db)
x_rack_cache = feed_response.headers[:x_rack_cache]
if x_rack_cache.include?('fresh') || (x_rack_cache.include?('valid') && !x_rack_cache.include?('invalid'))
Rails.logger.info "Feed #{feed.id} - #{feed.fetch_url} cached response is valid, there are no new entries. Skipping response processing."
return nil
end

# Specify encoding ISO-8859-1 if necessary
if feed_response.try(:encoding)==Encoding::UTF_8 && !feed_response.try(:valid_encoding?)
feed_response.force_encoding 'iso-8859-1'
Expand Down
Empty file removed rack_cache/entitystore/.gitkeep
Empty file.
Empty file removed rack_cache/metastore/.gitkeep
Empty file.

0 comments on commit ed1aa26

Please sign in to comment.