Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
vinikjkkj committed Aug 8, 2024
2 parents f6d24f5 + 0f90f49 commit 2cdb425
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

0 comments on commit 2cdb425

Please sign in to comment.