0.1.8 • Published 1 year ago

@zcodeapp/cache v0.1.8

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

Cache

Cache CI codecov

Overview

The Cache module is a part of the @zcodeapp framework, designed to provide a flexible caching mechanism for applications. It leverages dependency injection and interfaces from the framework to create a versatile and extensible caching system. The module includes a basic MemoryStrategy for caching, but is built to accommodate different caching strategies.

Features

Flexible Caching Strategy The Cache class allows for changing the caching strategy at runtime, providing flexibility in how data is cached.

Memory-Based Caching Includes a default MemoryStrategy for in-memory caching, suitable for scenarios where quick access to data is required and persistence is not a concern.

MD5 Key Transformation Utilizes MD5 hashing for keys, ensuring consistent key formatting and potentially reducing the risk of key collisions.

Simple API Offers a straightforward API with set and get methods for storing and retrieving data.

Logging Integration Integrates with the @zcodeapp/logger for logging activities, aiding in debugging and monitoring.

Installation

Include the Cache class in your TypeScript project:

npm install @zcodeapp/cache

Usage

import { Di } from '@zcodeapp/di';
import { Cache } from '@zcodeapp/cache';

const di = Di.getInstance();
const cache = di.get(Cache);

await cache.set('myKey', 'myValue');

const value = await cache.get('myKey');

API Reference

Cache Class

  • constructor(logger: Logger, cacheStrategy: MemoryStrategy): Initializes the cache with a logger and a caching strategy.
  • changeStrategy(strategy: ICacheStrategy): void: Changes the caching strategy at runtime.
  • async set(key: string, value: string): Promise<void>: Stores a value in the cache.
  • async get(key: string): Promise<string>: Retrieves a value from the cache.

MemoryStrategy Class

  • async set(key: string, value: string): Promise<void>: Stores a value in memory.
  • async get(key: string): Promise<string>: Retrieves a value from memory.

Example

exampleStrategy.ts

import { Injectable } from "@zcodeapp/di";
import { ICacheStrategy } from "@zcodeapp/interfaces";

@Injectable()
export class ExampleStrategy implements ICacheStrategy {
    public async set(key: string, value: string): Promise<void> {
      // your code...
    }

    public async get(key: string): Promise<string> {
      // your code...
    }
}

main.ts

import { Di } from '@zcodeapp/di';
import { Cache } from '@zcodeapp/cache';
import { ExampleStrategy } from 'exampleStrategy';

const di = Di.getInstance();
const cache = di.get(Cache);
cache.changeStrategy(di.get(ExampleStrategy));

await cache.set('myKey', 'myValue');

const value = await cache.get('myKey');

License

This project is licensed under the MIT License - see the LICENSE file for details.

0.1.8

1 year ago

0.1.7

1 year ago

0.1.6

1 year ago

0.1.5

1 year ago

0.1.4

1 year ago

0.1.2

1 year ago

0.1.3

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago

0.0.1

1 year ago