feature-base-cache v0.0.7
Feature-Base-Cache: A Pre-built Function for Faster Redis Implementation
Introduction
Feature-Base-Cache is a powerful package designed to streamline the implementation of Redis in your NestJS applications. It provides a set of pre-built functions that simplify the process of managing cache data, making it easier for developers to focus on the core functionalities of their applications.
Key Features
- Easy Integration: Seamlessly integrate Redis into your NestJS modules with minimal configuration.
- Pre-built Functions: Access a wide range of functions for managing cache data, including
delete
,update
,replace
,create
, and more. - Customizable: Tailor the caching behavior to your needs with customizable options such as cache delay, prefix, and unique key enforcement.
Getting Started
Step 1: Install the Package
First, ensure you have NestJS installed in your project. Then, install the feature-base-cache
package using npm:
npm install feature-base-cache
Step 2: Import and Configure the Module
In your NestJS module, import RedisProvider
and ConfigService
from feature-base-cache
. Then, include them in the providers
array of your module.
import { Module } from '@nestjs/common';
import { UserController } from './user.controller';
import { UserService } from './user.service';
import { ConfigService, RedisProvider } from 'feature-base-cache';
@Module({
controllers: [UserController],
providers: [UserService, RedisProvider, ConfigService]
})
export class YourModule {}
Step 3: Configure Redis Environment Variables
Set up your Redis environment variables in a .env
file. This allows your application to connect to the Redis server.
REDIS_HOST=localhost
REDIS_PORT=6379
Step 4: Use the Cache Service in Your Service or Repository
In your service or repository file, import BaseCacheService
and REDIS_DB
from feature-base-cache
. Then, inject REDIS_DB
into your service to access the Redis database.
import { Inject, Injectable } from '@nestjs/common';
import { BaseCacheService, REDIS_DB, RedisIO } from "feature-base-cache";
interface YourInterface {
something: any
}
@Injectable()
export class YourService{
private cacheService: BaseCacheService<YourInterface>;
constructor(
@Inject(REDIS_DB) readonly redis: RedisIO,
) {
this.cacheService = new BaseCacheService<YourInterface>(redis, {
cacheDelaySec: 60,
cachePrefix: 'your-prefix',
onlyUniqueKeys: true,
});
}
async test(){
this.cacheService.getKey("string");
}
}
Conclusion
With Feature-Base-Cache, you can efficiently manage cache data in your NestJS applications. Its pre-built functions and customizable options make it a versatile tool for developers looking to enhance their applications with caching capabilities.