dreymonde/Shallows

Purge from memoryCache (while leaving in composed diskCache)?

Closed this issue · 10 comments

Just curious whether there's a simple way (that I haven't seen) to remove an item from a memoryCache, while leaving it in the diskCache (I have a disk+memory composition). I want to be able to free up memory (on iOS), but retain the option to quickly reload from diskCache.

Thanks in advance.

Hi! There was a very similar issue: #7
Check it out and ping me if you’ll still have any questions after it!

Ah, okay, didn't see that. Thanks.
So, if I wanted to clear a single item, I'm guessing it might be something like: memoryCache.storage[<myKey>] = nil
Yes?

@jbmaxwell yes, that’s right!

Perfect. Super simple. Thanks.

Hmm, has something changed? I can't seem to get .storage at all... I only see asStorage(), but that doesn't support subscripting. I'd actually like to add a function to delete items from both disk and memory storage (in a combined disk+memory storage). How would I do that?

storage exists only for MemoryStorage instances. For DiskStorage, I’m afraid, you’ll need to provide your own solution

Ah, okay. And I suppose a DiskStorage combined with a MemoryStorage is fundamentally a DiskStorage? (Because my combined MemoryStorage also doesn't have .storage.):

let myMemoryCache = MemoryStorage<Filename, Chord>()
        .combined(with: DiskStorage.main.folder("stuff", in: .cachesDirectory).mapJSONObject(Chord.self))

This also doesn't recognize .storage.

When you perform any kind of transformation (mapping, combining, etc.), the original type information is lost. When you combine MemoryStorage with DiskStorage, you get just abstract Storage<Key, Value>

Check #7 for how to deal with this in certain scenarios

Okay, I can see what you're doing in MemoryStorage, but I don't understand exactly how I'd do something similar for DiskStorage (or I suppose, for Storage)—your code is beautiful, but terse.
I'll figure out a workaround... Would it work to just remove the disk item, as in DiskStorage's clear() function?
UPDATE: Deleting using fileURL() works for the DiskStorage but, as I expected, the MemoryStorage still has the item... Is there a way to refresh the MemoryStorage, based on the DiskStorage? Or can I get a reference to the MemoryStorage only from a combined Storage? (Wouldn't it be reasonable to have a built-in function to remove items?)