Added light caching for requests

Signed-off-by: French Ben <frenchben@docker.com>
This commit is contained in:
French Ben 2016-08-01 17:52:07 -07:00
Родитель 80f3c6a99b
Коммит 86153c7c88
2 изменённых файлов: 14 добавлений и 6 удалений

Просмотреть файл

@ -1,6 +1,6 @@
{ {
"name": "Kitematic", "name": "Kitematic",
"version": "0.12.0", "version": "0.12.0.1",
"author": "Kitematic", "author": "Kitematic",
"description": "Simple Docker Container management for Mac OS X, Windows and Ubuntu.", "description": "Simple Docker Container management for Mac OS X, Windows and Ubuntu.",
"homepage": "https://kitematic.com/", "homepage": "https://kitematic.com/",
@ -28,6 +28,7 @@
"async": "^1.4.2", "async": "^1.4.2",
"bluebird": "^2.9.24", "bluebird": "^2.9.24",
"bugsnag-js": "^2.4.7", "bugsnag-js": "^2.4.7",
"cached-request": "^0.1.4",
"classnames": "^2.1.5", "classnames": "^2.1.5",
"coveralls": "^2.11.2", "coveralls": "^2.11.2",
"deep-extend": "^0.4.0", "deep-extend": "^0.4.0",

Просмотреть файл

@ -5,6 +5,11 @@ import util from '../utils/Util';
import hubUtil from '../utils/HubUtil'; import hubUtil from '../utils/HubUtil';
import repositoryServerActions from '../actions/RepositoryServerActions'; import repositoryServerActions from '../actions/RepositoryServerActions';
import tagServerActions from '../actions/TagServerActions'; import tagServerActions from '../actions/TagServerActions';
import os from 'os';
var cachedRequest = require('cached-request')(request);
var cacheDirectory = os.tmpDir() + '/cachekitematic';
cachedRequest.setCacheDirectory(cacheDirectory);
cachedRequest.set('ttl', 3000);
let REGHUB2_ENDPOINT = process.env.REGHUB2_ENDPOINT || 'https://hub.docker.com/v2'; let REGHUB2_ENDPOINT = process.env.REGHUB2_ENDPOINT || 'https://hub.docker.com/v2';
let searchReq = null; let searchReq = null;
@ -27,7 +32,7 @@ module.exports = {
search: function (query, page, sorting = null) { search: function (query, page, sorting = null) {
if (searchReq) { if (searchReq) {
searchReq.abort(); searchReq.request.abort();
searchReq = null; searchReq = null;
} }
@ -43,7 +48,7 @@ module.exports = {
* is_official: 1 * is_official: 1
*/ */
searchReq = request.get({ searchReq = cachedRequest({
url: `${REGHUB2_ENDPOINT}/search/repositories/?`, url: `${REGHUB2_ENDPOINT}/search/repositories/?`,
qs: {query: query, page: page, page_size: PAGING, sorting} qs: {query: query, page: page, page_size: PAGING, sorting}
}, (error, response, body) => { }, (error, response, body) => {
@ -66,7 +71,9 @@ module.exports = {
}, },
recommended: function () { recommended: function () {
request.get('https://kitematic.com/recommended.json', (error, response, body) => { cachedRequest({
url: 'https://kitematic.com/recommended.json'
}, (error, response, body) => {
if (error) { if (error) {
repositoryServerActions.error({error}); repositoryServerActions.error({error});
return; return;
@ -85,7 +92,7 @@ module.exports = {
name = 'library/' + name; name = 'library/' + name;
} }
request.get({ cachedRequest({
url: `${REGHUB2_ENDPOINT}/repositories/${name}` url: `${REGHUB2_ENDPOINT}/repositories/${name}`
}, (error, response, body) => { }, (error, response, body) => {
if (error) { if (error) {
@ -168,7 +175,7 @@ module.exports = {
}); });
// Add current user // Add current user
namespaces.push(hubUtil.username()); namespaces.push(hubUtil.username());
} catch(jsonError) { } catch (jsonError) {
repositoryServerActions.error({jsonError}); repositoryServerActions.error({jsonError});
if (callback) { if (callback) {
return callback(jsonError); return callback(jsonError);