From b50f0c8aa1118d425e4f03d887edf3726901a5e6 Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Tue, 29 Dec 2015 14:53:49 -0800 Subject: [PATCH] joyent/node-triton#63 "triton images" with a filter should not be cached --- CHANGES.md | 1 + lib/tritonapi.js | 28 ++++++++++++++++++++-------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 6982746..e4d45ab 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,7 @@ ## 3.4.2 (not yet released) +- #63 "triton images" with a filter should not be cached. - #65 Fix `triton profile(s)` handling when the user has no profiles yet. diff --git a/lib/tritonapi.js b/lib/tritonapi.js index faccfc6..84c0cf8 100644 --- a/lib/tritonapi.js +++ b/lib/tritonapi.js @@ -220,8 +220,11 @@ TritonApi.prototype._cacheGetJson = function _cacheGetJson(key, cb) { * CloudAPI listImages wrapper with optional caching. * * @param opts {Object} Optional. - * - useCase {Boolean} Whether to use Triton's local cache. - * - ... all other cloudapi ListImages options + * - useCache {Boolean} Default false. Whether to use Triton's local cache. + * Note that the *currently* implementation will only use the cache + * when there are no filter options. + * - ... all other cloudapi ListImages options per + * * @param {Function} callback `function (err, imgs)` */ TritonApi.prototype.listImages = function listImages(opts, cb) { @@ -237,14 +240,21 @@ TritonApi.prototype.listImages = function listImages(opts, cb) { var listOpts = common.objCopy(opts); delete listOpts.useCache; - var cacheKey = 'images.json'; + // For now at least, we only cache full results (no filtering). + var useCache = Boolean(opts.useCache); + var cacheKey; + if (Object.keys(listOpts).length > 0) { + useCache = false; + } else { + cacheKey = 'images.json'; + } var cached; var fetched; var res; vasync.pipeline({funcs: [ function tryCache(_, next) { - if (!opts.useCache) { + if (!useCache) { return next(); } self._cacheGetJson(cacheKey, function (err, cached_) { @@ -272,10 +282,11 @@ TritonApi.prototype.listImages = function listImages(opts, cb) { }, function cacheFetched(_, next) { - if (!fetched) { - return next(); + if (cacheKey && fetched) { + self._cachePutJson(cacheKey, fetched, next); + } else { + next(); } - self._cachePutJson(cacheKey, fetched, next); } ]}, function (err) { @@ -311,7 +322,8 @@ TritonApi.prototype.getImage = function getImage(opts, cb) { next(); return; } - self._cacheGetJson('images.json', function (err, images) { + var cacheKey = 'images.json'; + self._cacheGetJson(cacheKey, function (err, images) { if (err) { next(err); return;