Skip to content

rommansabbir/hawkx

Repository files navigation

HawkX 🚀

HawkX is a lightweight, file-based encrypted caching library designed for Android and Kotlin applications. It leverages the Android Keystore to securely generate and manage AES keys, providing a robust and asynchronous caching solution that is both efficient and easy to integrate. HawkX organizes cached data by schema, supports automatic expiration (TTL), and offers a simple API for storing, retrieving, removing, and querying encrypted data—all while maintaining high performance and a minimal memory footprint. 🔒

Cover Image

Cover image courtesy of Unsplash • Icon by Hanis


Features ✨

  • ✅ Robust Encryption:
    Utilizes AES/GCM/NoPadding with keys securely stored in the Android Keystore for hardware-backed security. 🔑

  • ✅ File-Based Storage:
    Persists data within your app’s internal cache directory under a dedicated subdirectory (hawkx), ensuring privacy without requiring additional permissions. 📁

  • ✅ Asynchronous Operations:
    Built with Kotlin coroutines to provide non-blocking I/O operations, ensuring smooth and responsive app performance. ⚡

  • ✅ Schema-Based Organization:
    Organize your cache data into logical schemas for structured storage and straightforward retrieval. 🗂️

  • ✅ Automatic Expiry (TTL):
    Supports time-to-live for cache entries, with automatic cleanup of expired data. ⏳

  • ✅ Thread-Safe Singleton:
    Provides a singleton instance with safe application context management (using a WeakReference) to prevent memory leaks. 🛡️


Installation 🌏

Add this to repository:

maven { url("https://jitpack.io") }

Add HawkX to your project by including the dependency in your build.gradle.kts file:

dependencies {
    implementation("com.github.rommansabbir:hawkx:1.0.0")
}

Note: HawkX requires a minimum API level of 23. 🛠️


Getting Started 🚀

Initialization

Initialize HawkX in your Application class to ensure the application context is available:

class MyApp : Application() {
    override fun onCreate() {
        super.onCreate()
        HawkX.init(this, loggingEnabled = true) // Enable or disable logging
    }
}

Then, retrieve the singleton instance wherever needed:

val hawkX = HawkX.getInstance()

Storing Data 💾

Store an object under a specific schema and key with an optional expiry (TTL) in seconds:

CoroutineScope().launch {
    // Save user data in the "Users" schema with a TTL of 1 hour (3600 seconds)
    hawkX.put("user_123", User(123, "Romman", "romman@example.com"), expirySeconds = 3600)
}

Retrieving Data 🔍

Retrieve a cached value by its key:

CoroutineScope().launch {
    val user: User? = hawkX.get("user_123")
    println("Retrieved user: $user")
}

Removing Data 🗑️

Delete a specific cache entry:

CoroutineScope().launch {
    hawkX.remove<User>("user_123")
}

Querying Data 🔎

Query cached entries using a custom filter function with optional pagination:

CoroutineScope().launch {
    val users = hawkX.query<User>({ it.name.contains("Romman") }, page = 0, pageSize = 10)
    users.forEach { println(it) }
}

Fetching All Stored Keys 🛠️

Retrieve a list of all stored keys:

CoroutineScope().launch {
    val keys = hawkX.getAllKeys()
    println("Stored keys: $keys")
}

Clearing Cache ❌

Delete all stored data:

CoroutineScope().launch {
    hawkX.dropCache()
}

API Reference 📚

  • put<T>(key: String, value: T, expirySeconds: Long = 3600, schema: String = T::class.simpleName ?: "DefaultSchema")
    Description: Stores the specified object under the given key with a TTL (in seconds).

  • get<T>(key: String): T?
    Description: Retrieves the stored object for the given key. Returns null if the key is missing or the entry has expired.

  • remove<T>(key: String)
    Description: Deletes the cache entry for the specified key.

  • query<T>(filter: (T) -> Boolean, page: Int? = null, pageSize: Int? = null): List<T>
    Description: Queries all entries, returning a list of values that match the provided filter with optional pagination.

  • getAllKeys(): List<String>
    Description: Returns a list of all stored keys.

  • dropCache()
    Description: Clears the entire cache.

  • Singleton Management:
    Initialize HawkX using HawkX.init(context, loggingEnabled = true) and retrieve the instance with HawkX.getInstance().


Contributing 🤝

Contributions to HawkX are welcome! If you have suggestions for improvements, bug fixes, or new features, please open an issue or submit a pull request on our GitHub repository.


License 📄

HawkX is an open source Android library by Romman Sabbir, licensed under the MIT License.

MIT License

Copyright (c) 2025 Romman Sabbir

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Contact 📧

For support or inquiries, please open an issue on GitHub or contact us at rommansabbir@gmail.com.


Happy coding with HawkX! 🎉

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages