Registry / storage / redis-value-cache

redis-value-cache

JSON →
library1.1.0jsnpmunverified

An in-memory cache backed by Redis with pub/sub updates, version 1.1.0. Designed for applications needing low-latency access to Redis-stored data with real-time invalidation. Uses an LRU cache internally, supports key generation and deserialization functions, and offers multiple error handling and update strategies. Unlike simple Redis caching, it subscribes to channels to automatically update or evict cached items upon data changes, reducing server load and ensuring freshness. Suitable for TypeScript projects, ships types, and requires a Redis server for reading and pub/sub.

npm install redis-value-cache
INSTALL
IMPORT
SIG · REDIS-VALUE-CACHE
R
redis-value-cache
storagejavascriptv1.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.

RedisValueCache
import { RedisValueCache } from 'redis-value-cache'
import RedisValueCache from 'redis-value-cache'
Named export only, not default. ESM and TypeScript compatible.
RedisValueCacheOptions
import { RedisValueCacheOptions } from 'redis-value-cache'
TypeScript type for constructor options. Also importable via this path.
RedisGetOpts
import { RedisGetOpts } from 'redis-value-cache'
Type for specifying GET, HGET, or HGETALL retrieval strategy, used in genKeyFromMsg return value.
ErrorHandlerStrategy
import { ErrorHandlerStrategy } from 'redis-value-cache'
String union type for error handling options: 'emit' | 'warn' | 'throw' | 'ignore'.

Creates a RedisValueCache with a shared Redis client, subscribes to pattern 'cache:*', gets values via GET, and deserializes JSON.

import { RedisValueCache } from 'redis-value-cache'; import { createClient } from 'redis'; const client = createClient({ url: process.env.REDIS_URL ?? 'redis://localhost:6379' }); await client.connect(); const cache = new RedisValueCache({ redis: { client, // reuse for subscriber and client channelOpts: { pattern: 'cache:*' }, getOpts: { type: 'GET' } }, genKeyFromMsg: (message) => message, deserialize: (data: string | null) => data ? JSON.parse(data) : undefined, cacheMaxSize: 500, onMessageStrategy: 'drop', freeze: true }); await cache.init(); // subscribe to Redis const value = await cache.get('user:123'); console.log(value); await cache.close();
Debug
Known issues
breakingValues must not be null or undefined. Cache will ignore such values silently.
fix
Ensure deserialize never returns null/undefined; handle in fallbackFetchMethod if needed.
affects: >=1.0.0
gotchafreeze: true by default. Mutating cached objects throws in strict mode or silently fails.
fix
Set freeze: false if you need to mutate cached objects.
affects: >=1.0.0
gotchaErrors in genKeyFromMsg or deserialize are not thrown by default; they are handled by errorHandlerStrategy (default 'warn').
fix
Set errorHandlerStrategy: 'throw' during development to catch errors early.
affects: >=1.0.0
deprecatedThe 'fallbackFetchMethod' option is experimental and may change in minor versions.
fix
Use with caution; test upgrade paths when releasing new versions.
affects: >=1.0.0 <2.0.0
breakinginit() must be called before get(). Calling get() before init() may hang or fail.
fix
Always await cache.init() before any get() calls.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: client.duplicate is not a function
You passed a client object that doesn't have duplicate() method (e.g., older Redis client or mock).
fix
Provide a proper ioredis or node-redis client, or supply redis.clientOpts instead of a raw client.
Error: The subscribed channel is not active
RedisValueCache expects the client to be connected before subscribing, but it wasn't initialized properly.
fix
Await client.connect() before passing to RedisValueCache, or pass redis.clientOpts to let the library handle connection.
RedisValueCache: message received but genKeyFromMsg returned undefined
genKeyFromMsg function returned undefined, so the message was ignored without updating cache.
fix
Ensure genKeyFromMsg always returns a string key; log or handle cases where it might return undefined.
RedisValueCache: error during fetch, ignoring: TimeoutError
The errorHandlerStrategy is set to 'ignore' (default 'warn' if not set) and a Redis timeout occurred.
fix
Set errorHandlerStrategy: 'throw' or 'emit' to handle errors, or increase Redis timeout.
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies
redisrequiredPeer dependency for Redis client operations (subscribe, GET, HGET, HGETALL). Required at runtime.
Agent activity
26 hits · last 30 days
node
24
OpenAI (training)
1
Resources
redis-value-cache — npm install redis-value-cache · libregistry