diff --git a/src/instafeed.js b/src/instafeed.js index 44fa414..76def9d 100644 --- a/src/instafeed.js +++ b/src/instafeed.js @@ -17,6 +17,7 @@ function Instafeed(options) { apiTimeout: 10000, apiLimit: null, before: null, + cacheTimeout: 0, debug: false, error: null, filter: null, @@ -510,13 +511,27 @@ Instafeed.prototype._makeApiRequest = function makeApiRequest(url, callback) { var called = false; var scope = this; var apiRequest = null; + var cachedRequest = this._cache[url]; var callbackOnce = function callbackOnce(err, value) { if (!called) { called = true; + if (scope._options.cacheTimeout) { + scope._cache[url] = { + date: new Date().getTime(), + err: err, + value: value + }; + } callback(err, value); } }; + if (cachedRequest && this._options.cacheTimeout) { + if ((cachedRequest.date - this._options.cacheTimeout) < new Date().getTime()) { + return callback(cachedRequest.err, cachedRequest.value); + } + } + apiRequest = new XMLHttpRequest(); apiRequest.ontimeout = function apiRequestTimedOut() { @@ -621,4 +636,6 @@ Instafeed.prototype._runHook = function runHook(hookName, data) { return success; }; +Instafeed.prototype._cache = {}; + export default Instafeed;