Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

feat(server-cache): estimatedSize #23

Merged
merged 6 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions __tests__/intl/server-cache.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import lolex from 'lolex';
describe('server-cache', () => {
let set;
let get;
let getEstimatedSize;
let clock;
let intervalUnref;

Expand All @@ -37,9 +38,11 @@ describe('server-cache', () => {
};

jest.resetModules();
// eslint-disable-next-line global-require
const serverCache = require('../../src/intl/server-cache');
set = serverCache.set;
get = serverCache.get;
getEstimatedSize = serverCache.getEstimatedSize;
}

beforeEach(() => {
Expand Down Expand Up @@ -91,6 +94,16 @@ describe('server-cache', () => {
});
});

describe('getEstimatedSize', () => {
it('returns memory allocation of cache', () => {
delete process.env.NODE_ENV;
setupCache();
expect(getEstimatedSize()).toEqual(0);
set('add entry to cache', 'entry');
expect(getEstimatedSize()).toBe(82);
});
});

describe('cleanup', () => {
it('leaves non-expired values in cache', () => {
setupCache();
Expand Down
27 changes: 27 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@
]
},
"dependencies": {
"holocron": "^1.0.0",
"immutable": "^4.0.0-rc.12",
"js-cookie": "^2.2.0",
"holocron": "^1.0.0-9",
"lean-intl": "^4.2.2",
"object-sizeof": "^1.6.1",
"reselect": "^4.0.0"
},
"devDependencies": {
Expand Down
5 changes: 5 additions & 0 deletions src/intl/server-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* under the License.
*/

import sizeOf from 'object-sizeof';

const cache = new Map();

const CLEANUP_INTERVAL = 10 * 60 * 1e3; // intended to be 10 minutes
Expand All @@ -35,9 +37,12 @@ function get(url) {
return cached.data;
}

const getEstimatedSize = () => sizeOf(Object.fromEntries(cache));

export {
set,
get,
getEstimatedSize,
};

function cleanup() {
Expand Down