diff --git a/README.md b/README.md index 064a55a..85083a4 100644 --- a/README.md +++ b/README.md @@ -37,4 +37,11 @@ If in general it's usually true, this is very subjective if applied to implement ## General Theory -When the cache becomes full, a cache block or record/entry must be evicted to make room for a new block. The replacement policy determines which block to evict. \ No newline at end of file +When the cache becomes full, a cache block or record/entry must be evicted to make room for a new block. The replacement policy determines which block to evict. + +Various cache implementations can have slightly different public APIs, however most of them are very consistent in their core capabilities. In the list below there are typical extra features (or just their naming) which can be found in known realisations: + +| Member | Type | Description | +|--------|------|-------------------------------------------------------------------------------------| +|`setpop`|method|Sets a value for the given key, but besides that returns evicted object or old value.| +|`peek` |method|Retrieves the value associated with the given key, doesn't update access information.| \ No newline at end of file diff --git a/libs/decorators.ts b/libs/decorators/detect-one-hit-wonder.ts similarity index 68% rename from libs/decorators.ts rename to libs/decorators/detect-one-hit-wonder.ts index 0257838..b178052 100644 --- a/libs/decorators.ts +++ b/libs/decorators/detect-one-hit-wonder.ts @@ -1,8 +1,3 @@ -/** - * Cache method decorators. - * Designed to be used to extend cache capabilities. - */ - /** * Detect one-hit-wonder objects. */ @@ -31,17 +26,5 @@ const detectOneHitWonder = (() => { } })(); -function log() { - return function (originalMethod: any, _context: ClassMethodDecoratorContext) { - function replacementMethod(this: any, key: any, value: any) { - // @todo: implement - } - - return replacementMethod; - }; -} - -export { - detectOneHitWonder -}; \ No newline at end of file +export default detectOneHitWonder; \ No newline at end of file diff --git a/libs/decorators/log.ts b/libs/decorators/log.ts new file mode 100644 index 0000000..f5bcc31 --- /dev/null +++ b/libs/decorators/log.ts @@ -0,0 +1,15 @@ +/** + * Add logging for cache operations. + */ +function log() { + return function (originalMethod: any, _context: ClassMethodDecoratorContext) { + function replacementMethod(this: any, key: any, value: any) { + // @todo: implement + } + + return replacementMethod; + }; +} + + +export default log; \ No newline at end of file diff --git a/libs/types.d.ts b/libs/types.d.ts deleted file mode 100644 index 4cbf3a9..0000000 --- a/libs/types.d.ts +++ /dev/null @@ -1,22 +0,0 @@ -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 }; diff --git a/libs/types.js b/libs/types.js deleted file mode 100644 index cb0ff5c..0000000 --- a/libs/types.js +++ /dev/null @@ -1 +0,0 @@ -export {}; diff --git a/rr-cache/index.ts b/rr-cache/index.ts index d86ee26..a00a908 100644 --- a/rr-cache/index.ts +++ b/rr-cache/index.ts @@ -1,4 +1,4 @@ -import { TConfigOptions, ICache } from '../libs/types.js'; +import { ICache } from '../libs/types.js'; class RRCache implements ICache { #hits: number; @@ -9,9 +9,7 @@ class RRCache implements ICache { #freeSlots: Array; #store; - constructor ( options: TConfigOptions ) { - const { capacity } = options; - + constructor ( capacity: number ) { if (!Number.isInteger(capacity) || capacity <= 0) throw new Error('invalid "capacity": positive integer expected'); this.#hits = 0; diff --git a/tsconfig.json b/tsconfig.json index a85b0f0..684a66b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -14,6 +14,7 @@ "*/index.ts" ], "exclude": [ - "node_modules" + "node_modules", + "libs" ] }