-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(libs): add decorator for one-hit-wonder objects detection
Designed as an optional extention for cache capabilities.
- Loading branch information
Showing
6 changed files
with
116 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters