git clone https://mtsweb@dev.azure.com/mtsweb/oss/_git/storage
This package is distributed via npm. You can install it as a dependency in your project by running:
yarn add @micheldever/storage
Create a new StorageEntity
instance and use it to interact with your storage entries:
import { StorageEntity, createStorageEntry } from '@micheldever/storage';
import { MemoryStorageAdapter } from '@micheldever/storage/adapters';
const entity = new StorageEntity(new MemoryStorageAdapter());
You can attach one or more storage adapters to your storage instance during initialization.
Alternatively, you can attach additional adapters at any time using the addAdapter
method.
entity.addAdapter(new MemoryStorageAdapter());
await entity.set('testKey', createStorageEntry('testValue', 10000));
Entries stored within a StorageEntity
can define their own ttl
in milliseconds. After this time
has elapsed, the next time the value is accessed you will get undefined
instead. Entries with
a ttl
of Infinity
will never expire and will be available indefinitely.
const value = await entity.get('testKey');
await entity.del('testKey');
This package comes preconfigured with two adapters. Additional adapters can be created by
implementing the StorageAdapter
interface.