epic-cache
v1.1.0
Published
# Install
Downloads
5
Readme
Simple nodejs caching module
Install
npm i epic-cache
Cache classes
Cache
The base abstract class extended by the other cache classes.
Only used to create other types of cache.
GenericCache
The main cache, it holds a map of <String, CacheType = any>
Create the instance
import { GenericCache } from 'epic-cache';
const cache = new GenericCache<CacheType>(options);
CacheType
: (default:any
) Type of the elements you want to store in cache.options?
: GenericCacheOptions
GenericCacheOptions
maxSize
: (default:1000
) max elements to store in cachesizeExceededStrategy
: (default:no-cache
) action to perform when an element is add to the cache when it's full.no-cache
: don't put the new element in the cachethrow-error
: throw an error
clearExpiredOnSizeExceeded
: (default:true
) clear all the expired keys before trying to an element to the cache when it's fulldefaultExpireTime
: (default:15m
) default expire time assigned to elements add to cache without their own expireTime- The type of
defaultExpireTime
isTime
|TimeString
|number
- The type of
expireOnInterval
: (default:true
) check and remove the expired keys every interval (the cache will always remove the expired keys when you try to get them regardless of this value)expireCheckInterval
: (default:10m
) the time interval to check and remote expired keys- The type of
expireCheckInterval
isTime
|TimeString
|number
- The type of
Methods
Add an element
import { GenericCache, CacheElement } from 'epic-cache';
const cache = new GenericCache<number>();
// Add MattAge to the cache with value 31 with default expireTime of the cache
cache.set('MattAge', CacheElement.from(31));
// Add TaylorAge to the cache with value 22 with 10 seconds before expiration
cache.set('TaylorAge', CacheElement.from(22, '10s'));
// Note: you can also use the add function - The behavior is the exact same
cache.add('TaylorAge', CacheElement.from(22, '10s'))
Get an element
import { GenericCache, CacheElement } from 'epic-cache';
const cache = new GenericCache<number>();
cache.set('TaylorAge', CacheElement.from(22));
// Get the element from the cache
const taylorAge = cache.get('TaylorAge'); // 22
Delete an element
// TODO: Create the code
Events
// TODO: Create the code and the docs
CacheElement
// TODO: Create the code and the docs
Time classes
When you need to provide a time value you can use one of this three methods:
TimeString
type
import type { TimeString } from 'epic-cache';
const oneSecond: TimeString = '1s';
const fiveSeconds: TimeString = '5s';
const oneMinute: TimeString = '1m';
const oneHour: TimeString = '1h';
number
as milliseconds
const oneSecond = 1000;
const fiveSeconds = 1000 * 5;
const oneMinute = 1000 * 60;
const oneHour = 1000 * 60 * 60;
Time
class
import { Time } from 'epic-cache';
// You can pass a number or a TimeString as value
const oneSecond = new Time(1000);
const fiveSecond = new Time('5s');
// You can also use the static method from to create an instance
const oneMinute = Time.from(1000 * 60);
const oneHour = Time.from('1h');