Skip to content
This repository has been archived by the owner on Oct 19, 2022. It is now read-only.
/ may Public archive

A deliberately simple persistent key-value cache store for Android.

License

Notifications You must be signed in to change notification settings

ashutoshgngwr/may

Repository files navigation

May

A deliberately simple persistent key-value store for Android. It uses SQLite databases to store its data and Kryo binary serialization to (de)serialize objects.

Implementation

Each entry has a string key and a value object. Typically, values are simple data containers, i.e., POJO or Kotlin data classes. Each May datastore stores its data in a single table inside its corresponding SQLite database. It uses key's hash code as the table primary key to optimise SQLite look-up performance. It uses thread local Kryo instances to (de)serialize values and store them as blobs in the SQLite database.

Install

Grab the latest version from Maven Central at io.github.ashutoshgngwr:may.

implementation 'io.github.ashutoshgngwr:may:1.0.0'

Usage

Usually, the clients should not create more than one instance of May per datastore. SQLite can only handle write operations from one thread at a time. Opening multiple May instances on the same datastore may lead to its corruption.

val may = May.openOrCreateDatastore("path/to/my.may.db")
// or May.openOrCreateDatastore(context, "my.may.db", Context.MODE_PRIVATE)

// persist value
may.put("key", "value")

// check if key exists in the store
may.contains("key")

// retrieve value
val value: String? = may.getAs<String>("key")

// remove value
val wasRemoved = may.remove("key")

// list 10 keys by prefix in ascending order skipping the first 5 that match.
val keys = may.keys("prefix/", offset = 5, limit = 10)

// close datastore
may.close()

Matching keys using a prefix

May supports matching keys using a prefix for find-keys, get-all and remove-all operations. These operations use a SQLite index on the key column to optimise lookups.

Multi-threading

Under the hood, May uses reentrant read-write locks to synchronize all of its operations, allowing multiple concurrent reads and at most one write at any given instant.

SQLite Write-Ahead Logging

Use May.enableWriteAheadLogging() and May.disableWriteAheadLogging() to configure WAL on the underlying SQLite database.

License

Apache License, Version 2.0