Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option cacheTimeout #725

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/instafeed.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function Instafeed(options) {
apiTimeout: 10000,
apiLimit: null,
before: null,
cacheTimeout: 0,
debug: false,
error: null,
filter: null,
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -621,4 +636,6 @@ Instafeed.prototype._runHook = function runHook(hookName, data) {
return success;
};

Instafeed.prototype._cache = {};

export default Instafeed;