Source Functions
Source functions are a powerful feature in RunCache that allow you to dynamically generate cache values. This guide explains how to use source functions effectively in your applications.
Understanding Source Functions
A source function is a function that generates a value for a cache entry. Instead of providing a static value when setting a cache entry, you provide a function that RunCache will call to generate the value when needed.
Key benefits of using source functions:
Lazy Loading: Values are only generated when actually needed
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
Basic Usage
To use a source function, provide it as the sourceFn
parameter when setting a cache entry:
When RunCache.get('user-profile')
is called for the first time, RunCache will:
Execute the source function
Store the returned value in the cache
Return the value to the caller
Subsequent calls to RunCache.get('user-profile')
will return the cached value without calling the source function again.
Synchronous vs. Asynchronous Source Functions
RunCache supports both synchronous and asynchronous source functions:
Synchronous Source Functions
Asynchronous Source Functions
Both types of functions are handled appropriately by RunCache.
Source Functions with TTL
You can combine source functions with TTL (Time-to-Live) to automatically invalidate cache entries after a certain period:
In this example:
The first call to
RunCache.get('weather-data')
will execute the source functionSubsequent calls within 30 minutes will return the cached value
After 30 minutes, the next call will execute the source function again
Source Functions with Automatic Refetching
For critical data that should always be available, combine source functions with automatic refetching:
With autoRefetch: true
:
When the TTL expires, the next
get()
call will return the stale data immediatelySimultaneously, RunCache will trigger a background refresh using the source function
Once the refresh completes, the cache is updated with fresh data
Manually Refreshing Source Function Values
You can manually trigger a refresh for any cache entry with a source function:
This will:
Execute the source function again
Update the cache with the new value
Reset the TTL timer (if applicable)
Error Handling in Source Functions
If a source function throws an error, RunCache handles it appropriately:
Initial Cache Miss
If the source function throws an error on initial cache miss:
The error is propagated to the caller
No cache entry is created
Refetch Failures
If the source function throws during an automatic refetch:
The stale value remains in the cache
A refetch failure event is triggered
The error doesn't propagate to the caller of
get()
Advanced Source Function Patterns
Parameterized Source Functions
You can create source functions that use parameters from the cache key:
Caching Function Results
You can create a wrapper to automatically cache function results:
Source Functions with Dependencies
You can create source functions that depend on other cached values:
Best Practices for Source Functions
1. Keep Source Functions Pure
Source functions should:
Have no side effects
Return the same result for the same inputs
Not depend on external state that might change
2. Handle Errors Properly
Always implement error handling in your source functions:
3. Return Stringified Data
Source functions should always return string values:
4. Optimize for Performance
Keep source functions efficient:
Fetch only the data you need
Use appropriate caching headers for API requests
Consider batching related requests
5. Use Appropriate TTL Values
Set TTL values based on:
How frequently the underlying data changes
How critical data freshness is
Resource constraints of your application
Next Steps
Now that you understand source functions, explore these related topics:
TTL and Expiration - Learn more about time-to-live functionality
Automatic Refetching - Dive deeper into background refresh functionality
Dependency Tracking - Learn about relationships between cache entries
Event System - Understand how to use events for cache monitoring
Last updated