← Back to Upcase

Expiring api request caches


(Amree Zaid) #1

I’ve implemented API caching based on http://robots.thoughtbot.com/caching-api-requests. I’m using memory as the storage. How can I reset the cache manually without restarting the server?


(Chris Toomey) #2

Rails.cache.clear should work. You wrap that up in a rake task if that is easier to work with.


(Amree Zaid) #3

Thanks for the response but I’m not sure what’s wrong, but that doesn’t seem to work. The data is still getting pulled from the cache even after running Rails.cache.clear in the console. I checked it by observing the server log for my puts message (as shown below).

Caching code:

module Meh
  class Api
    include HTTParty
    
    #...

    cache_name = options[:path] + "/" + options[:params].values.join(",")

    response = nil
    APICache.get(cache_name, cache: 3600) do
      response = self.class.get options[:path], query: options[:params]

      # For future debugging
      puts "[API] Request: #{response.request.last_uri.to_s}"

      # Just return nil if there's an error with the request, for now
      if response.code == 200
        response.reverse!
      else
        response = nil
      end
    end
end

I’m using:

gem 'rails', '3.2.16'
gem 'httparty', '~> 0.11.0'
gem 'api_cache', '~> 0.2.3'