her icon indicating copy to clipboard operation
her copied to clipboard

Cache invalidation policy

Open letronje opened this issue 13 years ago • 8 comments

How easy is it to define a custom cache invalidation policy for the caching middleware that comes with faraday ? ( FaradayMiddleware::Caching ) .

What could be some good cache invalidation strategies that can be used with 'Her' ?

letronje avatar Apr 18 '13 04:04 letronje

Take a look at https://github.com/plataformatec/faraday-http-cache/

teamon avatar Jul 02 '13 13:07 teamon

@teamon did you ever implement something based on "Faraday Http Cache"? If so, could you please provide a simple example so we can add it to the documentation?

pencil avatar Oct 15 '13 17:10 pencil

Actually I did :) On Her side this is as simple as adding c.user :http_cache to faraday connection middleware. In fact Her does not care (nor it should) about cache at all, it is all done at the faraday's level by using standard HTTP headers based cache. So on client side this is really just a oneliner. On server side take a look at stuff like expires_in

teamon avatar Oct 15 '13 17:10 teamon

Meh, I read my post again - please forget it. I'll prepare something serious for docs :)

teamon avatar Oct 15 '13 17:10 teamon

api = Her::API.new
api.setup :url => "http://some.api.com" do |c|
  ...
  c.use :http_cache, Rails.cache, :logger => Rails.logger # from faraday-http-cache gem
  c.adapter Faraday.default_adapter
end

class Post
  include Her::Model
  uses_api api
end

Something like this should be fine

teamon avatar Oct 15 '13 17:10 teamon

Thank you, @teamon! Think we should change the documentation as this middleware seems to be superior to the one suggested by the README. @remiprev, what do you think?

pencil avatar Oct 15 '13 17:10 pencil

Any way to force a request when using an API that doesn't specify caching? Basically I want the resource to expire when doing an update or be able to invalidate it manually. I am using faraday middleware for caching. I tried using faraday-http-cache but could not get it to cache anything at all but I assume it would have the same issue if it worked.

espen avatar Jul 14 '14 15:07 espen

@pencil I assume faraday-http-cache is superior if your api provides all required http cache headers, otherwise maybe like @espen replied nothing will be cached, and personally I don't see/recall in a real world api return those http cache headers quite often in my experience.

my suggestion is keep the README as is, but could mention the options of faraday-http-cache if your api also "respects" http caching.


and back to the original issue, i still can't find a good solution to expire a cache manually. maybe could borrow the idea of https://github.com/plataformatec/faraday-http-cache/issues/36#issuecomment-38464461, a hack to faraday-http-cache so can't apply directly here but the idea should be similar, supply a specific parameter(invalidate_cache in the example) that will intercept the response and clear the cache in the middleware layer.

kinopyo avatar Aug 25 '14 06:08 kinopyo