TTL and Expiration
Time-to-Live (TTL) is a fundamental feature of RunCache that allows you to control how long cache entries remain valid. This guide explains how TTL works and how to effectively use it in your applications.
Understanding TTL
TTL (Time-to-Live) defines the lifespan of a cache entry in milliseconds. After the TTL period expires, the cache entry is considered stale and will be removed from the cache when accessed.
Key benefits of using TTL:
Data Freshness: Ensures cached data doesn't become too stale
Memory Management: Helps prevent the cache from growing indefinitely
Consistency: Provides a mechanism to periodically refresh data
Setting TTL
You can set TTL when creating a cache entry using the set
method:
Common TTL Values
Here are some common TTL values for reference:
How Expiration Works
When a cache entry's TTL expires:
The entry is not immediately removed from memory
The entry is marked as expired
When
get()
is called for that key, RunCache checks if it's expiredIf expired, the entry is removed and
get()
returnsundefined
(unlessautoRefetch
is enabled)
This lazy expiration approach is efficient as it only processes expired entries when they're accessed.
Checking Expiration Status
You can check if a key exists (and hasn't expired) using the has
method:
Expiry Events
RunCache provides an event system to monitor when cache entries expire:
These events can be useful for:
Logging and monitoring
Triggering background processes
Updating UI components when data becomes stale
TTL with Source Functions
When using source functions with TTL, you can create a powerful pattern for keeping cached data fresh:
In this example:
The first call to
RunCache.get('weather-data')
will fetch from the APISubsequent calls within 30 minutes will return the cached data
After 30 minutes, the next call will trigger a new API fetch
Automatic Refetching
For critical data that should always be available, you can combine TTL 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
Subsequent calls will get the new data
This pattern prevents cache stampedes and ensures users always get a response quickly.
Handling Refetch Failures
When using autoRefetch
, it's important to handle potential failures in the background refresh:
TTL Best Practices
Here are some best practices for effectively using TTL:
Match TTL to data volatility:
Use shorter TTL for frequently changing data
Use longer TTL for stable data
Consider the consequences of serving stale data
Use appropriate time units:
Define constants for common TTL values
Use clear variable names to indicate time units
Consider access patterns:
For frequently accessed data, longer TTL improves performance
For infrequently accessed data, shorter TTL keeps memory usage down
Implement staggered expiration:
Add small random variations to TTL to prevent mass expiration
Use
autoRefetch
for critical data:Ensures users always get a response
Prevents cache stampedes
Maintains data freshness in the background
Advanced TTL Scenarios
Sliding Expiration
RunCache doesn't directly support sliding expiration (extending TTL on access), but you can implement it manually:
Conditional Expiration
You can implement conditional expiration by checking certain conditions before refreshing:
Next Steps
Now that you understand TTL and expiration, explore these related topics:
Automatic Refetching - Learn more about background refresh functionality
Source Functions - Dive deeper into source functions
Event System - Understand how to use events for cache monitoring
Resource Management - Learn about memory management and cleanup
Last updated