@prezly/theme-kit-core 8.0.0 → 8.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,39 +3,30 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.RECORDS_LIMIT = void 0;
6
7
  exports.createSharedMemoryCache = createSharedMemoryCache;
7
8
  /* eslint-disable @typescript-eslint/no-use-before-define */
8
9
 
9
- var RECORDS_LIMIT = 10000;
10
+ var RECORDS_LIMIT = exports.RECORDS_LIMIT = 10000;
10
11
  var GC_PROBABILITY = 1 / 100;
11
12
  var CACHE = new Map();
12
13
  function createSharedMemoryCache() {
13
14
  var prefix = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
14
- function gc() {
15
- Array.from(CACHE.entries()).sort((_ref, _ref2) => {
16
- var [, a] = _ref;
17
- var [, b] = _ref2;
18
- return cmp(a.accessed, b.accessed);
19
- }).slice(RECORDS_LIMIT).forEach(_ref3 => {
20
- var [key] = _ref3;
21
- CACHE.delete("".concat(prefix).concat(key));
22
- });
23
- }
24
15
  return {
25
16
  get(key, latestVersion) {
26
- var entry = CACHE.get(key);
17
+ var entry = CACHE.get("".concat(prefix).concat(key));
27
18
  if (!entry) {
28
19
  return undefined;
29
20
  }
30
21
  if (entry.version < latestVersion) {
31
- CACHE.delete(key);
22
+ CACHE.delete("".concat(prefix).concat(key));
32
23
  return undefined;
33
24
  }
34
25
  var {
35
26
  value,
36
27
  version
37
28
  } = entry;
38
- CACHE.set(key, {
29
+ CACHE.set("".concat(prefix).concat(key), {
39
30
  value,
40
31
  version,
41
32
  accessed: new Date().getTime()
@@ -48,7 +39,7 @@ function createSharedMemoryCache() {
48
39
  version,
49
40
  accessed: new Date().getTime()
50
41
  };
51
- CACHE.set(key, entry);
42
+ CACHE.set("".concat(prefix).concat(key), entry);
52
43
  if (CACHE.size > RECORDS_LIMIT && Math.random() < GC_PROBABILITY) {
53
44
  gc();
54
45
  }
@@ -58,6 +49,16 @@ function createSharedMemoryCache() {
58
49
  }
59
50
  };
60
51
  }
52
+ function gc() {
53
+ Array.from(CACHE.entries()).sort((_ref, _ref2) => {
54
+ var [, a] = _ref;
55
+ var [, b] = _ref2;
56
+ return -cmp(a.accessed, b.accessed);
57
+ }).slice(RECORDS_LIMIT).forEach(_ref3 => {
58
+ var [key] = _ref3;
59
+ CACHE.delete(key);
60
+ });
61
+ }
61
62
  function cmp(a, b) {
62
63
  if (a === b) return 0;
63
64
  return a < b ? -1 : 1;
@@ -1,2 +1,3 @@
1
1
  import type { Cache } from './type';
2
+ export declare const RECORDS_LIMIT = 10000;
2
3
  export declare function createSharedMemoryCache(prefix?: string): Cache;
@@ -1,35 +1,25 @@
1
1
  /* eslint-disable @typescript-eslint/no-use-before-define */
2
2
 
3
- var RECORDS_LIMIT = 10000;
3
+ export var RECORDS_LIMIT = 10000;
4
4
  var GC_PROBABILITY = 1 / 100;
5
5
  var CACHE = new Map();
6
6
  export function createSharedMemoryCache() {
7
7
  var prefix = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
8
- function gc() {
9
- Array.from(CACHE.entries()).sort((_ref, _ref2) => {
10
- var [, a] = _ref;
11
- var [, b] = _ref2;
12
- return cmp(a.accessed, b.accessed);
13
- }).slice(RECORDS_LIMIT).forEach(_ref3 => {
14
- var [key] = _ref3;
15
- CACHE.delete("".concat(prefix).concat(key));
16
- });
17
- }
18
8
  return {
19
9
  get(key, latestVersion) {
20
- var entry = CACHE.get(key);
10
+ var entry = CACHE.get("".concat(prefix).concat(key));
21
11
  if (!entry) {
22
12
  return undefined;
23
13
  }
24
14
  if (entry.version < latestVersion) {
25
- CACHE.delete(key);
15
+ CACHE.delete("".concat(prefix).concat(key));
26
16
  return undefined;
27
17
  }
28
18
  var {
29
19
  value,
30
20
  version
31
21
  } = entry;
32
- CACHE.set(key, {
22
+ CACHE.set("".concat(prefix).concat(key), {
33
23
  value,
34
24
  version,
35
25
  accessed: new Date().getTime()
@@ -42,7 +32,7 @@ export function createSharedMemoryCache() {
42
32
  version,
43
33
  accessed: new Date().getTime()
44
34
  };
45
- CACHE.set(key, entry);
35
+ CACHE.set("".concat(prefix).concat(key), entry);
46
36
  if (CACHE.size > RECORDS_LIMIT && Math.random() < GC_PROBABILITY) {
47
37
  gc();
48
38
  }
@@ -52,6 +42,16 @@ export function createSharedMemoryCache() {
52
42
  }
53
43
  };
54
44
  }
45
+ function gc() {
46
+ Array.from(CACHE.entries()).sort((_ref, _ref2) => {
47
+ var [, a] = _ref;
48
+ var [, b] = _ref2;
49
+ return -cmp(a.accessed, b.accessed);
50
+ }).slice(RECORDS_LIMIT).forEach(_ref3 => {
51
+ var [key] = _ref3;
52
+ CACHE.delete(key);
53
+ });
54
+ }
55
55
  function cmp(a, b) {
56
56
  if (a === b) return 0;
57
57
  return a < b ? -1 : 1;
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+
3
+ var _memory = require("./memory.cjs");
4
+ describe('createSharedMemoryCache', () => {
5
+ it('should write and read keys', () => {
6
+ var cache = (0, _memory.createSharedMemoryCache)();
7
+ expect(cache.get('hello', 0)).toBeUndefined();
8
+ cache.set('hello', 'world', 0);
9
+ expect(cache.get('hello', 0)).toBe('world');
10
+ });
11
+ it('should delete accessed records if the current version is higher', () => {
12
+ var cache = (0, _memory.createSharedMemoryCache)();
13
+ cache.set('hello', 'world', 0);
14
+ expect(cache.get('hello', 0)).toBe('world');
15
+ expect(cache.get('hello', 1)).toBeUndefined();
16
+ expect(cache.get('hello', 0)).toBeUndefined();
17
+ });
18
+ it('should separate datasets by namespace', () => {
19
+ var cache = (0, _memory.createSharedMemoryCache)();
20
+ var a = cache.namespace('a:');
21
+ var b = cache.namespace('b:');
22
+ a.set('hello', 'world', 0);
23
+ b.set('hello', 'universe', 0);
24
+ expect(cache.get('hello', 0)).toBeUndefined();
25
+ expect(a.get('hello', 0)).toBe('world');
26
+ expect(b.get('hello', 0)).toBe('universe');
27
+ });
28
+ it('should garbage-collect old cache records when threshold is reached (with a chance of 1/100)', () => {
29
+ var cache = (0, _memory.createSharedMemoryCache)();
30
+ for (var i = 0; i < _memory.RECORDS_LIMIT; i += 1) {
31
+ cache.set("record-".concat(i), "value-".concat(i), 0);
32
+ }
33
+ for (var _i = 0; _i < _memory.RECORDS_LIMIT; _i += 1) {
34
+ expect(cache.get("record-".concat(_i), 0)).toBe("value-".concat(_i));
35
+ }
36
+
37
+ // Write 10% more records, 10 times to trigger CG
38
+ for (var repeat = 0; repeat < 10; repeat += 1) {
39
+ for (var _i2 = _memory.RECORDS_LIMIT; _i2 < _memory.RECORDS_LIMIT * 1.1; _i2 += 1) {
40
+ cache.set("record-".concat(_i2), "value-".concat(_i2), 0);
41
+ }
42
+ }
43
+
44
+ // the oldest part of the cache should be removed already
45
+ for (var _i3 = 0; _i3 < _memory.RECORDS_LIMIT * 0.09; _i3 += 1) {
46
+ expect(cache.get("record-".concat(_i3), 0)).toBeUndefined();
47
+ }
48
+
49
+ // and the rest kept
50
+ for (var _i4 = _memory.RECORDS_LIMIT * 0.11; _i4 < _memory.RECORDS_LIMIT * 1.1; _i4 += 1) {
51
+ expect(cache.get("record-".concat(_i4), 0)).toBe("value-".concat(_i4));
52
+ }
53
+ });
54
+ });
@@ -0,0 +1,52 @@
1
+ import { createSharedMemoryCache, RECORDS_LIMIT } from "./memory.mjs";
2
+ describe('createSharedMemoryCache', () => {
3
+ it('should write and read keys', () => {
4
+ var cache = createSharedMemoryCache();
5
+ expect(cache.get('hello', 0)).toBeUndefined();
6
+ cache.set('hello', 'world', 0);
7
+ expect(cache.get('hello', 0)).toBe('world');
8
+ });
9
+ it('should delete accessed records if the current version is higher', () => {
10
+ var cache = createSharedMemoryCache();
11
+ cache.set('hello', 'world', 0);
12
+ expect(cache.get('hello', 0)).toBe('world');
13
+ expect(cache.get('hello', 1)).toBeUndefined();
14
+ expect(cache.get('hello', 0)).toBeUndefined();
15
+ });
16
+ it('should separate datasets by namespace', () => {
17
+ var cache = createSharedMemoryCache();
18
+ var a = cache.namespace('a:');
19
+ var b = cache.namespace('b:');
20
+ a.set('hello', 'world', 0);
21
+ b.set('hello', 'universe', 0);
22
+ expect(cache.get('hello', 0)).toBeUndefined();
23
+ expect(a.get('hello', 0)).toBe('world');
24
+ expect(b.get('hello', 0)).toBe('universe');
25
+ });
26
+ it('should garbage-collect old cache records when threshold is reached (with a chance of 1/100)', () => {
27
+ var cache = createSharedMemoryCache();
28
+ for (var i = 0; i < RECORDS_LIMIT; i += 1) {
29
+ cache.set("record-".concat(i), "value-".concat(i), 0);
30
+ }
31
+ for (var _i = 0; _i < RECORDS_LIMIT; _i += 1) {
32
+ expect(cache.get("record-".concat(_i), 0)).toBe("value-".concat(_i));
33
+ }
34
+
35
+ // Write 10% more records, 10 times to trigger CG
36
+ for (var repeat = 0; repeat < 10; repeat += 1) {
37
+ for (var _i2 = RECORDS_LIMIT; _i2 < RECORDS_LIMIT * 1.1; _i2 += 1) {
38
+ cache.set("record-".concat(_i2), "value-".concat(_i2), 0);
39
+ }
40
+ }
41
+
42
+ // the oldest part of the cache should be removed already
43
+ for (var _i3 = 0; _i3 < RECORDS_LIMIT * 0.09; _i3 += 1) {
44
+ expect(cache.get("record-".concat(_i3), 0)).toBeUndefined();
45
+ }
46
+
47
+ // and the rest kept
48
+ for (var _i4 = RECORDS_LIMIT * 0.11; _i4 < RECORDS_LIMIT * 1.1; _i4 += 1) {
49
+ expect(cache.get("record-".concat(_i4), 0)).toBe("value-".concat(_i4));
50
+ }
51
+ });
52
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prezly/theme-kit-core",
3
- "version": "8.0.0",
3
+ "version": "8.0.1",
4
4
  "description": "Data layer and utility library for developing Prezly themes with JavaScript",
5
5
  "main": "build/index.mjs",
6
6
  "types": "build/index.d.ts",
@@ -70,5 +70,5 @@
70
70
  "publishConfig": {
71
71
  "access": "public"
72
72
  },
73
- "gitHead": "2561eeb2181adf7e01783ac5bddaafab70a04b44"
73
+ "gitHead": "3068afdd286035a4d0cf106ca25070114a779b25"
74
74
  }