Registry / database / co-cache

co-cache

JSON →
library4.1.0jsnpmunverified

A middleware-like caching library for async functions (including AsyncFunction and Promise-based) that caches results in Redis using ioredis. Version 4.1.0 is stable, with regular updates. It wraps any async function, caching its return value based on a configurable key (defaults to function name) and TTL. Supports custom get/set logic, conditional caching, manual cache management (get, set, clear, raw), and automatic serialization via JSON. Differentiators: simple API, first-class ioredis support, and fine-grained cache control per function invocation.

npm install co-cache
INSTALL
IMPORT
SIG · CO-CACHE
C
co-cache
databasejavascriptv4.1.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import cache from 'co-cache'
const cache = require('co-cache')(); // missing initial config
ESM import is fine; CommonJS require works but must call the exported function immediately.
default
const cache = require('co-cache')(defaultConfig);
const cache = require('co-cache'); // forgot to call the factory with config
The module exports a factory function that takes defaultConfig and returns a wrapper.

Shows basic setup with ioredis, wrapping an async function, cached vs raw execution.

import cache from 'co-cache'; import Redis from 'ioredis'; const redis = new Redis(); const myCache = cache({ client: redis, prefix: 'myapp:', expire: 60000 // 60s }); const expensiveFn = myCache(async function expensive(n) { console.log('Computing...'); return n * 2; }, { key: (n) => `double:${n}` }); await expensiveFn(5); // logs "Computing..." -> returns 10 await expensiveFn(5); // reads from cache, no log await expensiveFn.raw(5); // skips cache, logs again
Debug
Known issues
gotchaDefault key uses fn.name. Anonymous functions or functions with same name can cause collisions.
fix
Always provide a custom key option that ensures uniqueness.
affects: >=4.0.0
gotchaSetting expire to 0 or null caches indefinitely. No cache invalidation unless manually cleared or TTL expires.
fix
Always define a TTL (expire in ms) to avoid unbounded memory usage.
affects: *
deprecatedThe cache function does not support Proxy or advanced cache keys like template strings in older versions.
fix
Upgrade to 4.x and provide functions for dynamic keys.
affects: <4.0.0
gotchaReturning false from key function skips caching entirely. If you intend to cache but use a falsy key, it will be skipped.
fix
Ensure key function returns a string or if you want to skip, return false explicitly.
affects: *
Errors
Common errors & fixes
TypeError: cache is not a function
Using require('co-cache') without calling it as a factory.
fix
const cache = require('co-cache')({ client: redis });
Redis connection failure: connect ECONNREFUSED 127.0.0.1:6379
ioredis client not connected or Redis server not running.
fix
Start Redis server or use Redis client with correct host/port.
ReferenceError: fetch is not defined
Using co-cache in a browser environment that lacks node:buffer and net modules required by ioredis.
fix
Use co-cache only in Node.js environment with a proper Redis connection.
Upgrade
Version history
4.1.0latest on npm
Audit
Dependencies
ioredisrequiredPeer dependency: the library requires an ioredis client instance to connect to Redis.
Agent activity
11 hits · last 30 days
node
10
OpenAI (training)
1
Resources
co-cache — npm install co-cache · libregistry