cache-minimal
v1.0.4
Published
A minimalist caching library with Redis and in-memory fallback
Downloads
27
Maintainers
Readme
cache-minimal
A lightweight caching library for Node.js with Redis and in-memory caching support. Designed for simplicity and flexibility.
🚀 Features
- ✅ Supports both In-Memory and Redis caching
- ✅ Custom TTL (Time-To-Live) for cached data
- ✅ Easy integration with Node.js applications
- ✅ Minimal dependencies & high performance
📦 Installation
Install via npm:
npm install cache-minimal🔧 Usage
Basic In-Memory Caching
import { CacheManager } from "cache-minimal";
const cache = new CacheManager();
cache.set("name", "John Doe", 60); // Store with a 60-second TTL
const value = await cache.get("name");
console.log(value); // Output: John DoeUsing Redis as Cache Backend
Create a .env file in your project:
REDIS_HOST=your_redis_host
REDIS_PORT=your_redis_port
REDIS_USERNAME=your_redis_username
REDIS_PASSWORD=your_redis_password
REDIS_TTL=60import { CacheManager } from "cache-minimal";
// Use parameters according to your use case. If you don't require username and password, you can skip those parameters.
const cache = new CacheManager({
useRedis: true,
host: REDIS_HOST,
port: REDIS_PORT,
username: REDIS_USERNAME, // Fixed typo here
password: REDIS_PASSWORD,
defaultTTL: REDIS_TTL,
});
await cache.set("user", { id: 1, name: "Alice" }, 120);
const user = await cache.get("user");
console.log(user); // Output: { id: 1, name: "Alice" }Deleting a Cache Key
await cache.delete("name");Clearing All Cache
await cache.clear();
console.log("All cache cleared!");🌐 Environment Variables
If you are using Redis as the cache backend, you can configure the connection using a .env file. Create a .env file in your project root with the following variables:
REDIS_HOST=your_redis_host
REDIS_PORT=your_redis_port
REDIS_USERNAME=your_redis_username
REDIS_PASSWORD=your_redis_password
REDIS_TTL=60REDIS_HOST: The hostname or IP address of your Redis server (default:localhost).REDIS_PORT: The port number of your Redis server (default:6379).REDIS_USERNAME: (Optional) The username for Redis authentication.REDIS_PASSWORD: (Optional) The password for Redis authentication.REDIS_TTL: (Optional) Default time-to-live (TTL) for cached items in seconds (default:60).
📖 API Reference
CacheManager
The main class for managing cache operations.
Constructor
new CacheManager(options?: CacheOptions)options(optional): Configuration options for the cache.useRedis(boolean): Whether to use Redis as the cache backend (default:false).host(string): Redis host (default:localhost).port(number): Redis port (default:6379).username(string): Redis username (optional).password(string): Redis password (optional).defaultTTL(number): Default TTL for cached items in seconds (default:60).
Methods
set(key: string, value: any, ttl?: number): Promise<void>- Stores a value in the cache with an optional TTL.
get(key: string): Promise<any | null>- Retrieves a value from the cache by key.
delete(key: string): Promise<void>- Deletes a specific key from the cache.
clear(): Promise<void>- Clears all cached data.
📜 License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
