A Next.js-based cache management API service with pluggable cache drivers.
- Features
- Supported Cache Drivers
- Environment Configuration
- API Endpoints
- API Usage Examples
- Getting Started
- Development
- Pluggable Cache System: Support for PostgreSQL cache backend via a
CACHE_DRIVERenvironment variable - PostgreSQL Driver: Production-ready cache storage with automatic table creation and cleanup
- Cache Operations API: RESTful endpoints for cache operations (GET, POST, DELETE)
- Optional Authentication: Token-based authentication (disabled by default for easy development)
- Health Check: System health monitoring endpoint with cache connectivity testing
- Admin Operations: Cache statistics, cleanup, and management endpoints
- TTL Support: Time-to-live functionality for automatic cache expiration
- Namespace Support: Organize cache entries by namespace for better management
- PostgreSQL (
postgres) - Default driver with persistent storage, TTL support, and automatic cleanup
Copy .env.example to .env.local and configure your environment:
cp .env.example .env.localSet the CACHE_DRIVER environment variable to choose your cache backend:
# Use PostgreSQL (default)
CACHE_DRIVER=postgres
POSTGRES_URL=postgresql://username:password@localhost:5432/cache_mateAuthentication is disabled by default. Enable with AUTH_ENABLED=true for production.
# Optional authentication (default: disabled)
AUTH_ENABLED=true
CACHE_AUTH_TOKEN=your_cache_token_here # For cache operations
ADMIN_AUTH_TOKEN=your_admin_token_here # For admin + cache operationsWhen enabled, include token in requests: Authorization: Bearer your_token_here
Retrieve cached data from the specified namespace and key.
Auth: Requires cache or admin token when AUTH_ENABLED=true
Parameters:
namespace- The cache namespace (string)key- The cache key, can be nested (e.g.,users/123/profile)
Response:
200- Cache entry found404- Cache entry not found or expired500- Internal server error
Example Response (200):
{
"namespace": "users",
"key": "john/profile",
"value": {
"name": "John Doe",
"email": "john@example.com"
},
"retrieved_at": "2025-06-24T10:30:00.000Z"
}Store data in cache with optional TTL (time-to-live).
Auth: Requires cache or admin token when AUTH_ENABLED=true
Parameters:
namespace- The cache namespace (string)key- The cache key, can be nested (e.g.,users/123/profile)
Request Body:
{
"value": any,
"ttl": number (optional, seconds)
}Response:
200- Data stored successfully400- Invalid request (missing value)500- Internal server error
Example Response (200):
{
"namespace": "users",
"key": "john/profile",
"value": {
"name": "John Doe",
"email": "john@example.com"
},
"ttl": 3600,
"stored_at": "2025-06-24T10:30:00.000Z"
}Remove cached data from the specified namespace and key.
Auth: Requires cache or admin token when AUTH_ENABLED=true
Parameters:
namespace- The cache namespace (string)key- The cache key, can be nested (e.g.,users/123/profile)
Response:
200- Data deleted successfully500- Internal server error
Example Response (200):
{
"namespace": "users",
"key": "john/profile",
"deleted_at": "2025-06-24T10:30:00.000Z"
}Get cache statistics and health status.
Auth: Requires admin token when AUTH_ENABLED=true
Response:
200- Statistics retrieved successfully500- Internal server error
Example Response (200):
{
"driver": "postgres",
"status": "healthy",
"stats": {
"total": 150,
"expired": 5,
"byNamespace": {
"users": 100,
"sessions": 45,
"products": 5
}
},
"timestamp": "2025-06-24T10:30:00.000Z"
}Perform administrative operations on the cache.
Auth: Requires admin token when AUTH_ENABLED=true
Request Body:
{
"action": "cleanup" | "stats" | "clear"
}Actions:
cleanup- Remove expired entriesstats- Get detailed statisticsclear- Remove all cache entries
Response:
200- Operation completed successfully400- Invalid action500- Internal server error
Example Response for cleanup (200):
{
"action": "cleanup",
"status": "completed",
"timestamp": "2025-06-24T10:30:00.000Z"
}Example Response for clear (200):
{
"action": "clear",
"status": "completed",
"timestamp": "2025-06-24T10:30:00.000Z"
}Get service health status including cache connectivity.
Auth: No authentication required
Response:
200- Service is healthy503- Service has issues
Example Response (200):
{
"status": "ok",
"uptime": 3600.5,
"timestamp": 1719225000000,
"cache": {
"driver": "postgres",
"status": "ok"
}
}Example Response (503):
{
"status": "error",
"uptime": 3600.5,
"timestamp": 1719225000000,
"cache": {
"driver": "postgres",
"status": "error",
"error": "Cache connectivity failed"
}
}Note: Add -H "Authorization: Bearer your_token" when authentication is enabled.
curl -X POST http://localhost:3000/api/cache/users/john/profile \
-H "Content-Type: application/json" \
-d '{"value": {"name": "John Doe", "email": "john@example.com"}, "ttl": 3600}'curl http://localhost:3000/api/cache/users/john/profilecurl -X DELETE http://localhost:3000/api/cache/users/john/profilecurl http://localhost:3000/api/cache/admincurl -X POST http://localhost:3000/api/cache/admin \
-H "Content-Type: application/json" \
-d '{"action": "cleanup"}'curl -X POST http://localhost:3000/api/cache/admin \
-H "Content-Type: application/json" \
-d '{"action": "clear"}'curl http://localhost:3000/api/healthzInstall dependencies and run the development server:
npm install
npm run devThe API will be available at http://localhost:3000.
npm run dev # Start development server with Turbopack
npm run build # Build for production
npm run start # Start production server
npm run lint # Run ESLint