0.0.3 • Published 10 months ago
@micl/server-racache v0.0.3
micl-server-racache
Provides time-sensitive caching with local file snapshot storage.
Features
- Provides time-sensitive cache retrieval and storage.
- Works in-memory and supports local file snapshots for storage.
- API available for managing cache and snapshot storage.
API
MraCache Namespace
MraCacheOptionInterface
- expireTime: Default expiration time.
- storePath: Default path for local snapshot storage.
interface MraCacheOptionInterface {
expireTime: string;
storePath: string;
}config(options: MraCacheOptionInterface): void
- Description: Updates global configuration settings.
- Parameters:
options— Configuration options as defined inMraCacheOptionInterface.
function config(options: MraCacheOptionInterface): void;get(key: string): T
- Description: Retrieves data from the cache.
- Parameters:
key— A unique identifier for the cached data. - Returns: The cached data of type
T.
function get<T>(key: string): T;set(data: T): string
- Description: Stores data in the cache.
- Parameters:
data— Data to be stored in the cache. - Returns: A unique identifier for the stored data.
function set<T>(data: T): string;set(key: string, data: T, expireTime?: string): string
- Description: Stores data in the cache with a specific expiration time.
- Parameters:
key— A unique identifier for the cached data.data— Data to be stored in the cache.expireTime(optional) — Expiration time in the format1d,1h,1m.
- Returns: A unique identifier for the stored data.
function set<T>(key: string, data: T, expireTime?: string): string;remove(key: string): string | undefined
- Description: Removes data from the cache.
- Parameters:
key— The unique identifier for the data to be removed. - Returns: A unique identifier for the removed data, or
undefinedif not found.
function remove(key: string): string | undefined;store(path?: string): boolean
- Description: Saves the current cache data as a snapshot.
- Parameters:
path(optional) — The storage path for the snapshot. - Returns:
trueif the snapshot was successfully saved,falseotherwise.
function store(path?: string): boolean;load(path: string): void
- Description: Loads a snapshot from a specified path.
- Parameters:
path— The path from which to load the snapshot.
function load(path: string): void;Configuration
MraCacheOptionInterface
- expireTime: The default expiration time for cached data (e.g.,
1d,1h,1m). - storePath: The default directory where cache snapshots are stored.
Example:
{
"expireTime": "1d",
"storePath": "/test/test.json"
}Usage
Install the Package:
Use
npmoryarnto install the package.npm install micl-server-racacheImport and Use in Your Project:
const MraCache = require('micl-server-racache'); // Configure cache MraCache.config({ expireTime: '1d', storePath: './cache-snapshots.json' }); // Set data in cache MraCache.set('user_data', { name: 'John Doe', age: 30 }); // Get data from cache const userData = MraCache.get('user_data'); // Save cache snapshot MraCache.store('./cache-snapshots.json'); // Load cache snapshot MraCache.load('./cache-snapshots.json');
Contributing
You can contribute to this module online via CodeSandBox, or locally:
- Clone the repository.
- Install dependencies with
yarn installornpm install. - Start the development server using
yarn devornpm run dev.
License
This project is licensed under the MIT License.
Copyright (c) alexgogoing alexgogoing@sina.com