Akavache is now open source
Today, we’re open-sourcing a library that we have been using at GitHub: Akavache. Akavache is an asynchronous, persistent key-value cache created for writing native desktop and mobile applications in C#.…
Today, we’re open-sourcing a library that we have been using at GitHub: Akavache.
Akavache is an asynchronous, persistent key-value cache created for writing native desktop and mobile applications in C#. Think of it like memcached for desktop apps.
What does that mean?
Downloading and caching remote data from the internet while still keeping the UI responsive is a task that nearly every modern native application needs to do. However, many applications that don’t take the consideration of caching into the design from the start often end up with inconsistent, duplicated code for caching different types of objects.
Akavache is a library that makes common app patterns easy, and unifies caching of different object types (i.e. HTTP responses vs. JSON objects vs. images).
It’s built on a core key-value byte array store (conceptually similar to a Dictionary<string, byte[]>
), and on top of that store, extensions are added to support:
- Arbitrary objects via JSON
- HTTP Requests
- Fetching and loading Images
- Securely storing User Credentials
An example – consider Twitter for Mac
When you open Twitter for Mac, you immediately see content, even before the application finishes talking to Twitter. This content is cached, but the pattern of when to refresh the data might be different. For the tweets themselves, the logic might be something like, “Load the cached data, but always fetch the latest data”.
However, for the avatar images, you might have logic like, “Always load the cached image, but if the avatar is older than six hours, refresh the image.”
In Akavache, the former might be:
var tweets = await BlobCache.UserAccount.GetAndFetchLatest("tweets", DownloadSomeTweets());
And the latter might be something like:
var image = await BlobCache.LocalMachine.GetAndFetchLatest(tweet.AvatarUrl,
DownloadUrl(tweet.AvatarUrl),
createdAt => DateTimeOffset.Now - createdAt > TimeSpan.FromHours(6));
No (Thread.)Sleep ’till Brooklyn
Akavache is non-blocking, via a library called the Reactive Extensions – any operation that could delay the UI returns an Observable, which represents a future result.
Akavache also solves several difficult concurrency problems that simplify UI programming. For example, in the image above, a naive cache implementation could end up loading the GitHub avatar icon multiple times if the requests were issued at the same time (which is easy to do if you try to load the avatar for each tweet). Akavache ensures that exactly one network request is issued.
Learn More
Check out the GitHub Repository for more examples, and install the package via NuGet. Akavache is also used extensively in Play for Windows, a desktop client for Play, your employee-powered office DJ.
Written by
Related posts
GitHub Availability Report: November 2024
In November, we experienced one incident that resulted in degraded performance across GitHub services.
The top 10 gifts for the developer in your life
Whether you’re hunting for the perfect gift for your significant other, the colleague you drew in the office gift exchange, or maybe (just maybe) even for yourself, we’ve got you covered with our top 10 gifts that any developer would love.
Congratulations to the winners of the 2024 Gaady Awards
The Gaady Awards are like the Emmy Awards for the field of digital accessibility. And, just like the Emmys, the Gaadys are a reason to celebrate! On November 21, GitHub was honored to roll out the red carpet for the accessibility community at our San Francisco headquarters.