Skip to content

Commit

Permalink
feat(rr-cache): add hit ratio measurement
Browse files Browse the repository at this point in the history
  • Loading branch information
zhibirc committed Nov 30, 2023
1 parent 39547fc commit 5e52778
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
6 changes: 4 additions & 2 deletions rr-cache/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ type TStats = {
size: number;
capacity: number;
locked: boolean;
hitRatio: number;
};
interface IRRCache {
get stats(): TStats;
set locked(state: boolean);
set lock(state: boolean);
read: (key: any) => any;
add: (key: any, value: any) => void;
has: (key: any) => boolean;
Expand All @@ -27,8 +28,9 @@ declare class RRCache implements IRRCache {
size: number;
capacity: number;
locked: boolean;
hitRatio: number;
};
set locked(state: boolean);
set lock(state: boolean);
/**
* Read value stored in cache by assosiated key.
* @param {*} key - cache record's key
Expand Down
11 changes: 9 additions & 2 deletions rr-cache/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class RRCache {
#hits;
#misses;
#capacity;
#locked;
#keys;
Expand All @@ -8,6 +10,8 @@ class RRCache {
const { capacity } = options;
if (!Number.isInteger(capacity) || capacity <= 0)
throw new Error('invalid "capacity": positive integer expected');
this.#hits = 0;
this.#misses = 0;
this.#capacity = capacity;
this.#locked = false;
this.#keys = new Array(capacity);
Expand All @@ -18,10 +22,11 @@ class RRCache {
return {
size: this.#store.size,
capacity: this.#capacity,
locked: this.#locked
locked: this.#locked,
hitRatio: this.#hits / (this.#hits + this.#misses)
};
}
set locked(state) {
set lock(state) {
this.#locked = state;
}
/**
Expand All @@ -31,8 +36,10 @@ class RRCache {
*/
read(key) {
if (this.#store.has(key)) {
this.#hits += 1;
return this.#store.get(key).value;
}
this.#misses += 1;
return null;
}
add(key, value) {
Expand Down
15 changes: 12 additions & 3 deletions rr-cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ type TStats = {
capacity: number;
// represent if cache is locked currently
locked: boolean;
// measures how effective a cache is at fulfilling requests
hitRatio: number;
// @todo: may contain other props like cache misses, etc.
}

interface IRRCache {
// get cache statistics
get stats(): TStats;
// set lock state, if locked cache isn't growing
set locked(state: boolean);
set lock(state: boolean);
// read value from cache by its key
read: (key: any) => any;
// add value to cache by corresponding key
Expand All @@ -35,6 +37,8 @@ type TConfigOptions = {
}

class RRCache implements IRRCache {
#hits: number;
#misses: number;
#capacity: number;
#locked: boolean;
#keys: Array<any>;
Expand All @@ -46,6 +50,8 @@ class RRCache implements IRRCache {

if (!Number.isInteger(capacity) || capacity <= 0) throw new Error('invalid "capacity": positive integer expected');

this.#hits = 0;
this.#misses = 0;
this.#capacity = capacity;
this.#locked = false;
this.#keys = new Array(capacity);
Expand All @@ -57,11 +63,12 @@ class RRCache implements IRRCache {
return {
size: this.#store.size,
capacity: this.#capacity,
locked: this.#locked
locked: this.#locked,
hitRatio: this.#hits / (this.#hits + this.#misses)
};
}

set locked (state: boolean) {
set lock (state: boolean) {
this.#locked = state;
}

Expand All @@ -72,9 +79,11 @@ class RRCache implements IRRCache {
*/
read (key: any) {
if (this.#store.has(key)) {
this.#hits += 1;
return this.#store.get(key).value;
}

this.#misses += 1;
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion rr-cache/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
| Operation | Time complexity | Space complexity | Description |
|------------|-----------------|------------------|-------------------------------------------|
| `stats` | $O(1)$ | $O(1)$ | Get cache statistics. |
| `locked` | $O(1)$ | $O(1)$ | Set lock state. |
| `lock` | $O(1)$ | $O(1)$ | Set lock state. |
| `read` | $O(1)$ | $O(1)$ | Read value by its key. |
| `add` | $O(1)$ | $O(1)$ | Add value by corresponding key. |
| `has` | $O(1)$ | $O(1)$ | Check if there is a value by given key. |
Expand Down

0 comments on commit 5e52778

Please sign in to comment.