Skip to content

Add purgeStorage to InMemoryStorage #162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

anullog
Copy link

@anullog anullog commented May 12, 2025

Adds a purgeStorage API to InMemoryStorage that allows clearing of all InMemoryKey directly through @Dependency(\.defaultInMemoryStorage).

@mbrandonw
Copy link
Member

Hi @anullog, we personally do not think this is the way to go because you may be purging the inMemory keys for other parts of your code base that do not want to be cleared. A better approach would be for you to keep track of which keys you are using and reset them manually.

@anullog
Copy link
Author

anullog commented May 13, 2025

Hi @mbrandonw, thanks for the insight!

Just to add a bit more context on the PR — we’re running into two main issues with our app:

  • We have a number of inMemory keys spread across different targets, and we’re looking for a centralized way to manage and modify those tracked keys, more specifically - have to clear all of them at the end of each session.
  • Ideally, we’d like to handle this in a type-agnostic way — either by resetting the specified keys to their default values (as defined in the SharedKey extension) or by simply removing the key:value pair from storage.

From what I can tell, the current API doesn’t support this directly. Would it make sense to update purgeStorage to something like purgeKeys, allowing us to remove only specific keys?

Happy to hear any alternative suggestions as well!

@stephencelis
Copy link
Member

@anullog That sounds like a more reasonable feature to us! We chatted about potential design and requirements, so if you'd like to take them on, here they are:

  • 3 new APIs on InMemoryStorage (I think the latter 2 can simply call the 1st):
    • removeValue<Value>(for key: InMemoryKey<Value>)
    • removeValue(for key: String)
    • removeValue<Value>(for key: InMemoryKey<Value>.Default)
  • Ensure that live @Shared and @SharedReader property wrappers are notified of this removal
    • Unit test that when removing a value for a key, the shared/reader's wrapped value goes back to the default
    • Unit test that an observed shared/reader triggers observation during removal

I believe this may require InMemoryStorage to go through the PersistentReferences storage, and if you run into any stumbling blocks or have any questions, let us know and we'll try to help unblock you!

- add removeValue test
- add removeValueShared test
- add limited APIs
@anullog
Copy link
Author

anullog commented May 14, 2025

Hi @stephencelis, thank you for providing the design and requirements!

So I've been playing around with this and made some progress.

That said, I’m pretty sure the way I’m going about it isn’t the right approach. Right now, I’m mostly trying to surface the limitations and highlight where I’m getting stuck - accessing InMemoryStorage through PersistentReferences without having these limiting APIs.

While the code and tests actually work, it feels clunky, but hopefully it shows where the pain points are.

If you have any suggestions or ideas on how to approach this better, I’d really appreciate it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants