From 0f90f4971a39162530afc7a56493db86a4838474 Mon Sep 17 00:00:00 2001 From: vini <91087061+vinikjkkj@users.noreply.github.com> Date: Thu, 8 Aug 2024 18:07:21 -0300 Subject: [PATCH] Update README.md --- README.md | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/README.md b/README.md index 1f502fa..72470cb 100644 --- a/README.md +++ b/README.md @@ -81,3 +81,81 @@ Trigged when key expires, by a TTL, LRU, LFU... ```typescript cache.on('expired', key => console.log(key)) ``` + +### Pooling Caches +You can store all your caches in one variable, recommended in big projects to organize code + +#### Create new pool +```typescript +import { CachePool } from 'cachetools-js' + +const pool = new CachePool({maxsize: 10}) +``` +#### Create a cache in pool +```typescript +//create a new TTLCache in pool, this function return the new cache +pool.createCache('c1', 'ttl', {ttl: 10}) +``` +#### Get a cache in pool +```typescript +const cache = pool.getCache('c1') +``` +#### Del a cache in pool +```typescript +pool.delCache('c1') +``` +#### Del all caches in pool +```typescript +pool.delAllCaches() +``` +#### Set a key in some cache in pool +```typescript +pool.createCache('c1', 'ttl', {ttl: 10}) + +pool.set('c1', 'key', 'value') +``` +#### Get a key in some cache in pool +```typescript +pool.createCache('c1', 'ttl', {ttl: 10}) + +const cache = pool.get('c1', 'key') +``` +#### Del a key in some cache in pool +```typescript +pool.createCache('c1', 'ttl', {ttl: 10}) + +pool.del('c1', 'key') +``` +#### Del all keys in some cache in pool +```typescript +pool.createCache('c1', 'ttl', {ttl: 10}) + +pool.delAll('c1') +``` +#### Get the length of pool +```typescript +pool.createCache('c1', 'ttl', {ttl: 10}) + +const length = pool.length() +``` + +### Decorator +You can decore a function to apply caching if you use same args + +#### How to Use +```typescript +import { cacheDecorator, TTLCache } from 'cachetools-js' + +//function to sum values +const fn = (...args: number[]) => args.reduce((p, c) => p + c) +const cache = new TTLCache({ttl: 1000}) +const decoredFn = cacheDecorator(fn, cache) + +//will store result in cache and return +await decoredFn(1, 2, 3) + +//will return cached value -- second call +await decoredFn(1, 2, 3) + +//if you change args, the process will repeat +```