Cache Decorators

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,
port: Number(process.env.REDIS_PORT),
});
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";
export class MyCustomRepository implements ICacheRepository {
}
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 })
public async save(): Promise<any> {
}
}
@CacheRetrieve
class MyClass {
@CacheRetrieve({ key: "my-key", ttl: 60 })
public async get(): Promise<any> {
}
}
@CacheRemove
class MyClass {
@CacheRemove({ key: "my-key" })
public async save(): Promise<any> {
}
}
@CacheRemoveByPrefix
class MyClass {
@CacheRemoveByPrefix({ key: "my-key-prefix" })
public async save(): Promise<any> {
}
}
@CacheInvalidate
class MyClass {
@CacheInvalidate({ key: "my-key" })
public async save(): Promise<any> {
}
}
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> {
}
}
@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> {
}
}
License
This project is licensed under the MIT License.
Created By
