akavache 10.1.6

Prefix Reserved
dotnet add package akavache --version 10.1.6                
NuGet\Install-Package akavache -Version 10.1.6                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="akavache" Version="10.1.6" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add akavache --version 10.1.6                
#r "nuget: akavache, 10.1.6"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install akavache as a Cake Addin
#addin nuget:?package=akavache&version=10.1.6

// Install akavache as a Cake Tool
#tool nuget:?package=akavache&version=10.1.6                

NuGet Stats Build Code Coverage <br> <a href="https://www.nuget.org/packages/akavache"> <img src="https://img.shields.io/nuget/dt/akavache.svg"> </a> <a href="#backers"> <img src="https://opencollective.com/reactiveui/backers/badge.svg"> </a> <a href="#sponsors"> <img src="https://opencollective.com/reactiveui/sponsors/badge.svg"> </a> <a href="https://reactiveui.net/slack"> <img src="https://img.shields.io/badge/chat-slack-blue.svg"> </a>

<img alt="Akavache" src="https://raw.githubusercontent.com/reactiveui/styleguide/master/logo_akavache/main.png" width="150" />

Akavache: An Asynchronous Key-Value Store for Native Applications

Akavache is an asynchronous, persistent (i.e. writes to disk) key-value store created for writing desktop and mobile applications in C#, based on SQLite3. Akavache is great for both storing important data (i.e. user settings) as well as cached local data that expires.

Where can I use it?

Akavache is currently compatible with:

  • Xamarin.iOS / Xamarin.Mac / Xamarin.Android / Xamarin.TVOS / Xamarin.WatchOS
  • Maui iOS / Mac / Mac Catalyst / Android / TVOS
  • .NET 4.6.2 (and above) and .NET 6 Desktop (WPF and WinForms)
  • .NET 6.0
  • Windows 10 (Universal Windows Platform)
  • Tizen 4.0

What does that mean?

Downloading and storing remote data from the internet while still keeping the UI responsive is a task that nearly every modern 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.NET
  • Fetching and loading Images and URLs from the Internet
  • Storing and automatically encrypting User Credentials

Contents

Getting Started

Interacting with Akavache is primarily done through an object called BlobCache. At App startup, you must first set your app's name via BlobCache.ApplicationName or Akavache.Registrations.Start("ApplicationName") . After setting your app's name, you're ready to save some data.

For example with Xamarin Forms or WPF applications you'll place this in the constructor of your App.xaml.cs file.

Choose a location

There are four built-in locations that have some magic applied on some systems:

  • BlobCache.LocalMachine - Cached data. This data may get deleted without notification.
  • BlobCache.UserAccount - User settings. Some systems backup this data to the cloud.
  • BlobCache.Secure - For saving sensitive data - like credentials.
  • BlobCache.InMemory - A database, kept in memory. The data is stored for the lifetime of the app.

The magic

  • Xamarin.iOS may remove data, stored in BlobCache.LocalMachine, to free up disk space (only if your app is not running). The locations BlobCache.UserAccount and BlobCache.Secure will be backed up to iCloud and iTunes. Apple Documentation
  • Xamarin.Android may also start deleting data, stored in BlobCache.LocalMachine, if the system runs out of disk space. It isn't clearly specified if your app could be running while the system is cleaning this up. Android Documentation
  • Windows 10 (UWP) will replicate BlobCache.UserAccount and BlobCache.Secure to the cloud and synchronize it to all user devices on which the app is installed UWP Documentation

Platform-specific notes

  • Windows 10 (Universal Windows Platform) - You must mark your application as x86 or ARM, or else you will get a strange runtime error about SQLitePCL_Raw not loading correctly. You must also ensure that the Microsoft Visual C++ runtime is added to your project.
Handling Xamarin/Maui Linker

There are two options to ensure the Akavache.Sqlite3 dll will not be removed by Xamarin and Maui build tools

1) Add a file to reference the types

public static class LinkerPreserve
{
  static LinkerPreserve()
  {
    var persistentName = typeof(SQLitePersistentBlobCache).FullName;
    var encryptedName = typeof(SQLiteEncryptedBlobCache).FullName;
  }
}
2) Use the following initializer in your cross platform library or in your head project

Akavache.Registrations.Start("ApplicationName")

Using Akavache

The most straightforward way to use Akavache is via the object extensions:

using System.Reactive.Linq;   // IMPORTANT - this makes await work!

// Make sure you set the application name before doing any inserts or gets
Akavache.Registrations.Start("AkavacheExperiment")

var myToaster = new Toaster();
await BlobCache.UserAccount.InsertObject("toaster", myToaster);

//
// ...later, in another part of town...
//

// Using async/await
var toaster = await BlobCache.UserAccount.GetObject<Toaster>("toaster");

// or without async/await
Toaster toaster;

BlobCache.UserAccount.GetObject<Toaster>("toaster")
    .Subscribe(x => toaster = x, ex => Console.WriteLine("No Key!"));

Handling Errors

When a key is not present in the cache, GetObject throws a KeyNotFoundException (or more correctly, OnError's the IObservable). Often, you would want to return a default value instead of failing:

Toaster toaster;

try {
    toaster = await BlobCache.UserAccount.GetObject("toaster");
} catch (KeyNotFoundException ex) {
    toaster = new Toaster();
}

// Or without async/await:
toaster = await BlobCache.UserAccount.GetObject<Toaster>("toaster")
    .Catch(Observable.Return(new Toaster()));

Shutting Down

Critical to the integrity of your Akavache cache is the BlobCache.Shutdown() method. You must call this when your application shuts down. Moreover, be sure to wait for the result:

BlobCache.Shutdown().Wait();

Failure to do this may mean that queued items are not flushed to the cache.

Using a different SQLitePCL.raw bundle

To use a different SQLitePCL.raw bundle, e.g. Microsoft.AppCenter:

  • Install the akavache.sqlite3 nuget instead of akavache
  • Install the SQLitePCLRaw bundle you want to use, e.g., SQLitePCLRaw.bundle_green
  • Use Akavache.Sqlite3.Registrations.Start("ApplicationName", () => SQLitePCL.Batteries_V2.Init()); in your platform projects or in your cross platform project.
<PackageReference Include="akavache.sqlite3" Version="6.0.40-g7e90c572c6" />
<PackageReference Include="SQLitePCLRaw.bundle_green" Version="1.1.11" />
Akavache.Sqlite3.Registrations.Start("ApplicationName", () => SQLitePCL.Batteries_V2.Init());

For more info about using your own versions of SqlitePCL.raw

Examining Akavache caches

Using Akavache Explorer, you can dig into Akavache repos for debugging purposes to see what has been stored.

alternate text is missing from this package README image

What's this Global Variable nonsense?

Why can't I use $FAVORITE_IOC_LIBRARY?

You totally can. Just instantiate SQLitePersistentBlobCache or SQLiteEncryptedBlobCache instead - the static variables are there just to make it easier to get started.

DateTime/DateTimeOffset Considerations

Our default implementation overrides BSON to read and write DateTime's as UTC. To override the reader's behavior you can set BlobCache.ForcedDateTimeKind as in the following example:

// Sets the reader to return DateTime/DateTimeOffset in Local.
BlobCache.ForcedDateTimeKind = DateTimeKind.Local;

DateTime are stored as ticks for high precision. DateTimeOffset are stored as ticks for both the Date/Time aspect and the offset.

Basic Method Documentation

Every blob cache supports the basic raw operations given below (some of them are not implemented directly, but are added on via extension methods):

/*
 * Get items from the store
 */

// Get a single item
IObservable<byte[]> Get(string key);

// Get a list of items
IObservable<IDictionary<string, byte[]>> Get(IEnumerable<string> keys);

// Get an object serialized via InsertObject
IObservable<T> GetObject<T>(string key);

// Get all objects of type T
IObservable<IEnumerable<T>> GetAllObjects<T>();

// Get a list of objects given a list of keys
IObservable<IDictionary<string, T>> GetObjects<T>(IEnumerable<string> keys);

/*
 * Save items to the store
 */

// Insert a single item
IObservable<Unit> Insert(string key, byte[] data, DateTimeOffset? absoluteExpiration = null);

// Insert a set of items
IObservable<Unit> Insert(IDictionary<string, byte[]> keyValuePairs, DateTimeOffset? absoluteExpiration = null);

// Insert a single object
IObservable<Unit> InsertObject<T>(string key, T value, DateTimeOffset? absoluteExpiration = null);

// Insert a group of objects
IObservable<Unit> InsertObjects<T>(IDictionary<string, T> keyValuePairs, DateTimeOffset? absoluteExpiration = null);

/*
 * Remove items from the store
 */

// Delete a single item
IObservable<Unit> Invalidate(string key);

// Delete a list of items
IObservable<Unit> Invalidate(IEnumerable<string> keys);

// Delete a single object (do *not* use Invalidate for items inserted with InsertObject!)
IObservable<Unit> InvalidateObject<T>(string key);

// Deletes a list of objects
IObservable<Unit> InvalidateObjects<T>(IEnumerable<string> keys);

// Deletes all items (regardless if they are objects or not)
IObservable<Unit> InvalidateAll();

// Deletes all objects of type T
IObservable<Unit> InvalidateAllObjects<T>();

/*
 * Get Metadata about items
 */

// Return a list of all keys. Use for debugging purposes only.
IObservable<IEnumerable<string>> GetAllKeys();

// Return the time which an item was created
IObservable<DateTimeOffset?> GetCreatedAt(string key);

// Return the time which an object of type T was created
IObservable<DateTimeOffset?> GetObjectCreatedAt<T>(string key);

// Return the time which a list of keys were created
IObservable<IDictionary<string, DateTimeOffset?>> GetCreatedAt(IEnumerable<string> keys);

/*
 * Utility methods
 */

// Attempt to ensure all outstanding operations are written to disk
IObservable<Unit> Flush();

// Preemptively drop all expired keys and run SQLite's VACUUM method on the
// underlying database
IObservable<Unit> Vacuum();

Extension Method Documentation

On top of every IBlobCache object, there are extension methods that help with common application scenarios:

/*
 * Username / Login Methods (only available on ISecureBlobCache)
 */

// Save login information for the given host
IObservable<Unit> SaveLogin(string user, string password, string host = "default", DateTimeOffset? absoluteExpiration = null);

// Load information for the given host
IObservable<LoginInfo> GetLoginAsync(string host = "default");

// Erase information for the given host
IObservable<Unit> EraseLogin(string host = "default");

/*
 * Downloading and caching URLs and Images
 */

// Download a file as a byte array
IObservable<byte[]> DownloadUrl(string url,
    IDictionary<string, string> headers = null,
    bool fetchAlways = false,
    DateTimeOffset? absoluteExpiration = null);

// Load a given key as an image
IObservable<IBitmap> LoadImage(string key, float? desiredWidth = null, float? desiredHeight = null);

// Download an image from the network and load it
IObservable<IBitmap> LoadImageFromUrl(string url,
    bool fetchAlways = false,
    float? desiredWidth = null,
    float? desiredHeight = null,
    DateTimeOffset? absoluteExpiration = null);

/*
 * Composite operations
 */

// Attempt to return an object from the cache. If the item doesn't
// exist or returns an error, call a Func to return the latest
// version of an object and insert the result in the cache.
IObservable<T> GetOrFetchObject<T>(string key, Func<Task<T>> fetchFunc, DateTimeOffset? absoluteExpiration = null);

// Like GetOrFetchObject, but isn't async
IObservable<T> GetOrCreateObject<T>(string key, Func<T> fetchFunc, DateTimeOffset? absoluteExpiration = null);

// Immediately return a cached version of an object if available, but *always*
// also execute fetchFunc to retrieve the latest version of an object.
IObservable<T> GetAndFetchLatest<T>(this IBlobCache This,
    string key,
    Func<IObservable<T>> fetchFunc,
    Func<DateTimeOffset, bool> fetchPredicate = null,
    DateTimeOffset? absoluteExpiration = null,
    bool shouldInvalidateOnError = false,
    Func<T, bool> cacheValidationPredicate = null)
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-android34.0 is compatible.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-ios17.5 is compatible.  net8.0-maccatalyst was computed.  net8.0-maccatalyst17.5 is compatible.  net8.0-macos was computed.  net8.0-macos14.5 is compatible.  net8.0-tvos was computed.  net8.0-tvos17.5 is compatible.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (54)

Showing the top 5 NuGet packages that depend on akavache:

Package Downloads
SheshaMobile.Core

Common application functionality and features to be shared across the framework

SheshaMobile.Modules

The modules module contains common functionality shared across multiple modules

SheshaMobile.Modules.UserProfile

A module for building apps with user functionality

SheshaMobile.Modules.Facilities

The facilities module contains common functionality for browsing and viewing facilities

SheshaMobile.Modules.Home

The home module contains common functionality and essetntials required to build an app home page

GitHub repositories (17)

Showing the top 5 popular GitHub repositories that depend on akavache:

Repository Stars
CodeHubApp/CodeHub
CodeHub is an iOS application written using Xamarin
reactiveui/Akavache
An asynchronous, persistent key-value store created for writing desktop and mobile applications, based on SQLite3. Akavache is great for both storing important data as well as cached local data that expires.
MoocDownloader/MoocDownloader
An MOOC downloader implemented by .NET. 一枚由 .NET 实现的 MOOC 下载器.
reactiveui/Camelotia
Cross-platform sample .NET GUI for cloud file management.
reactiveui/ReactiveUI.Samples
This repository contains ReactiveUI samples.
Version Downloads Last updated
10.1.6 12,129 9/16/2024
10.0.1 53,339 5/1/2024
9.1.20 229,251 6/29/2023
9.1.7 63,504 2/1/2023
9.0.1 212,318 6/25/2022
8.1.1 201,910 12/12/2021
7.3.47 13,773 11/26/2021
7.3.1 134,648 6/6/2021
7.2.1 247,064 1/22/2021
7.1.1 181,321 10/23/2020
6.10.20 4,254,970 6/12/2020
6.10.17 49,447 5/7/2020
6.10.11 14,063 4/23/2020
6.10.6 14,238 4/1/2020
6.10.4 44,136 2/5/2020
6.10.3 9,296 1/29/2020
6.10.2 6,195 1/27/2020
6.10.1 831 1/27/2020
6.9.10 43,098 11/4/2019
6.9.1 40,056 10/5/2019
6.8.1 16,991 9/6/2019
6.7.1 14,733 8/6/2019
6.6.1 1,060 8/6/2019
6.5.20 16,967 7/28/2019
6.5.9 32,152 6/7/2019
6.5.1 90,095 3/27/2019
6.4.1 7,380,309 3/3/2019
6.3.6 8,162 2/19/2019
6.3.2 17,791 2/1/2019
6.3.1 2,877 1/25/2019
6.2.3 19,701 12/27/2018
6.2.1 1,108 12/27/2018
6.1.3 1,613 12/24/2018
6.1.2 2,036 12/24/2018
6.1.1 1,721 12/23/2018
6.0.31 39,831 11/11/2018
6.0.30 41,479 10/17/2018
6.0.27 14,095 10/5/2018
6.0.20 55,419 9/5/2018
6.0.19-beta 1,062 9/3/2018
6.0.17-beta 1,999 8/24/2018
6.0.0-alpha0038 78,119 9/3/2017
5.0.0 497,373 11/4/2016
4.1.2 83,473 10/27/2015
4.1.1 20,177 3/26/2015
4.1.0 5,355 1/3/2015
4.0.4 3,309 11/6/2014
4.0.3.2 6,495 8/28/2014
4.0.3.1 1,574 8/28/2014
4.0.3 1,524 8/28/2014
4.0.2 1,565 8/25/2014
4.0.1 1,715 8/8/2014
4.0.0 1,727 8/3/2014
3.99.3-beta 1,280 7/25/2014
3.99.2-beta 1,145 7/17/2014
3.99.1-beta 1,725 2/8/2014
3.2.0 4,143 12/11/2013
3.1.2 2,973 11/20/2013
3.1.1 14,709 10/12/2013
3.1.0 2,546 10/12/2013
3.0.2 7,366 8/9/2013
3.0.1 2,980 7/3/2013
3.0.0.20130620-alpha 1,995 6/21/2013
3.0.0.20130531-alpha 1,997 6/1/2013
3.0.0.20130519-alpha 1,909 5/20/2013
3.0.0.20130513-alpha 1,899 5/14/2013
2.6.12 2,718 4/1/2014
2.6.11 2,536 2/21/2014
2.6.10 2,518 1/14/2014
2.6.9 2,821 9/11/2013
2.6.8 2,897 8/22/2013
2.6.7 3,079 6/18/2013
2.6.6 3,652 5/2/2013
2.6.5 2,618 5/1/2013
2.6.4 2,764 4/2/2013
2.6.3 2,627 4/2/2013
2.6.2 2,769 3/13/2013
2.6.1 2,703 3/6/2013
2.6.0 2,903 3/5/2013
2.5.1 2,783 2/18/2013
2.5.0 2,798 2/11/2013
2.4.3 2,697 2/3/2013
2.4.1 2,393 1/14/2013
2.4.0 2,475 1/8/2013
2.3.1 2,455 1/8/2013
2.3.0 1,747 12/26/2012
2.2.2 1,686 12/20/2012
2.2.1 1,713 12/20/2012
2.2.0 1,621 12/20/2012
2.0.0 1,946 11/3/2012
1.0.1 2,191 4/28/2012
1.0.0 1,975 4/28/2012