base-cache-features v0.0.1
Base Cache Features
🫵PLEASE WRITE ME SUGGESTIONS HOW TO IMPROVE THIS PACKAGE🫵
LINK TO THIS PACKAGE IN GITHUB: https://github.com/Waste-arch/base-cache-features
Base Cache Features is a lightweight, yet powerful, npm package designed to simplify the process of managing cache operations in Node.js applications. It leverages the robustness of Redis, a popular in-memory data structure store, to provide efficient caching capabilities. This package is particularly useful for developers looking to implement caching strategies in their applications without the complexity of managing Redis directly.
Installation
To install Base Cache Features, simply run the following command in your project directory:
npm install base-cache-features
Usage
To use Base Cache Features, you first need to import the BaseCacheService
class and instantiate it with a Redis client and configuration options. Here's a basic example:
import { Redis } from "ioredis";
import { BaseCacheService } from "base-cache-features";
const redis = new Redis(); // Configure your Redis client as needed
const cacheService = new BaseCacheService(redis, {
cacheDelaySec: 3600, // Cache expiration time in seconds
cachePrefix: "myApp", // Prefix for cache keys
onlyUniqueKeys: true, // Ensure keys are unique
});
API Reference
BaseCacheService
Constructor
new BaseCacheService(redis: Redis, options: BaseCacheServiceOptions)
Creates a new instance of BaseCacheService
with the provided Redis client and configuration options.
create(id: string, object: OType): Promise<void>
Creates a new cache entry with the specified id
and object
. The object
is serialized to JSON before being stored.
createMany(objects: OType[], identifierField: keyof OType): Promise<void>
Creates multiple cache entries using an array of objects. The identifierField
is used to generate unique keys for each object.
replaceOne(id: string, object: OType): Promise<void>
Replaces an existing cache entry with the specified id
and object
. The object
is serialized to JSON before being stored.
getOne(id: string): Promise<OType | undefined>
Retrieves a single cache entry by id
. Returns the deserialized object or undefined
if the entry does not exist.
getMany(): Promise<OType[]>
Retrieves all cache entries. Returns an array of deserialized objects.
deleteOne(id: string): Promise<void>
Deletes a cache entry by id
.
drop(): Promise<void>
Deletes all cache entries.
BaseCacheServiceOptions
{
cacheDelaySec: number; // Cache expiration time in seconds
cachePrefix: string; // Prefix for cache keys
onlyUniqueKeys: boolean; // Ensure keys are unique
}
Configuration options for BaseCacheService
.
Contributing
Contributions are welcome! Please feel free to submit issues or pull requests.
License
Base Cache Features is released under the MIT License.
1 year ago