This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
petty-cache is a Node.js cache module that implements a two-level cache (in-memory cache + Redis) with features to avoid cache stampedes and thundering herds. It also includes distributed mutex and semaphore locking primitives.
# Run tests
npm test
# Run tests with coverage and send to Coveralls
npm run coveralls
# Run a single test file
npx mocha test/index.js# Run ESLint
npx eslint .# Install dependencies
npm install- Main Entry:
index.js- Contains the PettyCache class and all public API methods - Test Suite:
test/index.js- Comprehensive Mocha test suite testing all cache operations, mutex, and semaphore functionality - Dependencies:
redis(v3.1.0) - Redis client for distributed cachingmemory-cache(v0.2.0) - In-memory cache for recently accessed dataasync(v3.2.6) - Async utility functionslock(v1.1.0) - Local locking mechanism
-
Two-Level Caching: Data is cached in memory for 2-5 seconds to reduce Redis calls, with Redis serving as the distributed cache layer.
-
Double-Checked Locking: Cache miss functions are wrapped in double-checked locking to prevent cache stampedes - ensuring the function is only executed once even with concurrent requests.
-
TTL Jitter: Default TTL is randomized between 30-60 seconds to prevent thundering herds when many keys expire simultaneously.
-
Distributed Locking:
- Mutex: Single distributed lock with retry capability
- Semaphore: Pool of distributed locks with ability to release or consume slots
- Cache Operations:
get,set,fetch,bulkGet,bulkSet,bulkFetch,patch,fetchAndRefresh - Mutex Operations:
mutex.lock,mutex.unlock - Semaphore Operations:
semaphore.retrieveOrCreate,semaphore.acquireLock,semaphore.releaseLock,semaphore.consumeLock,semaphore.expand,semaphore.reset
- Tests require a running Redis instance (automatically set up in CI via GitHub Actions)
- Test coverage is tracked via Coveralls
- ESLint is configured with specific rules in
eslint.config.js
GitHub Actions workflow (.github/workflows/test.yml) runs on push and pull requests:
- Sets up Node.js 22.17.0
- Installs dependencies
- Runs ESLint
- Sets up Redis 6.0.14
- Runs tests with coverage
- Sends Slack notifications on completion