Skip to content

Commit

Permalink
feat(libs): add decorator for one-hit-wonder objects detection
Browse files Browse the repository at this point in the history
Designed as an optional extention for cache capabilities.
  • Loading branch information
zhibirc committed Dec 3, 2023
1 parent 5e52778 commit 1bb504f
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 62 deletions.
47 changes: 47 additions & 0 deletions libs/decorators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Cache method decorators.
* Designed to be used to extend cache capabilities.
*/

/**
* Detect one-hit-wonder objects.
*/
const detectOneHitWonder = (() => {
const hitCount = new Map();

return (threshold: number = 1) => {
return function (originalMethod: any, _context: ClassMethodDecoratorContext) {
function replacementMethod(this: any, key: any, value?: any) {
let n = hitCount.get(key) ?? 0;

hitCount.set(key, ++n);

switch (originalMethod.name) {
case 'read':
return originalMethod.call(this, key);
case 'add':
if (n > threshold) {
return originalMethod.call(this, key, value);
}
}
}

return replacementMethod;
};
}
})();

function log() {
return function (originalMethod: any, _context: ClassMethodDecoratorContext) {
function replacementMethod(this: any, key: any, value: any) {
// @todo: implement
}

return replacementMethod;
};
}


export {
detectOneHitWonder
};
22 changes: 22 additions & 0 deletions libs/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
type TStats = {
size: number;
capacity: number;
locked: boolean;
hitRatio: number;
};
type TConfigOptions = {
/**
* Capacity means how many items can be stored at the same time in cache.
*/
capacity: number;
};
interface ICache {
get stats(): TStats;
set lock(state: boolean);
read: (key: any) => any;
add: (key: any, value: any) => void;
has: (key: any) => boolean;
remove: (key: any) => void;
clear: () => void;
}
export { TStats, TConfigOptions, ICache };
1 change: 1 addition & 0 deletions libs/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
42 changes: 42 additions & 0 deletions libs/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
type TStats = {
// represent number of cache entries
size: number;
// represent value of cache capacity
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.
}

type TConfigOptions = {
/**
* Capacity means how many items can be stored at the same time in cache.
*/
capacity: number;
}

interface ICache {
// get cache statistics
get stats(): TStats;
// set lock state, if locked cache isn't growing
set lock(state: boolean);
// read value from cache by its key
read: (key: any) => any;
// add value to cache by corresponding key
add: (key: any, value: any) => void;
// check if cache contains value by given key
has: (key: any) => boolean;
// remove value from cache by associated key
remove: (key: any) => void;
// clear cache by removing all stored data and release all auxiliary resources
clear: () => void;
}


export {
TStats,
TConfigOptions,
ICache
};
26 changes: 2 additions & 24 deletions rr-cache/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
type TStats = {
size: number;
capacity: number;
locked: boolean;
hitRatio: number;
};
interface IRRCache {
get stats(): TStats;
set lock(state: boolean);
read: (key: any) => any;
add: (key: any, value: any) => void;
has: (key: any) => boolean;
remove: (key: any) => void;
clear: () => void;
}
type TConfigOptions = {
/**
* Capacity means how many items can be stored at the same time in cache.
* For RR cache, by definition, capacity is a required restriction,
* without it becomes almost meaningless, so this option is mandatory.
*/
capacity: number;
};
declare class RRCache implements IRRCache {
import { TConfigOptions, ICache } from '../libs/types.js';
declare class RRCache implements ICache {
#private;
constructor(options: TConfigOptions);
get stats(): {
Expand Down
40 changes: 2 additions & 38 deletions rr-cache/index.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,6 @@
type TStats = {
// represent number of cache entries
size: number;
// represent value of cache capacity
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 lock(state: boolean);
// read value from cache by its key
read: (key: any) => any;
// add value to cache by corresponding key
add: (key: any, value: any) => void;
// check if cache contains value by given key
has: (key: any) => boolean;
// remove value from cache by associated key
remove: (key: any) => void;
// clear cache by removing all stored data and release all auxiliary resources
clear: () => void;
}

type TConfigOptions = {
/**
* Capacity means how many items can be stored at the same time in cache.
* For RR cache, by definition, capacity is a required restriction,
* without it becomes almost meaningless, so this option is mandatory.
*/
capacity: number;
}
import { TConfigOptions, ICache } from '../libs/types.js';

class RRCache implements IRRCache {
class RRCache implements ICache {
#hits: number;
#misses: number;
#capacity: number;
Expand Down

0 comments on commit 1bb504f

Please sign in to comment.