mloughran / api_cache Goto Github PK
View Code? Open in Web Editor NEWSimple library which makes it easy to add caching to all your external API calls.
Home Page: http://mloughran.github.com/api_cache/
License: MIT License
Simple library which makes it easy to add caching to all your external API calls.
Home Page: http://mloughran.github.com/api_cache/
License: MIT License
Hi there,
I'm feeling pretty stupid, but could somebody (ideally you Martyn) explain the purpose of the :period attribute in the APICache options?
In particular if the value is smaller than :cache (i.e. the time-to-live), as it is in the example given in the documentation. Isn't it the case then that if the API is hit more frequently than :period, the cache should simply be current?
Or is the use case of :period that you want to avoid to call the API too frequently in case you manually invalidate the cache? Another idea: is this done to avoid concurrency issues, where many processes/threads simultaneously try to refetch a new value from the API in case the cached value's time-to-live is over?
Sorry, very new, just wondering where inr ails this goes...
Looks like the version on rubygems is a bit outdated
First of all wanna thank you for this utility gem! Just wanna share some constructive suggestions.
I believe period: 0
should be the default. A very common case is to get
a result (here we wanna enforce a period), and to have an "update" get
for the result (where it's very important to have NO period as updates could happen at any time) As a result in my code I cannot use :period
at all, cause the :period
set when I "get" is gonna block me from making updates (which might happen soon after)
Hope that makes sense. In short I suggest:
period: 0
the defaultignore_period: true
(this would allow one to "update" at anytime while still having a period for actual fetches)EDIT: Actually I realized that I didn't have to wrap my update request at all - just needed to delete the local cache after the update, so the "option to ignore period" prob isn't as useful as I thought... still think period should default to 0 to reduce confusion tho
Hi
Is this cache method compatible with the HttParty gem.
I am using it to access flight information from a service. The response takes lots of time. Actually a lot of data is returned.
Pls advise
Thanks.
I don't see any mention of conditional GET requests. If it's not supported, I'd certainly like it added. If it is supported, perhaps the readme could be updated?
Specifically, what I'm looking for is:
If I make an API request outside of the period
, hit the API with appropriate If-Modified-Since and If-None-Match headers, and if the response is a 304 - Not Modified, then serve the cached response.
Currently, as long as stale data is available, the only notice you'll get that an API is failing is a warning in the logs. However, if for example, if the API you're consuming has changed, you'd like to be notified during the stale period so that you can update your code.
I'm thinking webhooks. Maybe a global configurable address which if supplied it would be pinged on failure during the stale period.
So I have multiple values that I want to fetch in bulk but cache separately, is that possible with this gem? I think I could achieve this by fetching each key individually and then storing them individually, but this seems to be circumventing the library. Is there a better way to accomplish this?
I'm building a gem and added memcached as a dependency but then when I add it:
require "coinmarketcap/wrapper/version"
require "coinmarketcap/wrapper/api_client"
require "coinmarketcap/wrapper/coin"
require "coinmarketcap/wrapper/market_data"
require 'json'
require 'api_cache'
require "moneta"
require "memcached"
module Coinmarketcap
module Wrapper
BASE_API_URL = "https://api.coinmarketcap.com/v1"
APICache.store = Moneta.new(:Memcached)
end
end
and then run bin/console
I get an error:
Memcached::ServerIsMarkedDead: Key {"9aaf5eb3d6edd10f12643e3087b84d21"=>"127.0.0.1:11211:8"}
is there a configuration I am missing?
Hi Martyrn,
Can I upgrade api_cache to use the latest version of the Moneta gem. This gem has a different but more importantly uniform interface for creating the stores. The use of this version will only require changes to the Markdown and specs.
For some reason, when I try to fetch something, the error "queried too recently" is raised.
Cannot fetch url: queried too recently
.../api_cache-0.2.3/lib/api_cache/api.rb:81:in `check_queryable!'
.../api_cache-0.2.3/lib/api_cache/api.rb:35:in `get'
.../api_cache-0.2.3/lib/api_cache.rb:108:in `get'
lib/ext/nokogiri.rb:18:in `get_cached'
lib/ext/nokogiri.rb:32:in `get_cached'
I debugged and the cache_state is "missing". It makes sense because it is a fresh run.
My code that used APICache:
class Nokogiri::HTML::Document
# Use open-uri to get document and set url accordingly.
def self.get(url)
raw = open(url)
parse(raw, raw.base_uri.try(:to_s)) # handles redirected url
end
end
module Nokogiri::HTML
def self.get(url)
Document.get(url)
end
end
# with APICache
class Nokogiri::HTML::Document
# Use open-uri to get document and set url accordingly.
def self.get_cached(url)
doc = APICache.get('nokogiri' << url) do
get(url)
end
# If redirected, add new url to the cache as well
if doc.url != url
APICache.get('nokogiri' << doc.url) do
doc
end
end
doc
end
end
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.