Skip to content

Feature: Implement Low Memory Cache Invalidation  #2

Description

@brunogama

Goal: Automatically clear memoization caches when the operating system signals low memory pressure to potentially free up resources.

Proposed Implementation (Initial focus on Apple Platforms):

  1. System Notification: Use NotificationCenter to observe system memory warnings (e.g., UIApplication.didReceiveMemoryWarningNotification on iOS/tvOS, investigate equivalents or related mechanisms like memory pressure dispatch sources on macOS).
  2. Central Observer: Implement a central entity (e.g., a singleton or shared manager within the Memoization library) responsible for listening to these system notifications.
  3. Storage Registry: Create a mechanism (e.g., a WeakSet or similar weak collection) for active MemoizeStorage instances to register themselves with the central observer upon initialization and de-register upon deinitialization.
  4. Clear Action: When the central observer receives a low memory notification, it iterates through its registry of active storage instances and calls their clear() method.

Considerations:

  • Platform Abstraction: How to handle this for non-Apple platforms if broader support is desired? May require #if os(...) checks.
  • Granularity: Initial implementation can clear all registered caches. Future enhancements could consider more granular clearing based on priority or least-recently-used, but this adds complexity.

Reference: Conversation with Copilot on 2025-04-27.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions