TinyCache 2.0

TinyCache, https://github.com/TinyStuff/TinyCache, is an open-source library created by Mats Törnberg and it is a part of the collection of libraries that we call TinyStuff. It is a library that makes caching a little bit easier.

Today we are releasing version 2.0 of TinyCache. In the first version, you could only have a single instance of TinyCahce. But I made some changes and added a pull request to the library that makes it possible to have multiple instances. The reason for that is that sometimes I wanted to have different settings for the cache in different situations in the same app. If you have used TinyCache before, an update to 2.0 will break your code. This blog post will introduce you to the new changes, but also to how TinyCache is working if you don't have used it before.

TinyCacheHandler

TinyCache 2.0 introduces TinyCacheHandler that will handle the instances of TinyCache. To make this possible, TinyCache and its method is no longer static.

If we only want one instance of TinyCache, we can use the Default method of TinyCacheHandler. The first time you access the Default property a new instance of TinyCache will be created if there are no instances created. This instance will get default as the key.

Create an additional instance of TinyCache

To add a new instance of TinyCacheHandler we can use either the create method or the add method.

Using the Create method:

var newCache = TinyCacheHandler.Create("myNewCache");

Using the Add method:

var newCache = new TinyCache();
TinyCacheHandler.Add("myNewCache", newCache);

Set default cache

If we have multiple cache instances, we may not want the first one to be the default, then we can change that bypassing the cache key to the SetDefault method of TinyCacheHandler.

TinyCacheHandler.SetDefault("myNewCache");

Get a specific instance of TinyCache

If we want to get an instance of TinyCache that not are the default instance, we can use the key for the instance as in the code below.

var cache = TinyCacheHandler.Get("myNewCache");

var result = cache.RunAsync>("myNewCache", () => { return api.GetData("customdata"); });

Use TinyCache

To use TinyCache, just use the RunAsync method and pass the cache key and how to fetch the data. If there are data in the cache, it will return cached data.

// Fetch data with default policy
var result = await TinyCacheHandler.Default.RunAsync>("cachekey", () => { return api.GetData("customdata"); });

TinyCache policies

With policies you can set how the cache should work, when should it refresh data etc.

Here is an example how we can use policies:

TinyCacheHandler.Default.SetBasePolicy(
    new TinyCachePolicy()
        .SetMode(TinyCacheModeEnum.CacheFirst) // fetch from cache first
        .SetFetchTimeout(TimeSpan.FromSeconds(5)) // 5 second excecution limit
        .SetExpirationTime(TimeSpan.FromMinutes(10)) // 10 minute expiration before next fetch
        .SetUpdateCacheTimeout(50) // Wait 50ms before trying to update cache in background

TinyCache stores

If we use TinyCache without setting a store, it will only cache data in the memory. But if we want to persist the data there are a couple of options. I mostly use the file storage store.

TinyCache.FileStorage
Is provided by the NuGet package, TinyCache.FileStorage. This store is persisting the cache data to a file at the filesystem of the device.

var store = new FileStorage();

var cacheFolder = string.Empty;
            
#if __IOS__ || __MACOS__
            cacheFolder = NSSearchPath.GetDirectories(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomain.User)[0];
#elif __ANDROID__
            cacheFolder = Application.Context.CacheDir.AbsolutePath;
#elif __UWP__
            cacheFolder = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
#else
            cacheFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
#endif

store.Initialize(cacheFolder);

// Set cache storage
TinyCacheHandler.Default.SetCacheStore(store);

TinyCache.Forms
Is provided by the NuGet package, TinyCache.Forms. This store is using the property storage that is provided by the Application class in Xamarin.Forms

var store = new XamarinPropertyStorage();

// Set cache storage
TinyCacheHandler.Default.SetCacheStore(store);

Read more

You will find both the documentation and the source code in the GitHub repository, https://github.com/TinyStuff/TinyCache.

  11/26/2019 - 9:20 AM