The Cache Library is a simple cache implementation that allows you to store and retrieve values with a key-value pair system. The library supports multiple storage layers and providers to enable caching data in different locations such as memory, disk, or cloud storage.
To install the Cache library, you can use NPM or Yarn:
npm install super-cache
yarn add super-cache
To use the Cache library, you first need to import the necessary classes and types:
import Namespace from "./Namespace";
import StorageLayer from './StorageLayer';
import ValuesProvider from "./ValuesProvider";
import {GettableItem} from "./types";
import {CreateLayerOptions} from "./types/ILayerOptions";
import {DriverType, NamespaceContext, NamespaceProvider, StorageValue, TransactionOptions} from "./types/common";
import { isUnset, requireStorage } from './utils';
To create a new cache instance, you need to instantiate the Cache
class:
const cache = new Cache();
You can add storage layers to the cache by calling the addStorage
method:
cache.addStorage('memory');
cache.addStorage('local', { prefix: 'myapp' });
This adds a memory storage layer and a local storage layer with a prefix of "myapp". You can also pass a custom storage layer instance instead of a string identifier.
You can set the namespace for the cache by calling the setNamespace
method:
cache.setNamespace('user', {
getNamespace: () => 'user1'
});
This sets the namespace for the "user" context to "user1".
You can add value providers to the cache by calling the addValueProvider
method:
cache.addValueProvider('myprovider', (key) => {
return myDataProvider.getValue(key);
});
This adds a value provider with the base key of "myprovider". The getValue
method of myDataProvider
will be called to retrieve the value for the key.
You can get and set values in the cache by calling the get
and set
methods:
await cache.set('key', 'value');
const value = await cache.get('key');
You can remove values from the cache by calling the clear
method:
await cache.clear('myprovider');
This removes all values with the base key of "myprovider". If no base key is specified, all values in the cache will be removed.
You can synchronize values in the cache with the storage layers by calling the sync
method:
await cache.sync('key');
This synchronizes the value with the key "key" in all storage layers.
The cache library supports batch operations for getting and setting values:
const values = await cache.mget('key1', 'key2', 'key3');
await cache.mset({
key1: 'value1',
key2: 'value2',
key3: 'value3'
});
These methods allow you to retrieve and set multiple values at once.
The Cache Library is a simple and flexible caching solution that can be used in a variety of applications. With support for multiple storage layers and providers, you can choose the best caching strategy for your use case.