Cross Platform Object Cache

Hello,
I'm a bit new to Xamarin/.Net, most of my mobile development experience is with iOS.
I'm looking for a pre-existing cross platform class for cacheing objects in memory, something similar to the iOS NSCache class. For those who aren't familiar with this class it's essentially a dictionary with ties into the OS that evicts entries under memory pressure. This allows you do improve the responsiveness of your app by cacheing runtime generated objects without having to try to make guesses about what a reasonable memory footprint for the cache would be.

Does any such class exists? I've searched and haven't come up with anything although it seems like it should, perhaps with implementation ties into the garbage collector. For iOS I could roll my own and subscribe to UIApplicationDidReceiveMemoryWarningNotification to evict objects, but I'm not sure what the equivalent on other platforms would be.

Any ideas or advice would be greatly appreciated.

TIA,
Shea

Best Answer

Answers

  • adamkempadamkemp USInsider, Developer Group Leader mod

    In C# you can use a WeakReference.

  • sheashea USUniversity ✭✭

    I've played with that, but unfortunately it seems that WeakReferences are being collected more or less immediately. I'm looking for something that will stick around until the app starts experiencing memory pressure.

  • sheashea USUniversity ✭✭

    Thanks for your help. WeakReferences are indeed an important backing for any cache implementation. I'll roll my own using a technique along these lines. Thanks again!

Sign In or Register to comment.