npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@droplink/cache-decorators

v1.8.9

Published

A library for caching with decorators

Downloads

447

Readme

Cache Decorators

MAIN CI status Coverage Status NPM NPM Downloads

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