-
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.
add decorator / fix docs/readme / add license
- Loading branch information
Showing
15 changed files
with
131 additions
and
33 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,7 @@ | ||
Copyright 2024 Vinicius Alves | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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
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 @@ | ||
import { CacheLike } from '../types' | ||
|
||
export function cacheDecorator | ||
<T extends (...args: any[]) => Promise<any> | any> | ||
(fn: T, cache: CacheLike): T { | ||
return async function (...args: Parameters<T>): Promise<ReturnType<T>> { | ||
const key = args.join('.') | ||
|
||
const cachedResult = cache.get(key) | ||
if (cachedResult) { | ||
return cachedResult as ReturnType<T> | ||
} | ||
|
||
const result = await fn(...args) | ||
|
||
if (cache) { | ||
cache.set(key, result) | ||
} | ||
|
||
return result | ||
} as T | ||
} |
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
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
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
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
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 |
---|---|---|
@@ -1,23 +1,40 @@ | ||
export interface CacheParams { | ||
/**Defines the max quantity of keys that will be stored, in some caches this param is required, default is `undefined` */ | ||
/** | ||
* Defines the maximum number of keys that will be stored. | ||
* In some caches, this parameter is required; the default is `undefined`. | ||
*/ | ||
maxsize?: number | ||
/**If true, will create a **deep copy** of all data before store, default is `false` */ | ||
|
||
/** | ||
* If true, a **deep copy** of all data will be created before storing. | ||
* The default is `false`. | ||
*/ | ||
useClones?: boolean | ||
} | ||
|
||
export interface TTLParams extends CacheParams { | ||
/** It's the **Time to Live** of cache in **ms**, if you dont provide `ttl` param in `set` function, this ttl will be used, if not provided, the cache will not delete keys */ | ||
/** | ||
* The **Time to Live** of the cache in **ms**. | ||
* If you don't provide the `ttl` parameter in the `set` function, this `ttl` will be used. | ||
* If not provided, the cache will not delete keys. | ||
*/ | ||
ttl?: number | ||
} | ||
|
||
export interface RRParams extends CacheParams { | ||
/**Random logic for deletion of keys in cache, default is `Math.floor(Math.random * length)` */ | ||
/** | ||
* Random logic for the deletion of keys in the cache. | ||
* The default is `Math.floor(Math.random() * length)`. | ||
*/ | ||
randomLogic?: (length: number) => number | ||
} | ||
|
||
export type ParamsLike = CacheParams | TTLParams | RRParams | ||
|
||
export interface CachePoolParams { | ||
/**Defines the max quantity of caches that will be stored, this param is optional, default is `undefined` */ | ||
/** | ||
* Defines the maximum number of caches that will be stored. | ||
* This parameter is optional; the default is `undefined`. | ||
*/ | ||
maxsize?: number | ||
} |
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 |
---|---|---|
@@ -1,29 +1,30 @@ | ||
export class SizeError extends Error { | ||
constructor(){ | ||
super('Tried to set key in cache or pool that is full') | ||
constructor() { | ||
super('Attempted to set a key in a cache or pool that is full.') | ||
} | ||
} | ||
|
||
export class MissingSize extends Error { | ||
constructor(){ | ||
super('The maxsize param need to be passed in this cache type') | ||
constructor() { | ||
super('The `maxsize` parameter must be provided for this cache type.') | ||
} | ||
} | ||
|
||
export class CacheNotExists extends Error { | ||
constructor(){ | ||
super('Tried to access cache that not exists in a pool') | ||
constructor() { | ||
super('Attempted to access a cache that does not exist in the pool.') | ||
} | ||
} | ||
|
||
export class CacheTypeNotExists extends Error { | ||
constructor(){ | ||
super('Cache type passed to \'createCache\' not exists') | ||
constructor() { | ||
super('The cache type passed to `createCache` does not exist.') | ||
} | ||
} | ||
|
||
export class AlreadyExists extends Error { | ||
constructor(){ | ||
super('This cache already exists') | ||
constructor() { | ||
super('This cache already exists.') | ||
} | ||
} | ||
|
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,39 @@ | ||
import { Cache, cacheDecorator, CacheLike } from '../src' | ||
|
||
describe('Decorator', () => { | ||
let fn: (...args: number[]) => number | ||
let cache: CacheLike | ||
let cachedFunc: (...args: number[]) => number | ||
|
||
beforeEach(() => { | ||
fn = (...args) => args.reduce((p, c) => p + c) | ||
cache = new Cache({maxsize: 10}) | ||
cachedFunc = cacheDecorator(fn, cache) | ||
}) | ||
|
||
test('should get a cached value', async () => { | ||
//store a value | ||
await cachedFunc(1, 2, 3) | ||
|
||
//get a value | ||
expect( | ||
cache['1.2.3'] | ||
).toBe(6) | ||
|
||
//use a value | ||
await cachedFunc(1, 2, 3) | ||
|
||
//store new value | ||
await cachedFunc(1, 2, 3, 4) | ||
|
||
//get a value | ||
expect( | ||
cache['1.2.3.4'] | ||
).toBe(10) | ||
|
||
//check cache length -- 2 values | ||
expect( | ||
cache.length() | ||
).toBe(2) | ||
}) | ||
}) |