0.0.3 • Published 10 months ago

@micl/server-racache v0.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
10 months ago

micl-server-racache

Provides time-sensitive caching with local file snapshot storage.

Features

  1. Provides time-sensitive cache retrieval and storage.
  2. Works in-memory and supports local file snapshots for storage.
  3. 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 in MraCacheOptionInterface.
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 format 1d, 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 undefined if 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: true if the snapshot was successfully saved, false otherwise.
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

  1. Install the Package:

    Use npm or yarn to install the package.

    npm install micl-server-racache
  2. Import 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:

  1. Clone the repository.
  2. Install dependencies with yarn install or npm install.
  3. Start the development server using yarn dev or npm run dev.

License

This project is licensed under the MIT License.

Copyright (c) alexgogoing alexgogoing@sina.com

0.0.3

10 months ago

0.0.2

10 months ago

0.0.1

10 months ago