Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/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.
https://github.com/reactiveui/akavache

akavache c-sharp cache cross-platform dotnet reactive-extensions reactive-programming xamarin

Last synced: about 1 month ago
JSON representation

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.

Awesome Lists containing this project

README

        

[![NuGet Stats](https://img.shields.io/nuget/v/akavache.svg)](https://www.nuget.org/packages/akavache) ![Build](https://github.com/reactiveui/Akavache/workflows/Build/badge.svg) [![Code Coverage](https://codecov.io/gh/reactiveui/akavache/branch/main/graph/badge.svg)](https://codecov.io/gh/reactiveui/akavache)













Akavache

## 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](https://github.com/github/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`), 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](#getting-started)
* [Choose a location](#choose-a-location)
* [The magic](#the-magic)
* [Platform-specific notes](#platform-specific-notes)
* [Using Akavache](#using-akavache)
* [Handling Errors](#handling-errors)
* [Shutting Down](#shutting-down)
* [Using a different SQLitePCL.raw bundle](#using-a-different-sqlitepclraw-bundle)
* [Examining Akavache caches](#examining-akavache-caches)
* [What's this Global Variable nonsense?](#whats-this-global-variable-nonsense)
* [DateTime/DateTimeOffset Considerations](#datetimedatetimeoffset-considerations)
* [Basic Method Documentation](#basic-method-documentation)
* [Extension Method Documentation](#extension-method-documentation)

## 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](https://developer.apple.com/library/content/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html#//apple_ref/doc/uid/TP40010672-CH2-SW1)
* **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](https://developer.android.com/reference/android/content/Context.html#getCacheDir%28%29)
* **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](https://docs.microsoft.com/windows/uwp/design/app-settings/store-and-retrieve-app-data)

### 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

```cs

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

```cs

Akavache.Registrations.Start("ApplicationName")
```

## Using Akavache

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

```cs
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");

// or without async/await
Toaster toaster;

BlobCache.UserAccount.GetObject("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:

```cs
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")
.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:

```cs
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.

```XAML

```

```cs
Akavache.Sqlite3.Registrations.Start("ApplicationName", () => SQLitePCL.Batteries_V2.Init());
```

For more info about using your own versions of [SqlitePCL.raw](https://github.com/ericsink/SQLitePCL.raw/wiki/Using-multiple-libraries-that-use-SQLitePCL.raw)

### Examining Akavache caches

Using [Akavache Explorer](https://github.com/anaisbetts/AkavacheExplorer), you
can dig into Akavache repos for debugging purposes to see what has been stored.

![](http://f.cl.ly/items/2D3Y0L0k262X0U0y3B0e/Image%202012.05.07%206:57:48%20PM.png)

### 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:

```cs
// 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):

```cs
/*
* Get items from the store
*/

// Get a single item
IObservable Get(string key);

// Get a list of items
IObservable> Get(IEnumerable keys);

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

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

// Get a list of objects given a list of keys
IObservable> GetObjects(IEnumerable keys);

/*
* Save items to the store
*/

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

// Insert a set of items
IObservable Insert(IDictionary keyValuePairs, DateTimeOffset? absoluteExpiration = null);

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

// Insert a group of objects
IObservable InsertObjects(IDictionary keyValuePairs, DateTimeOffset? absoluteExpiration = null);

/*
* Remove items from the store
*/

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

// Delete a list of items
IObservable Invalidate(IEnumerable keys);

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

// Deletes a list of objects
IObservable InvalidateObjects(IEnumerable keys);

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

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

/*
* Get Metadata about items
*/

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

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

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

// Return the time which a list of keys were created
IObservable> GetCreatedAt(IEnumerable keys);

/*
* Utility methods
*/

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

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

## Extension Method Documentation

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

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

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

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

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

/*
* Downloading and caching URLs and Images
*/

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

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

// Download an image from the network and load it
IObservable 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 GetOrFetchObject(string key, Func> fetchFunc, DateTimeOffset? absoluteExpiration = null);

// Like GetOrFetchObject, but isn't async
IObservable GetOrCreateObject(string key, Func 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 GetAndFetchLatest(this IBlobCache This,
string key,
Func> fetchFunc,
Func fetchPredicate = null,
DateTimeOffset? absoluteExpiration = null,
bool shouldInvalidateOnError = false,
Func cacheValidationPredicate = null)
```