@droplink/cache-decorators
TypeScript icon, indicating that this package has built-in type declarations

1.9.2 • Public • Published

Cache Decorators

MAIN CI status Coverage Status NPM NPM Downloads Socket Badge

Cache Decorators is a lightweight TypeScript library that provides decorators for caching function results, seamlessly integrating with various caching implementations. It offers decorators for caching, invalidating cache entries, and removing cache entries based on predefined conditions. With Cache Decorators, you can easily enhance the performance of your applications by caching expensive function calls, reducing response times, and optimizing resource utilization.

Solution

Cache Decorators adopts a decorator-based approach, allowing you to apply caching logic to your functions with minimal effort. Whether you need to cache function results, invalidate cache entries, or remove cached data under specific conditions, Cache Decorators offers a flexible solution to suit your needs. Additionally, its support for the adapter pattern enables you to switch between different caching implementations effortlessly.

Installation

You can install Cache Decorators via npm:

npm install @droplink/cache-decorators

Getting Started

To start using Cache Decorators, follow these simple steps:

Initialize Repository: Start by initializing the cache repository using your preferred caching solution. For example:

import { DataSource, AdapterEnum } from "@droplink/cache-decorators";

DataSource.initialize(AdapterEnum.REDIS, {
  host: process.env.REDIS_HOST, // Set your caching host
  port: Number(process.env.REDIS_PORT), // Set your caching port
  // ...other options
});

Note: Install Dependencies: Depending on the adapter (enum) chosen, you may need to install the corresponding library for the caching strategy. For example, if you choose the REDIS adapter, you need to install the ioredis library.

  • AdapterEnum.REDIS: npm install ioredis
  • Other adapters: in progress

Initialize Custom Repository

Alternatively, you can define your own custom repository implementation.

First, import the ICacheRepository interface from the @droplink/cache-decorators package. Ensure that this interface is implemented to provide all necessary methods for decorators.

import { ICacheRepository } from "@droplink/cache-decorators"; // Ensure that this interface is implemented to provide all necessary methods for decorators

export class MyCustomRepository implements ICacheRepository {
  // Implement the required methods here
}

Then, initialize your custom repository like this:

import { DataSource } from "@droplink/cache-decorators";
import { MyCustomRepository } from "../MyCustomRepository";

DataSource.setCustomRepository(new MyCustomRepository());

Now, you can get your custom repository anywhere in your code, like this:

import { DataSource } from "@droplink/cache-decorators";

const repository = DataSource.getCustomRepository();

Using Decorators

Apply Decorators: Apply the cache decorators to your functions. For example:

@CacheSave

class MyClass {
  @CacheSave({ key: "my-key", ttl: 60 }) // TTL is specified in seconds
  public async save(): Promise<any> {
    // Your function logic here
  }
}

@CacheRetrieve

class MyClass {
  @CacheRetrieve({ key: "my-key", ttl: 60 }) // TTL is specified in seconds
  public async get(): Promise<any> {
    // Your function logic here
  }
}

@CacheRemove

class MyClass {
  @CacheRemove({ key: "my-key" })
  public async save(): Promise<any> {
    // Your function logic here
  }
}

@CacheRemoveByPrefix

class MyClass {
  @CacheRemoveByPrefix({ key: "my-key-prefix" })
  public async save(): Promise<any> {
    // Your function logic here
  }
}

@CacheInvalidate

class MyClass {
  @CacheInvalidate({ key: "my-key" })
  public async save(): Promise<any> {
    // Your function logic here
  }
}

Using Custom Keys

You have the flexibility to define custom keys for all decorators, as demonstrated below:

@CacheRetrieve | @CacheInvalidate | @CacheRemove | @CacheRemoveByPattern

You can specify the types for your input and use them to create a dynamic key based on the input arguments.

class MyClass {
  @CacheRetrieve<Input>({ key: (input) => `my-prefix/${input.myParams}` })
  public async save(input: Input): Promise<any> {
    // Your function logic here
  }
}

@CacheSave

You can specify the types for your input and output and use them to create a dynamic key based on the input and output arguments.

class MyClass {
  @CacheSave<Input, Output>({
    key: (input, output) =>
      `my-prefix/${input.myInputParams}/${output.myOutputParams}`,
  })
  public async save(input: Input): Promise<Output> {
    // Your function logic here
  }
}

License

This project is licensed under the MIT License.

Created By

Package Sidebar

Install

npm i @droplink/cache-decorators

Weekly Downloads

41

Version

1.9.2

License

MIT

Unpacked Size

255 kB

Total Files

408

Last publish

Collaborators

  • allanchrs