SimpleCacheRoutingStrategy
Overview
The SimpleCacheRoutingStrategy
wraps another routing strategy and caches its results for a specified duration. This improves performance by avoiding repeated expensive operations like network pings or complex calculations.
Routing Strategy Comparison
← Swipe to see more →
Strategy | Best For | Use Case | Predictability | Infrastructure Control |
---|---|---|---|---|
Simple Cache | Performance optimization | Wrapping expensive strategies like FastestPing | Medium | Medium |
Fastest Ping | Real-time applications | Performance-critical apps, gaming | Medium | Low |
Preferred + Fallback | Dedicated infrastructure | CDN with origin fallback, enterprise gateways | High | Maximum |
Round Robin | Load balancing | Even distribution across known gateways | High | High |
Random | Simple distribution | Basic load spreading, testing | Low | Medium |
← Swipe to see more →
How It Works
- Check Cache: Look for a cached result from previous strategy calls
- Cache Hit: Return cached gateway if still valid (within TTL)
- Cache Miss: Call the wrapped strategy to get a new gateway
- Store Result: Cache the new gateway with timestamp
- Return Gateway: Return the selected gateway
Configuration
Basic Usage
import {
SimpleCacheRoutingStrategy,
FastestPingRoutingStrategy,
} from '@ar.io/wayfinder-core'
const strategy = new SimpleCacheRoutingStrategy({
strategy: new FastestPingRoutingStrategy({ timeoutMs: 500 }),
cacheTimeMs: 30000, // Cache for 30 seconds
})
With Different Strategies
import {
SimpleCacheRoutingStrategy,
PreferredWithFallbackRoutingStrategy,
RandomRoutingStrategy,
} from '@ar.io/wayfinder-core'
// Cache a preferred with fallback strategy
const cachedStrategy = new SimpleCacheRoutingStrategy({
strategy: new PreferredWithFallbackRoutingStrategy({
preferredGateway: 'https://my-gateway.com',
fallbackStrategy: new RandomRoutingStrategy(),
}),
cacheTimeMs: 60000, // Cache for 1 minute
})
Parameters
Parameter | Type | Default | Description |
---|---|---|---|
strategy | RoutingStrategy | required | The routing strategy to wrap and cache |
cacheTimeMs | number | 30000 | How long to cache results in milliseconds |
logger | Logger | defaultLogger | Optional logger instance |
Integration with Wayfinder
import {
Wayfinder,
SimpleCacheRoutingStrategy,
FastestPingRoutingStrategy,
} from '@ar.io/wayfinder-core'
const wayfinder = new Wayfinder({
routingSettings: {
strategy: new SimpleCacheRoutingStrategy({
strategy: new FastestPingRoutingStrategy({ timeoutMs: 500 }),
cacheTimeMs: 30000,
}),
},
})
Use Cases
Caching Expensive Ping Operations
Perfect for wrapping FastestPingRoutingStrategy to avoid repeated network calls.
const cachedPingStrategy = new SimpleCacheRoutingStrategy({
strategy: new FastestPingRoutingStrategy({ timeoutMs: 1000 }),
cacheTimeMs: 30000, // Cache ping results for 30 seconds
})
Reducing Complex Calculations
Ideal for strategies that perform complex calculations or multiple network requests.
const cachedComplexStrategy = new SimpleCacheRoutingStrategy({
strategy: new PreferredWithFallbackRoutingStrategy({
preferredGateway: 'https://primary.com',
fallbackStrategy: new FastestPingRoutingStrategy({ timeoutMs: 500 }),
}),
cacheTimeMs: 60000, // Cache for 1 minute
})
Development and Testing
Useful for consistent behavior during development and testing.
const testStrategy = new SimpleCacheRoutingStrategy({
strategy: new RandomRoutingStrategy({ seed: 42 }),
cacheTimeMs: 300000, // Cache for 5 minutes during testing
})
Best Practices
- Choose Appropriate Cache Time: Balance between performance and freshness
- Wrap Expensive Strategies: Use with strategies that perform network calls
- Monitor Cache Hit Rates: Track how often cache is used vs. strategy calls
- Consider Cache Invalidation: Plan for scenarios where cache should be cleared
- Test Cache Behavior: Verify caching works correctly in your use cases
The SimpleCacheRoutingStrategy
is ideal for improving performance by caching results from expensive routing strategies.