mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-17 23:44:39 -05:00
Added basic memory cache storage implementation
refs https://github.com/TryGhost/Toolbox/issues/364 - This is groundwork to substitute in memory caches we use across the codebase. The first candidate would be settings cache. The interface of the memory cache was kept to the minimum. The "keys" method is a somewhat acceptable alternative to the "getAll" method used in the codebase right now. - The next iteration over this would be adding async methods are alternative key/value storage methanisms like Redis.
This commit is contained in:
parent
af0014917b
commit
ed79d3e9b3
4 changed files with 94 additions and 0 deletions
12
ghost/core/core/server/adapters/cache/Base.js
vendored
Normal file
12
ghost/core/core/server/adapters/cache/Base.js
vendored
Normal file
|
@ -0,0 +1,12 @@
|
|||
class Cache {
|
||||
constructor() {
|
||||
Object.defineProperty(this, 'requiredFns', {
|
||||
// NOTE: "keys" method is only here to provide smooth migration from deprecated "getAll" method
|
||||
// once use of "getAll" is eradicated, can also remove the "keys" method form the interface
|
||||
value: ['get', 'set', 'reset', 'keys'],
|
||||
writable: false
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Cache;
|
36
ghost/core/core/server/adapters/cache/Memory.js
vendored
Normal file
36
ghost/core/core/server/adapters/cache/Memory.js
vendored
Normal file
|
@ -0,0 +1,36 @@
|
|||
const Base = require('./Base');
|
||||
|
||||
class MemoryCache extends Base {
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this._data = {};
|
||||
}
|
||||
|
||||
get(key) {
|
||||
return this._data[key];
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {String} key
|
||||
* @param {*} value
|
||||
*/
|
||||
set(key, value) {
|
||||
this._data[key] = value;
|
||||
}
|
||||
|
||||
reset() {
|
||||
this._data = {};
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to assist "getAll" type of operations
|
||||
* @returns {Array<String>} all keys present in the cache
|
||||
*/
|
||||
keys() {
|
||||
return Object.keys(this._data);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MemoryCache;
|
17
ghost/core/core/server/adapters/cache/index.js
vendored
Normal file
17
ghost/core/core/server/adapters/cache/index.js
vendored
Normal file
|
@ -0,0 +1,17 @@
|
|||
const adapterManager = require('../../services/adapter-manager');
|
||||
|
||||
/**
|
||||
* @param {'settings'|'theme'|'urls'} [feature] - name for the "feature" to enable through adapter, e.g.: settings cache
|
||||
* @returns {Object} cache adapter instance
|
||||
*/
|
||||
function getCache(feature) {
|
||||
let adapterName = 'cache';
|
||||
|
||||
if (feature) {
|
||||
adapterName += `:${feature}`;
|
||||
}
|
||||
|
||||
return adapterManager.getAdapter(adapterName);
|
||||
}
|
||||
|
||||
module.exports.getCache = getCache;
|
29
ghost/core/test/unit/server/adapters/cache/Memory.test.js
vendored
Normal file
29
ghost/core/test/unit/server/adapters/cache/Memory.test.js
vendored
Normal file
|
@ -0,0 +1,29 @@
|
|||
const assert = require('assert');
|
||||
|
||||
const MemoryCache = require('../../../../../core/server/adapters/cache/Memory');
|
||||
|
||||
describe('In Memory Cache Adapter', function () {
|
||||
let memoryCache;
|
||||
|
||||
beforeEach(function () {
|
||||
memoryCache = new MemoryCache();
|
||||
});
|
||||
|
||||
it('stores a value through set method', function () {
|
||||
memoryCache.set('a', 'Alabama');
|
||||
|
||||
assert.deepEqual(['a'], memoryCache.keys());
|
||||
assert.equal('Alabama', memoryCache.get('a'));
|
||||
});
|
||||
|
||||
it('flushes the storage', function () {
|
||||
memoryCache.set('t', 'Texas');
|
||||
|
||||
assert.equal('Texas', memoryCache.get('t'));
|
||||
|
||||
memoryCache.reset();
|
||||
|
||||
assert.deepEqual([], memoryCache.keys());
|
||||
assert.equal(undefined, memoryCache.get('t'));
|
||||
});
|
||||
});
|
Loading…
Add table
Reference in a new issue