@del-internet/support-cache
v1.0.5
Published
TypeScript package to handle cache
Downloads
96
Readme
Install
npm i @del-internet/support-cache
Usage
import factory from '@del-internet/support-cache';
const config = {....}
const Manager = factory(config);
Configurations
This package supports following drivers by default:
s3
, static
, array
, file
A simple configuration example is here:
{
default: process.env.CACHE || 's3',
drivers: {
array: {},
static: {},
file: {
basePath: undefined,
ns: undefined,
ttl: 3600,
extension: undefined,
},
s3: {
bucket: process.env.S3_CACHE_BUCKET ?? 'cache',
expiration: process.env.S3_CACHE_EXPIRATION ?? 30, // remove after one month
client: {
credentials: {
accessKeyId: process.env.S3_ACCESS_KEY_ID,
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY,
},
region: process.env.S3_REGION ?? 'us',
endpoint: process.env.S3_BASE_URL,
forcePathStyle: process.env.S3_PATH_STYLE ?? true,
},
},
redis: {
url: process.env.REDIS_URL,
},
},
}
Do more configuration as possible in related packages:
redis
: 'https://www.npmjs.com/package/redis's3
: 'https://www.npmjs.com/package/@aws-sdk/client-s3'file
: 'https://www.npmjs.com/package/file-system-cache'
Methods
store into cache
- default driver
Manager::remember(key, value, ttl)
- custom driver
Manager::driver('driver-name').remember(key, value, ttl)
store into cache forever
- default driver
Manager::forever(key, value)
- custom driver
Manager::driver('driver-name').forever(key, value)
retrieve from cache
- default driver
Manager::get(key, default)
- custom driver
Manager::driver('driver-name').get(key, default)
retrieve from cache and forget
- default driver
Manager::pull(key, default)
- custom driver
Manager::driver('driver-name').pull(key, default)
delete from cache
- default driver
Manager::forget(key)
- custom driver
Manager::driver('driver-name').forget(key)
check cache
- default driver
Manager::has(key)
- custom driver
Manager::driver('driver-name').has(key)