Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions .eslintignore

This file was deleted.

18 changes: 0 additions & 18 deletions .eslintrc

This file was deleted.

6 changes: 3 additions & 3 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ jobs:

strategy:
matrix:
node-version: [14.x, 16.x, 18.x, 20.x]
node-version: [20.x, 22.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
Expand Down
32 changes: 6 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# What is it?

This is a library providing both a higher-order function and a decorator to cache the result of a function/method if given conditions are met.
This is a library providing a higher-order function to cache the result of a function if given conditions are met.

# How do I install it?

Expand Down Expand Up @@ -84,9 +84,7 @@ await sleep(31000); // <-- This will invalidate the cache because of the ttl

# How does it work?

## Higher-order function

The library exposes the `cacheCandidate` function which accepts the function to be cached as the first argument and the options as the second argument.
The library exposes the `cacheCandidate` function which accepts the function to be cached as the first argument and the options as the second argument.
The returned function is an async function which returns a Promise fulfilled with the cached value if the method has already been called with the same arguments and/or the conditions are met.

The options available are:
Expand Down Expand Up @@ -131,30 +129,12 @@ The options available are:
- `plugins` (_optional_): An array of plugins to be used. Default: `[]`.
Please, refer to the [@jointly/cache-candidate-plugin-base](https://github.com/JointlyTech/cache-candidate-plugin-base) package for more information.

## Decorator

The decorator expects to receive the options as the first argument and works exactly as the higher-order function.

### Example

```ts
import { CacheCandidate } from '@jointly/cache-candidate';
## Key composition

class MyClass {
@CacheCandidate({
requestsThreshold: 3,
ttl: 30000,
})
async getUsers(filters = {}) {
return db.query('SELECT * FROM users WHERE ?', filters);
}
}
The cache key is composed based on the following criteria:

const myInstance = new MyClass();
await myInstance.getUsers({ name: 'John' }); // <-- This won't be cached, because the requestsThreshold is 3
await myInstance.getUsers({ name: 'John' }); // <-- This won't be cached, because the requestsThreshold is 3
await myInstance.getUsers({ name: 'John' }); // <-- This WILL be cached, because the requestsThreshold is 3!
```
- The arguments passed to the method (JSON.stringify)
- `uniqueIdentifier`: A uniqid generated to allow multiple files to contain the same function name

## Conditions / Criterias
The conditions are, within the given `timeFrame`:
Expand Down
54 changes: 54 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import eslint from '@eslint/js';
import tseslint from '@typescript-eslint/eslint-plugin';
import tsparser from '@typescript-eslint/parser';

export default [
{
ignores: [
'node_modules/**',
'dist/**',
'demo/**',
'esbuild.js',
'coverage/**',
'jest.config.js',
'*.test.ts'
]
},
eslint.configs.recommended,
{
files: ['**/*.ts'],
languageOptions: {
globals: {
process: true,
__dirname: true,
__filename: true,
exports: true,
module: true,
require: true,
Buffer: true,
setTimeout: true,
clearTimeout: true,
setInterval: true,
clearInterval: true,
console: true,
global: true
},
parser: tsparser,
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module'
}
},
plugins: {
'@typescript-eslint': tseslint
},
rules: {
'no-console': 'warn',
'no-debugger': 'error',
'no-var': 'error',
'no-unused-vars': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/ban-types': 'off'
}
}
];
5 changes: 0 additions & 5 deletions jest.config.js

This file was deleted.

Loading