0
我已根据http://robots.thoughtbot.com/caching-api-requests实施API缓存。我正在使用内存作为存储。如何在不重新启动服务器的情况下手动重置缓存?即将到期的API请求缓存
我试过使用Rails.cache.clear
,但它似乎没有工作。数据仍然从缓存中获取。我通过观察我的puts
消息的服务器日志来检查它(如下所示)。
缓存代码:
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
'rake tmp:cache:clear'不起作用,我的'tmp/cache'目录中没有文件。实际上,我想创建一个rake任务(如果它还不是任务)来清除缓存并可能从我的代码中调用它。 – amree
通过读取api_cache gem,它看起来像是内存缓存,而不是文件缓存。这与您的报告一致。它看起来像APICache api上有一个.delete方法。 [link](https://github.com/mloughran/api_cache/tree/master/lib/api_cache)所以APICache.delete(cache_name)可能就是你要找的。 – mykewould
我想我已经根据你的评论解决了它。由于最新的代码更改(与删除密钥相关)尚未发布,因此我必须从Github中设置gem的源代码。我也注意到,从控制台删除密钥不会清除正在运行的服务器中的密钥,因为它们使用的是不同的内存。我必须使用其他类型的存储(如Redis)来确保从控制台中删除的密钥也会删除正在运行的服务器的密钥。也许你可以根据你的评论反映你的答案。非常感谢提示。 – amree