net.i2p.router.message

## Class OutboundCache

- java.lang.Object
- - net.i2p.router.message.OutboundCache

-     public class OutboundCache
      extends Object

  Helper cache manager for outbound message optimization in OCMOSJ.

  This class maintains several caches to optimize outbound message routing and delivery:

  - LeaseSet cache - minimizes overhead by reusing our own LeaseSet.
  - Lease cache - persists sending to the same lease to reduce out-of-order delivery.
  - Tunnel caches - caches outbound tunnels per destination to stabilize delivery paths.
  - Last reply request cache - ensures periodic replies to detect failed tunnels.
  - Multihomed cache - tracks LeaseSets for multihomed routers.

  All caches use ConcurrentHashMap with tuned initial capacity, load factor, and concurrency level to allow safe concurrent access and high throughput with low contention. Tunnel caches have been converted from HashMaps with explicit synchronization to ConcurrentHashMaps for consistent concurrency management without external locking.

  Cache cleaning occurs periodically with a configurable interval, removing expired entries to balance memory use and cache freshness.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `(package private) static class` | `OutboundCache.HashPair` Composite key for caches, combining source and destination Hashes.

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `(package private) ConcurrentHashMap<OutboundCache.HashPair,TunnelInfo>` | `backloggedTunnelCache` Cache for backlogged tunnels per source-destination pair.
    `(package private) ConcurrentHashMap<OutboundCache.HashPair,Long>` | `lastReplyRequestCache` Cache to track the last reply request time per source-destination pair.
    `(package private) ConcurrentHashMap<OutboundCache.HashPair,Lease>` | `leaseCache` Lease cache keyed by source-destination pairs.
    `(package private) ConcurrentHashMap<OutboundCache.HashPair,LeaseSet>` | `leaseSetCache` LeaseSet cache keyed by source-destination pairs.
    `ConcurrentHashMap<Hash,LeaseSet>` | `multihomedCache` Cache for LeaseSets associated with multihomed routers or hosted destinations.
    `(package private) ConcurrentHashMap<OutboundCache.HashPair,TunnelInfo>` | `tunnelCache` Cache for outbound tunnels per source-destination pair.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `OutboundCache(RouterContext ctx)` Creates an OutboundCache instance and schedules periodic cache cleanup.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `clearAllCaches()` Clears all caches managed by this instance.
    `(package private) void` | `clearCaches(OutboundCache.HashPair hashPair, Lease lease, TunnelInfo inTunnel, TunnelInfo outTunnel)` Clears cache entries related to the provided key and elements upon failure, allowing retries with fresh associations.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### backloggedTunnelCache

          final ConcurrentHashMap<OutboundCache.HashPair,TunnelInfo> backloggedTunnelCache

      Cache for backlogged tunnels per source-destination pair. Also uses ConcurrentHashMap for consistency and concurrency.

    - #### lastReplyRequestCache

          final ConcurrentHashMap<OutboundCache.HashPair,Long> lastReplyRequestCache

      Cache to track the last reply request time per source-destination pair. Used to detect and recover from failed tunnels.

    - #### leaseCache

          final ConcurrentHashMap<OutboundCache.HashPair,Lease> leaseCache

      Lease cache keyed by source-destination pairs. Ensures persistent use of the same inbound lease to minimize out-of-order delivery.

    - #### leaseSetCache

          final ConcurrentHashMap<OutboundCache.HashPair,LeaseSet> leaseSetCache

      LeaseSet cache keyed by source-destination pairs. Controls when to bundle our own LeaseSet to reduce overhead on repeated communications.

    - #### multihomedCache

          public final ConcurrentHashMap<Hash,LeaseSet> multihomedCache

      Cache for LeaseSets associated with multihomed routers or hosted destinations.

    - #### tunnelCache

          final ConcurrentHashMap<OutboundCache.HashPair,TunnelInfo> tunnelCache

      Cache for outbound tunnels per source-destination pair. Maintains consistent outbound tunnel usage to optimize streaming performance. Uses ConcurrentHashMap for thread safety and high concurrency.

  - ### Constructor Detail

    - #### OutboundCache

          public OutboundCache(RouterContext ctx)

      Creates an OutboundCache instance and schedules periodic cache cleanup.
      Parameters:
        `ctx` - the router context providing environment and utilities.

  - ### Method Detail

    - #### clearAllCaches

          public void clearAllCaches()

      Clears all caches managed by this instance. Use cautiously as this impacts performance temporarily.

    - #### clearCaches

          void clearCaches(OutboundCache.HashPair hashPair,
                           Lease lease,
                           TunnelInfo inTunnel,
                           TunnelInfo outTunnel)

      Clears cache entries related to the provided key and elements upon failure, allowing retries with fresh associations.
      Parameters:
        `hashPair` - the source-destination key for caches.
        `lease` - lease to clear from leaseCache, may be null.
        `inTunnel` - inbound tunnel to check for leaseSetCache invalidation, may be null.
        `outTunnel` - outbound tunnel to clear from tunnel caches, may be null.