Skip to content

Commit

Permalink
feat(api): add memoryUsage to stats info
Browse files Browse the repository at this point in the history
  • Loading branch information
zhibirc committed Dec 12, 2023
1 parent 1bb5934 commit 2d27b08
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 22 deletions.
8 changes: 8 additions & 0 deletions fifo-cache/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ declare class FifoCache implements ICache {
capacity: number;
locked: boolean;
hitRatio: number;
memoryUsage: {
rss: number;
heapTotal: number;
heapUsed: number;
external: number;
arrayBuffers: number;
freeTotalRatio: number;
};
};
set lock(state: boolean);
/**
Expand Down
27 changes: 17 additions & 10 deletions fifo-cache/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os from 'node:os';
const Node = (data, next = null, prev = null) => ({ data, next, prev });
class FifoCache {
#hits;
Expand All @@ -17,7 +18,9 @@ class FifoCache {
this.#locked = false;
// store is a Doubly Linked List to support fast add (to tail) and delete (from head) records,
// as well as effective deletion
// points to first node (head) of the list
this.#head = null;
// points to last node (tail) of the list
this.#tail = null;
// struct for fast access to cache records, use as <key:node> lookup table
// to compensate O(N) bottleneck to search node in such list
Expand All @@ -28,7 +31,11 @@ class FifoCache {
size: this.#map.size,
capacity: this.#capacity,
locked: this.#locked,
hitRatio: this.#hits / (this.#hits + this.#misses)
hitRatio: this.#hits / (this.#hits + this.#misses),
memoryUsage: {
freeTotalRatio: Math.round(os.freemem() / os.totalmem() * 100) / 100,
...process.memoryUsage()
}
};
}
set lock(state) {
Expand All @@ -52,20 +59,20 @@ class FifoCache {
// check if cache capacity limit is reached
if (this.#map.size === this.#capacity) {
// evict head since we are out of capacity
const head = this.#head;
this.#head = head.next;
head.next = null;
this.#head.prev = null;
this.#map.delete(head.data.key);
this.#map.delete(this.#head.data.key);
this.#head = this.#head.next;
if (this.#head) {
this.#head.prev = null;
}
}
const node = Node({ key, value });
if (this.#map.size === 0) {
if (this.#head === null) {
this.#head = node;
}
else if (this.#map.size === 1) {
else if (this.#tail === null) {
this.#tail = node;
this.#head.next = this.#tail;
this.#tail.prev = this.#head;
this.#head.next = node;
node.prev = this.#head;
}
else {
this.#tail.next = node;
Expand Down
31 changes: 21 additions & 10 deletions fifo-cache/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import os from 'node:os';
import { ICache } from '../libs/types.js';

type TNode = {
data: {
key: any,
value: any
};
// reference to the next node
next: TNode | null;
// reference to the previous node
prev: TNode | null;
};

Expand Down Expand Up @@ -36,8 +39,12 @@ class FifoCache implements ICache {

// store is a Doubly Linked List to support fast add (to tail) and delete (from head) records,
// as well as effective deletion

// points to first node (head) of the list
this.#head = null;
// points to last node (tail) of the list
this.#tail = null;

// struct for fast access to cache records, use as <key:node> lookup table
// to compensate O(N) bottleneck to search node in such list
this.#map = new Map();
Expand All @@ -48,7 +55,11 @@ class FifoCache implements ICache {
size: this.#map.size,
capacity: this.#capacity,
locked: this.#locked,
hitRatio: this.#hits / (this.#hits + this.#misses)
hitRatio: this.#hits / (this.#hits + this.#misses),
memoryUsage: {
freeTotalRatio: Math.round(os.freemem() / os.totalmem() * 100) / 100,
...process.memoryUsage()
}
};
}

Expand Down Expand Up @@ -76,21 +87,21 @@ class FifoCache implements ICache {
// check if cache capacity limit is reached
if (this.#map.size === this.#capacity) {
// evict head since we are out of capacity
const head = this.#head;
this.#head = head.next;
head.next = null;
this.#head.prev = null;
this.#map.delete(head.data.key);
this.#map.delete(this.#head.data.key);
this.#head = this.#head.next;
if (this.#head) {
this.#head.prev = null;
}
}

const node: TNode = Node({key, value});

if (this.#map.size === 0) {
if (this.#head === null) {
this.#head = node;
} else if (this.#map.size === 1) {
} else if (this.#tail === null) {
this.#tail = node;
this.#head.next = this.#tail;
this.#tail.prev = this.#head;
this.#head.next = node;
node.prev = this.#head;
} else {
this.#tail.next = node;
node.prev = this.#tail;
Expand Down
3 changes: 3 additions & 0 deletions libs/filters/bloom/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## More information

[Mnemonist: Bloom Filter](https://yomguithereal.github.io/mnemonist/bloom-filter)
2 changes: 2 additions & 0 deletions libs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ type TStats = {
locked: boolean;
// measures how effective a cache is at fulfilling requests
hitRatio: number;
// object describing the memory usage of the process
memoryUsage: Record<string, number>;
// @todo: may contain other props like cache misses, etc.
}

Expand Down
8 changes: 8 additions & 0 deletions rr-cache/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ declare class RRCache implements ICache {
capacity: number;
locked: boolean;
hitRatio: number;
memoryUsage: {
rss: number;
heapTotal: number;
heapUsed: number;
external: number;
arrayBuffers: number;
freeTotalRatio: number;
};
};
set lock(state: boolean);
/**
Expand Down
7 changes: 6 additions & 1 deletion rr-cache/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os from 'node:os';
class RRCache {
#hits;
#misses;
Expand All @@ -23,7 +24,11 @@ class RRCache {
size: this.#store.size,
capacity: this.#capacity,
locked: this.#locked,
hitRatio: this.#hits / (this.#hits + this.#misses)
hitRatio: this.#hits / (this.#hits + this.#misses),
memoryUsage: {
freeTotalRatio: Math.round(os.freemem() / os.totalmem() * 100) / 100,
...process.memoryUsage()
}
};
}
set lock(state) {
Expand Down
7 changes: 6 additions & 1 deletion rr-cache/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os from 'node:os';
import { ICache } from '../libs/types.js';

class RRCache implements ICache {
Expand Down Expand Up @@ -28,7 +29,11 @@ class RRCache implements ICache {
size: this.#store.size,
capacity: this.#capacity,
locked: this.#locked,
hitRatio: this.#hits / (this.#hits + this.#misses)
hitRatio: this.#hits / (this.#hits + this.#misses),
memoryUsage: {
freeTotalRatio: Math.round(os.freemem() / os.totalmem() * 100) / 100,
...process.memoryUsage()
}
};
}

Expand Down

0 comments on commit 2d27b08

Please sign in to comment.