net.i2p.util

## Class TryCache<T>

- java.lang.Object
- - net.i2p.util.TryCache<T>

- Type Parameters:
    `T` - type of objects cached
  Direct Known Subclasses:
    ByteCache

      public class TryCache<T>
      extends Object

  A thread-safe, lock-free-ish object cache that reuses objects to reduce allocation overhead. Designed to avoid object loss due to lock contention.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static interface` | `TryCache.ObjectFactory<T>` Factory interface for creating new objects in TryCache.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `TryCache(TryCache.ObjectFactory<T> factory, int capacity)`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `T` | `acquire()` Acquire an object from the cache or create a new one.
    `void` | `clear()` Clear all cached items.
    `long` | `getLastUnderflowTime()`
    `void` | `release(T item)` Try to return the item to the cache.
    `void` | `shrink(int targetSize)`
    `int` | `size()`
    `boolean` | `wasUnderfilled(long thresholdMillis)`

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

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

- - ### Constructor Detail

    - #### TryCache

          public TryCache(TryCache.ObjectFactory<T> factory,
                          int capacity)

  - ### Method Detail

    - #### acquire

          public T acquire()

      Acquire an object from the cache or create a new one.

    - #### clear

          public void clear()

      Clear all cached items. This is best-effort; some may still be in flight.

    - #### getLastUnderflowTime

          public long getLastUnderflowTime()

    - #### release

          public void release(T item)

      Try to return the item to the cache. If the cache is full, the item is discarded.

    - #### shrink

          public void shrink(int targetSize)

    - #### size

          public int size()

    - #### wasUnderfilled

          public boolean wasUnderfilled(long thresholdMillis)