@spoosh/plugin-gc 0.1.5 → 0.1.7

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.
package/dist/index.js CHANGED
@@ -32,6 +32,9 @@ function runGarbageCollection(stateManager, options) {
32
32
  const entries = stateManager.getAllCacheEntries();
33
33
  if (maxAge !== void 0) {
34
34
  for (const { key, entry } of entries) {
35
+ if (stateManager.getSubscribersCount(key) > 0) {
36
+ continue;
37
+ }
35
38
  const age = now - entry.state.timestamp;
36
39
  if (age > maxAge) {
37
40
  stateManager.deleteCache(key);
@@ -41,12 +44,15 @@ function runGarbageCollection(stateManager, options) {
41
44
  }
42
45
  if (maxEntries !== void 0) {
43
46
  const currentEntries = stateManager.getAllCacheEntries();
47
+ const gcCandidates = currentEntries.filter(
48
+ ({ key }) => stateManager.getSubscribersCount(key) === 0
49
+ );
44
50
  const excess = currentEntries.length - maxEntries;
45
51
  if (excess > 0) {
46
- const sorted = currentEntries.sort(
52
+ const sorted = gcCandidates.sort(
47
53
  (a, b) => a.entry.state.timestamp - b.entry.state.timestamp
48
54
  );
49
- for (let i = 0; i < excess; i++) {
55
+ for (let i = 0; i < Math.min(excess, sorted.length); i++) {
50
56
  const entry = sorted[i];
51
57
  if (entry) {
52
58
  stateManager.deleteCache(entry.key);
package/dist/index.mjs CHANGED
@@ -6,6 +6,9 @@ function runGarbageCollection(stateManager, options) {
6
6
  const entries = stateManager.getAllCacheEntries();
7
7
  if (maxAge !== void 0) {
8
8
  for (const { key, entry } of entries) {
9
+ if (stateManager.getSubscribersCount(key) > 0) {
10
+ continue;
11
+ }
9
12
  const age = now - entry.state.timestamp;
10
13
  if (age > maxAge) {
11
14
  stateManager.deleteCache(key);
@@ -15,12 +18,15 @@ function runGarbageCollection(stateManager, options) {
15
18
  }
16
19
  if (maxEntries !== void 0) {
17
20
  const currentEntries = stateManager.getAllCacheEntries();
21
+ const gcCandidates = currentEntries.filter(
22
+ ({ key }) => stateManager.getSubscribersCount(key) === 0
23
+ );
18
24
  const excess = currentEntries.length - maxEntries;
19
25
  if (excess > 0) {
20
- const sorted = currentEntries.sort(
26
+ const sorted = gcCandidates.sort(
21
27
  (a, b) => a.entry.state.timestamp - b.entry.state.timestamp
22
28
  );
23
- for (let i = 0; i < excess; i++) {
29
+ for (let i = 0; i < Math.min(excess, sorted.length); i++) {
24
30
  const entry = sorted[i];
25
31
  if (entry) {
26
32
  stateManager.deleteCache(entry.key);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spoosh/plugin-gc",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "description": "Garbage collection plugin for Spoosh cache with time and size-based cleanup",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -34,11 +34,11 @@
34
34
  }
35
35
  },
36
36
  "peerDependencies": {
37
- "@spoosh/core": ">=0.4.0"
37
+ "@spoosh/core": ">=0.11.1"
38
38
  },
39
39
  "devDependencies": {
40
- "@spoosh/core": "0.10.0",
41
- "@spoosh/test-utils": "0.1.5"
40
+ "@spoosh/core": "0.11.1",
41
+ "@spoosh/test-utils": "0.1.6"
42
42
  },
43
43
  "scripts": {
44
44
  "dev": "tsup --watch",