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
![](https://github.blog/wp-content/uploads/2023/10/Collaboration-DarkMode-2.png?resize=400%2C212)
2024 is the biggest global election year in history. What’s at stake for developers?
GitHub is considering what is at stake for our users and platform, how we can take responsible action to support free and fair elections, and how developers contribute to resilient democratic processes.
![](https://github.blog/wp-content/uploads/2023/10/AI-DarkMode-2.png?resize=400%2C212)
GitHub named a Leader in the Gartner first-ever Magic Quadrant for AI Code Assistants
This year, as part of its annual Magic Quadrant series, Gartner published a first-of-its-kind report analyzing the state of play in the AI Code Assistants market–and named GitHub a Leader.
![AI in software development 2024 survey results with colorful graphics and percentages.](https://github.blog/wp-content/uploads/2024/08/ai-survey-header.png?resize=400%2C212)
Survey: The AI wave continues to grow on software development teams
We surveyed 2,000 people on software development teams at enterprises in the U.S., Brazil, India, and Germany about the use, experience, and expectations around generative AI tools in software development.