mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-28 05:37:34 +03:00
Refactored cached image size to have cache as DI
refs https://github.com/TryGhost/Toolbox/issues/364 - Groundwork before swapping out existing cache for in-memory cache from the adapter
This commit is contained in:
parent
d7aa064e0f
commit
7d3b678d4e
@ -1,10 +1,17 @@
|
||||
const debug = require('@tryghost/debug')('utils:image-size-cache');
|
||||
const errors = require('@tryghost/errors');
|
||||
const logging = require('@tryghost/logging');
|
||||
|
||||
class CachedImageSizeFromUrl {
|
||||
constructor({imageSize}) {
|
||||
/**
|
||||
*
|
||||
* @param {Object} options
|
||||
* @param {Object} options.imageSize - instance with "getImageSizeFromUrl" method
|
||||
* @param {Object} options.cache - cache store instance
|
||||
*/
|
||||
constructor({imageSize, cache}) {
|
||||
this.imageSize = imageSize;
|
||||
this.cache = new Map();
|
||||
this.cache = cache;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -19,31 +26,32 @@ class CachedImageSizeFromUrl {
|
||||
if (!url || url === undefined || url === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// image size is not in cache
|
||||
if (!this.cache.has(url)) {
|
||||
return this.imageSize.getImageSizeFromUrl(url).then((res) => {
|
||||
this.cache.set(url, res);
|
||||
|
||||
|
||||
debug('Cached image:', url);
|
||||
|
||||
|
||||
return this.cache.get(url);
|
||||
}).catch(errors.NotFoundError, () => {
|
||||
debug('Cached image (not found):', url);
|
||||
// in case of error we just attach the url
|
||||
this.cache.set(url, url);
|
||||
|
||||
|
||||
return this.cache.get(url);
|
||||
}).catch((err) => {
|
||||
debug('Cached image (error):', url);
|
||||
logging.error(err);
|
||||
|
||||
|
||||
// in case of error we just attach the url
|
||||
this.cache.set(url, url);
|
||||
|
||||
|
||||
return this.cache.get(url);
|
||||
});
|
||||
}
|
||||
|
||||
debug('Read image from cache:', url);
|
||||
// returns image size from cache
|
||||
return this.cache.get(url);
|
||||
|
@ -7,7 +7,10 @@ class ImageUtils {
|
||||
constructor({config, urlUtils, settingsCache, storageUtils, storage, validator, request}) {
|
||||
this.blogIcon = new BlogIcon({config, urlUtils, settingsCache, storageUtils});
|
||||
this.imageSize = new ImageSize({config, storage, storageUtils, validator, urlUtils, request});
|
||||
this.cachedImageSizeFromUrl = new CachedImageSizeFromUrl({imageSize: this.imageSize});
|
||||
this.cachedImageSizeFromUrl = new CachedImageSizeFromUrl({
|
||||
imageSize: this.imageSize,
|
||||
cache: new Map()
|
||||
});
|
||||
this.gravatar = new Gravatar({config, request});
|
||||
}
|
||||
}
|
||||
|
@ -26,11 +26,12 @@ describe('lib/image: image size cache', function () {
|
||||
type: 'jpg'
|
||||
}));
|
||||
|
||||
const cachedImageSizeFromUrl = new CachedImageSizeFromUrl({logging: {
|
||||
error: () => {}
|
||||
}, imageSize: {
|
||||
getImageSizeFromUrl: sizeOfStub
|
||||
}});
|
||||
const cachedImageSizeFromUrl = new CachedImageSizeFromUrl({
|
||||
imageSize: {
|
||||
getImageSizeFromUrl: sizeOfStub
|
||||
},
|
||||
cache: new Map()
|
||||
});
|
||||
|
||||
imageSizeSpy = sizeOfStub;
|
||||
|
||||
@ -70,11 +71,12 @@ describe('lib/image: image size cache', function () {
|
||||
|
||||
sizeOfStub.returns(new Promise.reject('error'));
|
||||
|
||||
const cachedImageSizeFromUrl = new CachedImageSizeFromUrl({logging: {
|
||||
error: () => {}
|
||||
}, imageSize: {
|
||||
getImageSizeFromUrl: sizeOfStub
|
||||
}});
|
||||
const cachedImageSizeFromUrl = new CachedImageSizeFromUrl({
|
||||
imageSize: {
|
||||
getImageSizeFromUrl: sizeOfStub
|
||||
},
|
||||
cache: new Map()
|
||||
});
|
||||
|
||||
cachedImageSizeResult = Promise.resolve(cachedImageSizeFromUrl.getCachedImageSizeFromUrl(url));
|
||||
cachedImageSizeResult.then(function () {
|
||||
@ -89,7 +91,10 @@ describe('lib/image: image size cache', function () {
|
||||
});
|
||||
|
||||
it('should return null if url is undefined', function (done) {
|
||||
const cachedImageSizeFromUrl = new CachedImageSizeFromUrl({logging: {}, imageSize: {}});
|
||||
const cachedImageSizeFromUrl = new CachedImageSizeFromUrl({
|
||||
imageSize: {},
|
||||
cache: new Map()
|
||||
});
|
||||
const url = null;
|
||||
let result;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user