cache-center
v1.0.33
Published
A powerful and flexible Node.js caching library that provides a simple interface for managing cached data with support for products, categories, and users.
Readme
Cache Center
A Node.js library for managing cache operations, providing a set of functions for handling cached data.
Installation
npm install cache-centerInitialization
First, create a .env file in your project root with the following variables:
REDIS_HOST=localhost # Server host
REDIS_PORT=6379 # Server port
REDIS_PASSWORD= # Password (optional)Then, initialize the Redis client in your application:
import { initRedisClient } from 'cache-center';
// Initialize Redis client
initRedisClient({
host: process.env.REDIS_HOST || 'localhost',
port: parseInt(process.env.REDIS_PORT || '6379', 10),
password: process.env.REDIS_PASSWORD,
connectTimeout: 10000 // optional, defaults to 10000ms
});Available Functions
Products
// Store a product in cache
// id: Product identifier
// value: Product data
// ttlInSec: Time to live in seconds (optional)
await setProduct(id: string, value: any, ttlInSec?: number)
// Retrieve a product from cache
// id: Product identifier
// return: Product data or null if not found
await getProduct(id: string)
// Delete a product from cache
// id: Product identifier
await deleteProduct(id: string)Categories
// Store a category in cache
// id: Category identifier
// value: Category data
// ttlInSec: Time to live in seconds (optional)
await setCategory(id: string, value: any, ttlInSec?: number)
// Retrieve a category from cache
// id: Category identifier
// return: Category data or null if not found
await getCategory(id: string)
// Delete a category from cache
// id: Category identifier
await deleteCategory(id: string)Users
// Store a user in cache
// id: User identifier
// value: User data
// ttlInSec: Time to live in seconds (optional)
await setUser(id: string, value: any, ttlInSec?: number)
// Retrieve a user from cache
// id: User identifier
// return: User data or null if not found
await getUser(id: string)
// Delete a user from cache
// id: User identifier
await deleteUser(id: string)Usage Examples
import { setProduct, getProduct, deleteProduct } from 'cache-center';
// Store a product with 1-hour expiration
await setProduct('123', { name: 'Product 1', price: 100 }, 3600);
// Retrieve product data
const product = await getProduct('123');
// Delete the product
await deleteProduct('123');Error Handling
The library automatically handles Redis connection errors and provides console logging for:
- Successful connection
- Connection errors
- Connection timeouts
- Client initialization status
License
ISC
