In-memory caching in Xamarin apps
It turns out Mono doesn't have
System.Runtime.Caching namespace, which makes it easy to implement caching for .NET apps. We had to find another way.
Caching libraries for Xamarin
We looked at a few libraries for caching (e.g., MemoryCache and Akavache), but surprisingly none of them manage cache size and memory. They simply add items to Dictionary, and if you add too many you get
It may not be an issue for many applications, but in Azure App we need to take into account users who has multiple subscriptions with thousands of resources.
BTW: Akavache is a great library. Besides in-memory cache it also supports persistent cache, have clean APIs and a lot of great documentation.
Implementing in-memory cache
After browsing internets and asking people at Xamarin chat we didn't find anything that would work for us, and we decided to implement in-memory cache by ourselves.
We use two layers of caching. First is using
WeakReference that leaves memory management to Garbage Collector. As GC is not very predictable and sometimes may unnecessary release some reference, we have second layer of caching. We call it
_limitedCache, and it keeps objects in memory until capacity reach 1000 objects. Then we remove half (500), least used objects from dictionary. Because the same objects are being kept in two dictionaries, the
WeakReference will never be released as long as object is in
_limitedCache. Thus, we always check only if object is present in
There is also third dictionary that keeps track of pending tasks that are responsible for getting data. This prevents us from sending the same requests more than once if object is not in cache yet.
What is great about building apps with Xamarin is the ability to share code across platforms. When we were implementing cache, we didn't touch any platform specific code. All work was done in Portable Class Library.
Adding cache to Azure App helped not only to decrease user's network data usage, but also to improve performance significantly!
If you need in-memory cache for your app, go ahead and use the above code snippet! If you are looking for persistent cache then consider using Akavache.
Are you caching? How? Why? Why not?