npm package: @micl/server-racache
Description: Provides time-sensitive caching with local file snapshot storage.
Author: alexgogoing@sina.com
Latest version: 0.0.4


# micl-server-racache

> Provides time-sensitive caching with local file snapshot storageLocal.

## Features

1. Provides time-sensitive cache retrieval and storageLocal.
2. Works in-memory and supports local file snapshots for storageLocal.
3. API available for managing cache and snapshot storageLocal.

## API

### MraCache Namespace

#### MraCacheOptionInterface

- **expireTime**: Default expiration time.
- **storePath**: Default path for local snapshot storageLocal.

```ts
interface MraCacheOptionInterface {
  expireTime: string;
  storePath: string;
}
```

#### config(options: MraCacheOptionInterface): void

- **Description**: Updates global configuration settings.
- **Parameters**: `options` — Configuration options as defined in `MraCacheOptionInterface`.

```ts
function config(options: MraCacheOptionInterface): void;
```

#### get<T>(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`.

```ts
function get<T>(key: string): T;
```

#### set<T>(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.

```ts
function set<T>(data: T): string;
```

#### set<T>(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.

```ts
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.

```ts
function remove(key: string): string | undefined;
```

#### store(path?: string): boolean

- **Description**: Saves the current cache data as a snapshot.
- **Parameters**: `path` (optional) — The storageLocal path for the snapshot.
- **Returns**: `true` if the snapshot was successfully saved, `false` otherwise.

```ts
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.

```ts
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:

```json
{
      "expireTime": "1d", 
      "storePath": "/test/test.json"
}
```

## Usage

1. **Install the Package:**

   Use `npm` or `yarn` to install the package.

   ```bash
   npm install micl-server-racache
   ```

2. **Import and Use in Your Project:**

   ```js
   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