Basic Usage
This guide covers the fundamental operations and patterns for using RunCache in your applications. After reading this guide, you'll understand how to perform basic caching operations and integrate RunCache into your projects.
Importing the Library
First, import RunCache in your JavaScript or TypeScript file:
Basic Cache Operations
Setting Cache Values
The most basic operation is storing a value in the cache:
When working with objects or complex data, you need to stringify them first:
Getting Cache Values
To retrieve values from the cache:
Checking if a Key Exists
To check if a key exists in the cache (and hasn't expired):
Deleting Cache Entries
To remove a specific entry from the cache:
Clearing the Entire Cache
To remove all entries from the cache:
Working with Time-to-Live (TTL)
You can set an expiration time for cache entries using the ttl
parameter (in milliseconds):
After the TTL period, the value will be automatically removed from the cache when accessed.
Using Source Functions
Source functions allow you to generate cache values dynamically. This is particularly useful for API calls or expensive computations:
Benefits of Source Functions
Using source functions provides several advantages:
Lazy Loading: The function is only called when the value is first requested
Automatic Serialization: No need to manually stringify the result
Error Handling: RunCache handles errors from the source function
Automatic Refresh: Can be combined with TTL and autoRefetch
Automatic Refetching
You can configure cache entries to automatically refresh when they expire:
With autoRefetch: true
, when the TTL expires, RunCache will:
Return the stale value to the caller immediately
Trigger a background refresh using the source function
Update the cache with the new value
This pattern prevents cache stampedes and ensures users always get a response quickly.
Working with Patterns
RunCache supports wildcard patterns for operating on multiple related cache keys:
Patterns are useful for:
Batch operations on related cache entries
Implementing hierarchical cache structures
Organizing cache entries by domain or entity type
Error Handling
RunCache operations can throw errors in certain situations:
Common error scenarios include:
Empty or invalid keys
Missing required parameters
Source function errors
Storage adapter failures
Basic Configuration
Configure RunCache with global settings:
Configuration options include:
maxEntries
: Maximum number of entries before evictionevictionPolicy
: Strategy for removing entries when maxEntries is reacheddebug
: Enable detailed logging for debuggingstorageAdapter
: Configure persistent storage
Practical Example: API Caching
Here's a practical example of using RunCache to cache API responses:
This pattern is commonly used to:
Reduce API calls and improve performance
Decrease load on backend services
Improve user experience with faster response times
Handle intermittent API failures gracefully
Next Steps
Now that you understand RunCache, you can explore more advanced features:
TTL and Expiration - Learn more about time-to-live functionality
Source Functions - Dive deeper into source functions
Eviction Policies - Configure how cache entries are evicted
Pattern Matching - Master wildcard patterns
Last updated