CacheStrategy

public enum CacheStrategy

Defines the caching strategy for storage operations.

The cache strategy determines how StorageDelegate manages its in-memory cache in relation to the underlying storage. Each strategy offers different trade-offs between performance, consistency, and resilience.

Available Strategies

CACHE

Priority: Performance

Items are cached in memory when saved. Reads are served from cache when available, providing the fastest possible access.

Use when: Performance is critical and you can tolerate cache-storage inconsistencies on save failures.

NO_CACHE

Priority: Consistency

No caching layer is used. All operations go directly to the underlying storage, ensuring you always have the most recent data from the source of truth.

Use when: You always need fresh data or memory usage is a concern.

CACHE_ON_FAILURE

Priority: Resilience

Cache serves as a fallback mechanism when storage operations fail. On successful operations, fresh data is fetched from storage and cached. Cache is used only when storage is unavailable.

Use when: You need resilience against intermittent failures and can tolerate stale data.

Choosing a Strategy

Use this decision tree:

  1. Is storage reliable and always available?

    • Yes → Consider NO_CACHE or CACHE
    • No → Use CACHE_ON_FAILURE
  2. Is read performance critical?

    • Yes → Use CACHE
    • No → Consider NO_CACHE or CACHE_ON_FAILURE
  3. Must data always be fresh?

    • Yes → Use NO_CACHE
    • No → Use CACHE or CACHE_ON_FAILURE

Thread Safety

All cache strategies are thread-safe through Swift actor isolation.

See also

StorageDelegate
  • Always cache items in memory for fast repeated access.

    Reads are served from cache when available. Writes populate both cache and storage. On save failure, the item remains cached.

    Best for: Frequently read data where performance is critical.

    Declaration

    Swift

    case CACHE
  • Never cache items in memory. All operations go directly to storage.

    Ensures data is always fresh from storage at the cost of performance.

    Best for: Data that changes frequently or memory-constrained environments.

    Declaration

    Swift

    case NO_CACHE
  • Cache items as a fallback for storage failures.

    On successful operations, fresh data is fetched from storage and cached. Cache serves as a safety net during failures.

    Best for: Network storage or scenarios with intermittent failures.

    Declaration

    Swift

    case CACHE_ON_FAILURE