4.2.0 • Published 5 months ago

sugar-cache v4.2.0

Weekly downloads
-
License
GPLV3
Repository
github
Last release
5 months ago

sugar-cache

Cache library for Node.js built on top of ioredis.

Usage

SugarCache exports a class that instantiates a cache for you when constructed -

import { SugarCache } from 'sugar-cache';
import Redis from 'ioredis';

const redisObj = new Redis();
const cache = new SugarCache(redisObj, { width: 1000 });

The cache creation options are -

  • width: The maximum number of elements that the cache can hold. This option is required
  • namespace: The namespace for your cache. If namespace is not specified all values are cached under the same default namespace as other instances of the cache. This option allows you to create multiple caches isolated from each other, which is recommended if you're instantiating multiple cache objects.
  • ttl: Time-to-live for cache entries in milliseconds.

Interface

The cache object provides some base API methods -

  • set - Adds a value to cache.
await cache.set(key, value);
  • get - Reads a value from cache.
const cachedVal = await cache.get(key);
  • del - Deletes a key value pair from cache.
await cache.del(key);
  • clear - Delete all values under the namespace of the cache.
await cache.clear();

Decorator methods

SugarCache provides some decorator methods for easier usage. instead of invoking the function again. You can use the getOrSet method for this.

  • getOrSet -
    Say you need to set cache on the results of a function invocation so that the next time the function is invoked with some subset of its arguments, you get the result from cache.
@cache.getOrSet(['resourceId', 'parentResourceId'])
async readResource(resourceId: string, parentResourceId: string, ...rest) { ... }
  • invalidate - For when you need to invalidate a resource from cache. For instance, for the readResource function above, if we wish to delete the resource from cache whenever its changed from another function -
@cache.invalidate(['resourceId', 'parentResourceId'])
async editResource(resourceId: string, parentResourceId: string, logger, ...rest) { ... }

Contributors

License

GPL-v3

4.2.0

5 months ago

4.1.0

5 months ago

4.0.1

5 months ago

4.0.0

6 months ago

3.0.1

1 year ago

3.0.0

1 year ago

1.2.8

2 years ago

1.2.7

2 years ago

1.4.0

1 year ago

1.3.0

1 year ago

2.0.2

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.2.9

2 years ago

1.2.12

1 year ago

1.2.10

1 year ago

1.2.11

1 year ago

1.2.6

2 years ago

1.2.5

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago