0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00

Swapped image size "Map" cache for adapter cache

refs https://github.com/TryGhost/Toolbox/issues/364

- The "new Map()" cache was a "hidden cache" that did not follow any specific pattern. Following the cache adapter pattern here makes it possible swapping out the cache for alternative implementations - e.g. Redis storage
This commit is contained in:
Naz 2022-08-15 15:44:10 +02:00
parent 34b4421452
commit e4d02657ee
5 changed files with 28 additions and 6 deletions

View file

@ -0,0 +1,7 @@
const Memory = require('./Memory');
class ImageSizesCacheSyncInMemory extends Memory {
}
module.exports = ImageSizesCacheSyncInMemory;

View file

@ -4,12 +4,12 @@ const Gravatar = require('./gravatar');
const ImageSize = require('./image-size');
class ImageUtils {
constructor({config, urlUtils, settingsCache, storageUtils, storage, validator, request}) {
constructor({config, urlUtils, settingsCache, storageUtils, storage, validator, request, cacheStore}) {
this.blogIcon = new BlogIcon({config, urlUtils, settingsCache, storageUtils});
this.imageSize = new ImageSize({config, storage, storageUtils, validator, urlUtils, request});
this.cachedImageSizeFromUrl = new CachedImageSizeFromUrl({
getImageSizeFromUrl: this.imageSize.getImageSizeFromUrl.bind(this.imageSize),
cache: new Map()
cache: cacheStore
});
this.gravatar = new Gravatar({config, request});
}

View file

@ -7,4 +7,17 @@ const config = require('../../../shared/config');
const settingsCache = require('../../../shared/settings-cache');
const ImageUtils = require('./image-utils');
module.exports = new ImageUtils({config, urlUtils, settingsCache, storageUtils, storage, validator, request});
const adapterManager = require('../../services/adapter-manager');
const cacheStore = adapterManager.getAdapter('cache:imageSizes');
module.exports = new ImageUtils({
config,
urlUtils,
settingsCache,
storageUtils,
storage,
validator,
request,
cacheStore
});

View file

@ -27,7 +27,9 @@
"cache": {
"active": "Memory",
"settings": "SettingsCacheSyncInMemory",
"SettingsCacheSyncInMemory": {}
"SettingsCacheSyncInMemory": {},
"imageSizes": "ImageSizesCacheSyncInMemory",
"ImageSizesCacheSyncInMemory": {}
}
},
"storage": {

View file

@ -2,7 +2,7 @@ const errors = require('@tryghost/errors');
const should = require('should');
const sinon = require('sinon');
const CachedImageSizeFromUrl = require('../../../../../core/server/lib/image/cached-image-size-from-url');
const InMemoryCache = require('../../../../../core/server/adapters/cache/Memory');
const InMemoryCache = require('../../../../../core/server/adapters/cache/ImageSizesCacheSyncInMemory');
describe('lib/image: image size cache', function () {
let sizeOfStub;
@ -64,7 +64,7 @@ describe('lib/image: image size cache', function () {
sizeOfStub.rejects('error');
const cacheStore = new InMemoryCache()
const cacheStore = new InMemoryCache();
const cachedImageSizeFromUrl = new CachedImageSizeFromUrl({
getImageSizeFromUrl: sizeOfStub,
cache: cacheStore