RunCache provides a comprehensive event system that allows you to monitor and respond to various cache lifecycle events. This page provides a complete reference for all event types, event objects, and event handling methods.
Event Types
RunCache defines several event types in the EVENT enum:
Each event type has a corresponding event object with specific properties:
ExpiryEvent
Triggered when a cache entry expires.
interface ExpiryEvent {
key: string; // The key of the expired entry
ttl: number; // The TTL value in milliseconds
updatedAt: number; // Timestamp when the entry was last updated
metadata?: any; // Optional custom metadata associated with the entry
}
RefetchEvent
Triggered when a cache entry is refreshed.
interface RefetchEvent {
key: string; // The key of the refreshed entry
metadata?: any; // Optional custom metadata associated with the entry
}
RefetchFailureEvent
Triggered when a cache refresh operation fails.
interface RefetchFailureEvent {
key: string; // The key of the entry that failed to refresh
error: Error; // The error that occurred during refresh
metadata?: any; // Optional custom metadata associated with the entry
}
TagInvalidationEvent
Triggered when a cache entry is invalidated by tag.
interface TagInvalidationEvent {
key: string; // The key of the invalidated entry
tag: string; // The tag that caused the invalidation
metadata?: any; // Optional custom metadata associated with the entry
}
DependencyInvalidationEvent
Triggered when a cache entry is invalidated due to a dependency.
interface DependencyInvalidationEvent {
key: string; // The key of the invalidated entry
dependencyKey: string; // The key of the dependency that caused the invalidation
metadata?: any; // Optional custom metadata associated with the entry
}
Global Event Listeners
Global event listeners are triggered for all cache entries that match the specified event type.
callback: Function to call when matching entries are refreshed
Example:
// Specific key
RunCache.onKeyRefetch("weather-data", (event) => {
console.log(`Weather data was refreshed`);
});
// Pattern matching
RunCache.onKeyRefetch("api:*:data", (event) => {
console.log(`API data ${event.key} was refreshed`);
});
onKeyRefetchFailure
Registers a callback for refetch failure events for a specific key or pattern.
callback: Function to call when matching entries are invalidated by dependency
Example:
// Specific key
RunCache.onKeyDependencyInvalidation("dashboard:main", (event) => {
console.log(`Main dashboard was invalidated due to dependency on: ${event.dependencyKey}`);
});
// Pattern matching
RunCache.onKeyDependencyInvalidation("dashboard:*", (event) => {
console.log(`Dashboard ${event.key} was invalidated due to dependency on: ${event.dependencyKey}`);
});
Managing Event Listeners
clearEventListeners
Removes event listeners based on the specified options.
Use specific key patterns to minimize unnecessary event processing:
// Too broad - triggers for all keys
RunCache.onKeyExpiry('*', (event) => {
// This will run for EVERY expiry
});
// Better - more specific pattern
RunCache.onKeyExpiry('api:*', (event) => {
// This will only run for API-related expirations
});
4. Clean Up Event Listeners
Remove event listeners when they're no longer needed:
// In a component or module initialization
function initializeModule() {
// Set up event listeners
RunCache.onKeyRefetch('module-data', handleRefetch);
// Return cleanup function
return () => {
RunCache.clearEventListeners({
event: EVENT.REFETCH,
key: 'module-data'
});
};
}
// Later, when shutting down
const cleanup = initializeModule();
// When module is unloaded
cleanup();