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 →
StrategyBest ForUse CasePredictabilityInfrastructure Control
Simple CachePerformance optimizationWrapping expensive strategies like FastestPingMediumMedium
Fastest PingReal-time applicationsPerformance-critical apps, gamingMediumLow
Preferred + FallbackDedicated infrastructureCDN with origin fallback, enterprise gatewaysHighMaximum
Round RobinLoad balancingEven distribution across known gatewaysHighHigh
RandomSimple distributionBasic load spreading, testingLowMedium
← Swipe to see more →

How It Works

  1. Check Cache: Look for a cached result from previous strategy calls
  2. Cache Hit: Return cached gateway if still valid (within TTL)
  3. Cache Miss: Call the wrapped strategy to get a new gateway
  4. Store Result: Cache the new gateway with timestamp
  5. 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

ParameterTypeDefaultDescription
strategyRoutingStrategyrequiredThe routing strategy to wrap and cache
cacheTimeMsnumber30000How long to cache results in milliseconds
loggerLoggerdefaultLoggerOptional 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

  1. Choose Appropriate Cache Time: Balance between performance and freshness
  2. Wrap Expensive Strategies: Use with strategies that perform network calls
  3. Monitor Cache Hit Rates: Track how often cache is used vs. strategy calls
  4. Consider Cache Invalidation: Plan for scenarios where cache should be cleared
  5. 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.

Was this page helpful?