> For the complete documentation index, see [llms.txt](https://runcache.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://runcache.gitbook.io/docs/next/core-features/ttl-expiration.md). Agents can also search and read this documentation through its [MCP server](https://runcache.gitbook.io/docs/~gitbook/mcp).

# 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:

```typescript
await RunCache.set({
  key: 'user-profile',
  value: { name: 'John Doe' },
  ttl: 3600000 // 1 hour in milliseconds
});
```

### Common TTL Values

Here are some common TTL values for reference:

```typescript
const TTL = {
  MINUTE: 60 * 1000,         // 60,000 ms
  HOUR: 60 * 60 * 1000,      // 3,600,000 ms
  DAY: 24 * 60 * 60 * 1000,  // 86,400,000 ms
  WEEK: 7 * 24 * 60 * 60 * 1000 // 604,800,000 ms
};

// Examples
await RunCache.set({ key: 'frequently-updated', value: 'data', ttl: TTL.MINUTE });
await RunCache.set({ key: 'daily-stats', value: 'statistics', ttl: TTL.DAY });
```

### Valid TTL Values

`ttl` must be a finite, positive number of milliseconds. `set()` throws for `0`, negative values, `NaN`, `Infinity` and non-numbers. To cache an entry that never expires, leave `ttl` out.

There is no upper limit. Long TTLs, including ones beyond the roughly 24.8-day maximum delay of `setTimeout`, expire at the right time:

```typescript
// Expires in 90 days
await RunCache.set({ key: 'quarterly-report', value: 'report', ttl: 90 * TTL.DAY });
```

## How Expiration Works

Each entry with a TTL has a timer. When the TTL elapses:

1. An `expire` event is emitted
2. What happens next depends on the entry:
   * **Plain values** (no `sourceFn`) are removed from memory immediately, whether or not they are ever read again. They no longer count towards `maxEntries`.
   * **Entries with `autoRefetch`** are refreshed in the background. Until the refresh completes, `get()` returns the previous (stale) value.
   * **Entries with a `sourceFn` but without `autoRefetch`** stay in memory so that you can still call `refetch(key)`, for example from an expiry listener. A `get()` or `has()` on such an entry returns `undefined`/`false` and removes it.

Expired entries are never served by `get()` (except the stale value of an auto-refetched entry described above), and they are not written to storage by `saveToStorage()`.

In Node.js the timers are `unref()`'d, so pending TTLs never keep a process from exiting on its own.

## Checking Expiration Status

You can check if a key exists (and hasn't expired) using the `has` method:

```typescript
const isValid = await RunCache.has('user-profile');
if (isValid) {
  console.log('The user profile is still valid');
} else {
  console.log('The user profile has expired or doesn\'t exist');
}
```

## Expiry Events

RunCache provides an event system to monitor when cache entries expire:

```typescript
// Global expiry event
await RunCache.onExpiry((event) => {
  console.log(`Cache key ${event.key} expired at ${new Date(event.updatedAt + event.ttl).toISOString()}`);
});

// Specific key expiry
await RunCache.onKeyExpiry("api-data", (event) => {
  console.log(`API data cache expired`);
});

// Pattern-based expiry events
await RunCache.onKeyExpiry("user-*", (event) => {
  console.log(`User cache ${event.key} expired`);
});
```

Each expiry is reported once, with the entry's stored `value` and its `ttl`. The event fires when the TTL timer elapses. If a `get()` or `has()` call notices the expiry first, the event fires then instead, and it is not repeated. Entries that are overwritten or deleted before their TTL elapses do not emit an expiry event.

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:

```typescript
await RunCache.set({
  key: 'weather-data',
  sourceFn: async () => {
    const response = await fetch('https://api.weather.com/current');
    const data = await response.json();
    return data;
  },
  ttl: 30 * 60 * 1000 // 30 minutes
});
```

In this example:

1. The first call to `RunCache.get('weather-data')` will fetch from the API
2. Subsequent calls within 30 minutes will return the cached data
3. 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:

```typescript
await RunCache.set({
  key: 'critical-stats',
  sourceFn: async () => {
    const response = await fetch('https://api.example.com/stats');
    const data = await response.json();
    return data;
  },
  ttl: 5 * 60 * 1000, // 5 minutes
  autoRefetch: true
});
```

With `autoRefetch: true`:

1. When the TTL expires, the next `get()` call will return the stale data immediately
2. Simultaneously, RunCache will trigger a background refresh using the source function
3. Once the refresh completes, the cache is updated with fresh data
4. 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:

```typescript
// Global refetch failure event
await RunCache.onRefetchFailure((event) => {
  console.error(`Failed to refresh cache key ${event.key}: ${event.error}`);
});

// Specific key refetch failure
await RunCache.onKeyRefetchFailure("critical-stats", (event) => {
  console.error(`Critical stats refresh failed: ${event.error}`);

  // Implement retry logic or fallback mechanism
  setTimeout(() => RunCache.refetch("critical-stats"), 30000); // Retry after 30 seconds
});
```

## TTL Best Practices

Here are some best practices for effectively using TTL:

1. **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
2. **Use appropriate time units**:
   * Define constants for common TTL values
   * Use clear variable names to indicate time units
3. **Consider access patterns**:
   * For frequently accessed data, longer TTL improves performance
   * For infrequently accessed data, shorter TTL keeps memory usage down
4. **Implement staggered expiration**:

   * Add small random variations to TTL to prevent mass expiration

   ```typescript
   const baseTTL = 3600000; // 1 hour
   const jitter = Math.floor(Math.random() * 300000); // 0-5 minutes
   await RunCache.set({ key: 'data', value: 'value', ttl: baseTTL + jitter });
   ```
5. **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:

```typescript
async function getWithSlidingExpiration(key, slidingTTL) {
  const value = await RunCache.get(key);
  if (value) {
    // Reset the TTL by setting the same value again
    await RunCache.set({ key, value, ttl: slidingTTL });
  }
  return value;
}

// Usage
const userData = await getWithSlidingExpiration('user:1', 3600000);
```

### Conditional Expiration

You can implement conditional expiration by checking certain conditions before refreshing:

```typescript
async function getWithConditionalRefresh(key) {
  const value = await RunCache.get(key);

  // If value exists but some condition requires refresh
  if (value !== undefined && shouldRefreshData(value)) {
    await RunCache.refetch(key);
    return await RunCache.get(key);
  }

  return value;
}

function shouldRefreshData(data) {
  // Custom logic to determine if data needs refresh
  return data.version < CURRENT_VERSION;
}
```

## Next Steps

Now that you understand TTL and expiration, explore these related topics:

* [Automatic Refetching](https://runcache.gitbook.io/docs/next/core-features/auto-refetch.md) - Learn more about background refresh functionality
* [Source Functions](https://runcache.gitbook.io/docs/next/core-features/source-functions.md) - Dive deeper into source functions
* [Event System](https://runcache.gitbook.io/docs/next/advanced-features/event-system.md) - Understand how to use events for cache monitoring
* [Resource Management](https://runcache.gitbook.io/docs/next/advanced-features/resource-management.md) - Learn about memory management and cleanup
