CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-com-tencent--mmkv-static

High-performance, cross-platform key-value storage framework with static C++ linking for Android applications

Pending
Overview
Eval results
Files

data-operations.mddocs/

Data Storage and Retrieval

MMKV provides high-performance key-value storage supporting Java primitives, strings, byte arrays, Parcelable objects, and string sets. All data is immediately persisted to disk using memory-mapped files, with optional key expiration support.

Capabilities

Boolean Values

Store and retrieve boolean values with optional default values and expiration.

/**
 * Store a boolean value
 * @param key The key to store the value under
 * @param value The boolean value to store
 * @return true if successful, false otherwise
 */
boolean encode(String key, boolean value);

/**
 * Store a boolean value with expiration
 * @param key The key to store the value under
 * @param value The boolean value to store
 * @param expireDurationInSecond Override the default duration, 0 means never expire
 * @return true if successful, false otherwise
 */
boolean encode(String key, boolean value, int expireDurationInSecond);

/**
 * Retrieve a boolean value
 * @param key The key to retrieve
 * @return The stored boolean value, or false if key doesn't exist
 */
boolean decodeBool(String key);

/**
 * Retrieve a boolean value with default
 * @param key The key to retrieve
 * @param defaultValue The default value to return if key doesn't exist
 * @return The stored boolean value, or defaultValue if key doesn't exist
 */
boolean decodeBool(String key, boolean defaultValue);

Usage Examples:

MMKV kv = MMKV.defaultMMKV();

// Store boolean values
kv.encode("user_logged_in", true);
kv.encode("notifications_enabled", false);

// Store with expiration (expires in 1 hour)
kv.encode("temp_flag", true, MMKV.ExpireInHour);

// Retrieve boolean values
boolean isLoggedIn = kv.decodeBool("user_logged_in");
boolean notificationsOn = kv.decodeBool("notifications_enabled", false);

Integer Values

Store and retrieve 32-bit integer values with optional expiration.

/**
 * Store an integer value
 * @param key The key to store the value under
 * @param value The integer value to store
 * @return true if successful, false otherwise
 */
boolean encode(String key, int value);

/**
 * Store an integer value with expiration
 * @param key The key to store the value under
 * @param value The integer value to store
 * @param expireDurationInSecond Override the default duration, 0 means never expire
 * @return true if successful, false otherwise
 */
boolean encode(String key, int value, int expireDurationInSecond);

/**
 * Retrieve an integer value
 * @param key The key to retrieve
 * @return The stored integer value, or 0 if key doesn't exist
 */
int decodeInt(String key);

/**
 * Retrieve an integer value with default
 * @param key The key to retrieve
 * @param defaultValue The default value to return if key doesn't exist
 * @return The stored integer value, or defaultValue if key doesn't exist
 */
int decodeInt(String key, int defaultValue);

Usage Examples:

// Store integer values
kv.encode("user_id", 12345);
kv.encode("login_attempts", 3);

// Store with expiration (expires in 1 day)
kv.encode("daily_score", 1500, MMKV.ExpireInDay);

// Retrieve integer values
int userId = kv.decodeInt("user_id");
int attempts = kv.decodeInt("login_attempts", 0);

Long Values

Store and retrieve 64-bit long values with optional expiration.

/**
 * Store a long value
 * @param key The key to store the value under
 * @param value The long value to store
 * @return true if successful, false otherwise
 */
boolean encode(String key, long value);

/**
 * Store a long value with expiration
 * @param key The key to store the value under
 * @param value The long value to store
 * @param expireDurationInSecond Override the default duration, 0 means never expire
 * @return true if successful, false otherwise
 */
boolean encode(String key, long value, int expireDurationInSecond);

/**
 * Retrieve a long value
 * @param key The key to retrieve
 * @return The stored long value, or 0 if key doesn't exist
 */
long decodeLong(String key);

/**
 * Retrieve a long value with default
 * @param key The key to retrieve
 * @param defaultValue The default value to return if key doesn't exist
 * @return The stored long value, or defaultValue if key doesn't exist
 */
long decodeLong(String key, long defaultValue);

Usage Examples:

// Store long values
kv.encode("timestamp", System.currentTimeMillis());
kv.encode("file_size", 1024L * 1024L * 100L); // 100MB

// Store with expiration (expires in 1 minute)
kv.encode("session_start", System.currentTimeMillis(), MMKV.ExpireInMinute);

// Retrieve long values
long timestamp = kv.decodeLong("timestamp");
long fileSize = kv.decodeLong("file_size", 0L);

Float Values

Store and retrieve 32-bit float values with optional expiration.

/**
 * Store a float value
 * @param key The key to store the value under
 * @param value The float value to store
 * @return true if successful, false otherwise
 */
boolean encode(String key, float value);

/**
 * Store a float value with expiration
 * @param key The key to store the value under
 * @param value The float value to store
 * @param expireDurationInSecond Override the default duration, 0 means never expire
 * @return true if successful, false otherwise
 */
boolean encode(String key, float value, int expireDurationInSecond);

/**
 * Retrieve a float value
 * @param key The key to retrieve
 * @return The stored float value, or 0.0f if key doesn't exist
 */
float decodeFloat(String key);

/**
 * Retrieve a float value with default
 * @param key The key to retrieve
 * @param defaultValue The default value to return if key doesn't exist
 * @return The stored float value, or defaultValue if key doesn't exist
 */
float decodeFloat(String key, float defaultValue);

Usage Examples:

// Store float values
kv.encode("user_rating", 4.5f);
kv.encode("completion_percentage", 0.75f);

// Retrieve float values
float rating = kv.decodeFloat("user_rating");
float completion = kv.decodeFloat("completion_percentage", 0.0f);

Double Values

Store and retrieve 64-bit double values with optional expiration.

/**
 * Store a double value
 * @param key The key to store the value under
 * @param value The double value to store
 * @return true if successful, false otherwise
 */
boolean encode(String key, double value);

/**
 * Store a double value with expiration
 * @param key The key to store the value under
 * @param value The double value to store
 * @param expireDurationInSecond Override the default duration, 0 means never expire
 * @return true if successful, false otherwise
 */
boolean encode(String key, double value, int expireDurationInSecond);

/**
 * Retrieve a double value
 * @param key The key to retrieve
 * @return The stored double value, or 0.0 if key doesn't exist
 */
double decodeDouble(String key);

/**
 * Retrieve a double value with default
 * @param key The key to retrieve
 * @param defaultValue The default value to return if key doesn't exist
 * @return The stored double value, or defaultValue if key doesn't exist
 */
double decodeDouble(String key, double defaultValue);

Usage Examples:

// Store double values for high precision
kv.encode("latitude", 37.7749295);
kv.encode("longitude", -122.4194155);

// Retrieve double values
double lat = kv.decodeDouble("latitude");
double lng = kv.decodeDouble("longitude", 0.0);

String Values

Store and retrieve string values with optional expiration and null safety.

/**
 * Store a string value
 * @param key The key to store the value under
 * @param value The string value to store (can be null)
 * @return true if successful, false otherwise
 */
boolean encode(String key, String value);

/**
 * Store a string value with expiration
 * @param key The key to store the value under
 * @param value The string value to store (can be null)
 * @param expireDurationInSecond Override the default duration, 0 means never expire
 * @return true if successful, false otherwise
 */
boolean encode(String key, String value, int expireDurationInSecond);

/**
 * Retrieve a string value
 * @param key The key to retrieve
 * @return The stored string value, or null if key doesn't exist
 */
String decodeString(String key);

/**
 * Retrieve a string value with default
 * @param key The key to retrieve
 * @param defaultValue The default value to return if key doesn't exist (can be null)
 * @return The stored string value, or defaultValue if key doesn't exist
 */
String decodeString(String key, String defaultValue);

Usage Examples:

// Store string values
kv.encode("username", "john_doe");
kv.encode("user_email", "john@example.com");
kv.encode("optional_field", null); // Store null values

// Store with expiration (expires in 1 hour)
kv.encode("session_token", "abc123xyz", MMKV.ExpireInHour);

// Retrieve string values
String username = kv.decodeString("username");
String email = kv.decodeString("user_email", "unknown@example.com");
String token = kv.decodeString("session_token"); // May return null if expired

String Sets

Store and retrieve sets of strings with optional expiration.

/**
 * Store a string set
 * @param key The key to store the value under
 * @param value The string set to store (can be null)
 * @return true if successful, false otherwise
 */
boolean encode(String key, Set<String> value);

/**
 * Store a string set with expiration
 * @param key The key to store the value under
 * @param value The string set to store (can be null)
 * @param expireDurationInSecond Override the default duration, 0 means never expire
 * @return true if successful, false otherwise
 */
boolean encode(String key, Set<String> value, int expireDurationInSecond);

/**
 * Retrieve a string set
 * @param key The key to retrieve
 * @return The stored string set, or null if key doesn't exist
 */
Set<String> decodeStringSet(String key);

/**
 * Retrieve a string set with default
 * @param key The key to retrieve
 * @param defaultValue The default value to return if key doesn't exist (can be null)
 * @return The stored string set, or defaultValue if key doesn't exist
 */
Set<String> decodeStringSet(String key, Set<String> defaultValue);

/**
 * Retrieve a string set with custom Set implementation
 * @param key The key to retrieve
 * @param defaultValue The default value to return if key doesn't exist (can be null)
 * @param cls The Set class to instantiate for the result
 * @return The stored string set, or defaultValue if key doesn't exist
 */
Set<String> decodeStringSet(String key, Set<String> defaultValue, Class<? extends Set> cls);

Usage Examples:

// Store string sets
Set<String> tags = new HashSet<>();
tags.add("java");
tags.add("android");
tags.add("mobile");
kv.encode("user_tags", tags);

// Store with expiration
Set<String> tempCategories = new HashSet<>();
tempCategories.add("temp1");
tempCategories.add("temp2");
kv.encode("temp_categories", tempCategories, MMKV.ExpireInHour);

// Retrieve string sets
Set<String> userTags = kv.decodeStringSet("user_tags");
Set<String> emptyTags = kv.decodeStringSet("non_existent", new HashSet<>());

// Use specific Set implementation
LinkedHashSet<String> orderedTags = (LinkedHashSet<String>) kv.decodeStringSet(
    "user_tags", 
    new LinkedHashSet<>(), 
    LinkedHashSet.class
);

Byte Arrays

Store and retrieve raw byte arrays with optional expiration.

/**
 * Store a byte array
 * @param key The key to store the value under
 * @param value The byte array to store (can be null)
 * @return true if successful, false otherwise
 */
boolean encode(String key, byte[] value);

/**
 * Store a byte array with expiration
 * @param key The key to store the value under
 * @param value The byte array to store (can be null)
 * @param expireDurationInSecond Override the default duration, 0 means never expire
 * @return true if successful, false otherwise
 */
boolean encode(String key, byte[] value, int expireDurationInSecond);

/**
 * Retrieve a byte array
 * @param key The key to retrieve
 * @return The stored byte array, or null if key doesn't exist
 */
byte[] decodeBytes(String key);

/**
 * Retrieve a byte array with default
 * @param key The key to retrieve
 * @param defaultValue The default value to return if key doesn't exist (can be null)
 * @return The stored byte array, or defaultValue if key doesn't exist
 */
byte[] decodeBytes(String key, byte[] defaultValue);

Usage Examples:

// Store binary data
byte[] imageData = loadImageAsBytes();
kv.encode("profile_image", imageData);

// Store with expiration (expires in 1 day)
byte[] tempData = generateTempData();
kv.encode("temp_binary", tempData, MMKV.ExpireInDay);

// Retrieve binary data
byte[] storedImage = kv.decodeBytes("profile_image");
byte[] defaultImage = kv.decodeBytes("profile_image", getDefaultImageBytes());

Parcelable Objects

Store and retrieve Android Parcelable objects with automatic serialization.

/**
 * Store a Parcelable object
 * @param key The key to store the value under
 * @param value The Parcelable object to store (can be null)
 * @return true if successful, false otherwise
 */
boolean encode(String key, Parcelable value);

/**
 * Store a Parcelable object with expiration
 * @param key The key to store the value under
 * @param value The Parcelable object to store (can be null)
 * @param expireDurationInSecond Override the default duration, 0 means never expire
 * @return true if successful, false otherwise
 */
boolean encode(String key, Parcelable value, int expireDurationInSecond);

/**
 * Retrieve a Parcelable object
 * @param key The key to retrieve
 * @param tClass The class of the Parcelable object
 * @return The stored Parcelable object, or null if key doesn't exist
 */
<T extends Parcelable> T decodeParcelable(String key, Class<T> tClass);

/**
 * Retrieve a Parcelable object with default
 * @param key The key to retrieve
 * @param tClass The class of the Parcelable object
 * @param defaultValue The default value to return if key doesn't exist (can be null)
 * @return The stored Parcelable object, or defaultValue if key doesn't exist
 */
<T extends Parcelable> T decodeParcelable(String key, Class<T> tClass, T defaultValue);

Usage Examples:

// Custom Parcelable class
public class UserProfile implements Parcelable {
    public String name;
    public int age;
    public String email;
    
    // Parcelable implementation...
}

// Store Parcelable objects
UserProfile profile = new UserProfile("John", 25, "john@example.com");
kv.encode("user_profile", profile);

// Store with expiration
UserProfile tempProfile = createTempProfile();
kv.encode("temp_profile", tempProfile, MMKV.ExpireInHour);

// Retrieve Parcelable objects
UserProfile storedProfile = kv.decodeParcelable("user_profile", UserProfile.class);
UserProfile defaultProfile = kv.decodeParcelable(
    "user_profile", 
    UserProfile.class, 
    new UserProfile("Unknown", 0, "")
);

Key Management

Check for key existence and get information about stored values.

/**
 * Check whether or not MMKV contains the key
 * @param key The key to check
 * @return true if the key exists, false otherwise
 */
boolean containsKey(String key);

/**
 * Get all keys
 * @return Array of all keys, or null if empty
 */
String[] allKeys();

/**
 * Get all non-expired keys. Note that this call has costs.
 * @return Array of non-expired keys, or null if empty
 */
String[] allNonExpireKeys();

/**
 * Get the total count of all keys
 * @return The total count of keys
 */
long count();

/**
 * Get the total count of all non-expired keys. Note that this call has costs.
 * @return The total count of non-expired keys
 */
long countNonExpiredKeys();

Usage Examples:

// Check if key exists before retrieving
if (kv.containsKey("user_profile")) {
    UserProfile profile = kv.decodeParcelable("user_profile", UserProfile.class);
    // Use profile...
}

// Get all keys for debugging or migration
String[] allKeys = kv.allKeys();
for (String key : allKeys) {
    Log.d("MMKV", "Key: " + key);
}

// Get count of stored items
long totalItems = kv.count();
long activeItems = kv.countNonExpiredKeys();
Log.d("MMKV", "Total: " + totalItems + ", Active: " + activeItems);

Value Size Information

Get information about the storage size of values.

/**
 * Get the actual size consumption of the key's value.
 * Note: might be a little bigger than value's length.
 * @param key The key of the value
 * @return The storage size in bytes
 */
int getValueSize(String key);

/**
 * Get the actual size of the key's value. String's length or byte[]'s length, etc.
 * @param key The key of the value
 * @return The actual value size
 */
int getValueActualSize(String key);

Usage Examples:

// Check storage overhead
int storageSize = kv.getValueSize("large_data");
int actualSize = kv.getValueActualSize("large_data");
Log.d("MMKV", "Storage: " + storageSize + ", Actual: " + actualSize);

Value Removal

Remove individual keys or multiple keys at once.

/**
 * Remove a single key-value pair
 * @param key The key to remove
 */
void removeValueForKey(String key);

/**
 * Batch remove multiple keys from the MMKV instance
 * @param arrKeys The keys to be removed
 */
void removeValuesForKeys(String[] arrKeys);

Usage Examples:

// Remove single key
kv.removeValueForKey("temp_data");

// Remove multiple keys
String[] keysToRemove = {"temp1", "temp2", "temp3"};
kv.removeValuesForKeys(keysToRemove);

Install with Tessl CLI

npx tessl i tessl/maven-com-tencent--mmkv-static

docs

advanced-features.md

data-operations.md

index.md

initialization.md

instance-management.md

namespace.md

process-management.md

shared-preferences.md

utility-types.md

tile.json