{"id":18565052,"url":"https://github.com/jenly1314/kvcache","last_synced_at":"2025-04-10T04:32:19.513Z","repository":{"id":46963398,"uuid":"515537815","full_name":"jenly1314/KVCache","owner":"jenly1314","description":":memo: KVCache 是一个便于统一管理的键值缓存库；支持无缝切换缓存实现","archived":false,"fork":false,"pushed_at":"2024-08-27T16:00:13.000Z","size":20118,"stargazers_count":25,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-08-27T17:36:34.343Z","etag":null,"topics":["android","cache","cache-storage","data-store","key-value-pair","key-value-store","kotlin","mmkv","sharedpreferences"],"latest_commit_sha":null,"homepage":"https://jenly1314.github.io/KVCache/","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jenly1314.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-07-19T10:27:55.000Z","updated_at":"2024-08-27T16:11:21.000Z","dependencies_parsed_at":"2024-01-27T15:29:45.582Z","dependency_job_id":"8b7a0b04-2ad6-4f90-a8fc-eb4a0b5f8609","html_url":"https://github.com/jenly1314/KVCache","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jenly1314%2FKVCache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jenly1314%2FKVCache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jenly1314%2FKVCache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jenly1314%2FKVCache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jenly1314","download_url":"https://codeload.github.com/jenly1314/KVCache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223424313,"owners_count":17142745,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","cache","cache-storage","data-store","key-value-pair","key-value-store","kotlin","mmkv","sharedpreferences"],"created_at":"2024-11-06T22:17:31.800Z","updated_at":"2024-11-06T22:17:32.494Z","avatar_url":"https://github.com/jenly1314.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# KVCache\n\n[![Download](https://img.shields.io/badge/download-App-blue.svg)](https://raw.githubusercontent.com/jenly1314/KVCache/master/app/release/app-release.apk)\n[![MavenCentral](https://img.shields.io/maven-central/v/com.github.jenly1314/kvcache)](https://repo1.maven.org/maven2/com/github/jenly1314/kvcache/)\n[![JitPack](https://jitpack.io/v/jenly1314/KVCache.svg)](https://jitpack.io/#jenly1314/KVCache)\n[![CircleCI](https://circleci.com/gh/jenly1314/KVCache.svg?style=svg)](https://circleci.com/gh/jenly1314/KVCache)\n[![API](https://img.shields.io/badge/API-21%2B-blue.svg?style=flat)](https://android-arsenal.com/api?level=21)\n[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/licenses/mit-license.php)\n\nKVCache 是一个便于统一管理的键值缓存库；支持无缝切换缓存实现。\n\n**主要有如下特点：**\n\n* 支持无缝切换缓存的实现；（主要包括：**MMKV** 、**DataStore** 、**SharedPreferences**、**Memory** ）\n* 你可以无需关心 API 之间的差异，使用时就像使用Map一样简单；\n* 利用Kotlin的委托属性特性，使用更简洁。\n\n## 引入\n\n### Gradle:\n\n1. 在Project的 **build.gradle** 或 **setting.gradle** 中添加远程仓库\n\n    ```gradle\n    repositories {\n        //...\n        mavenCentral()\n    }\n    ```\n\n2. 在Module的 **build.gradle** 里面添加引入依赖项\n\n    ```gradle\n    implementation 'com.github.jenly1314:kvcache:1.3.0'\n    ```\n\n## 使用\n\n### KVCache 初始化\n```kotlin\n\n    // 建议在 Application 中进行初始化\n    KVCache.initialize(this, provider)\n\n```\n\nKVCache初始化时，关于 **Provider** 参数的相关说明如下：\n\n```kotlin\n   /**\n     * 使用 MMKV 提供缓存实现；需依赖 MMKV\n     */\n    Provider.MMKV_CACHE\n    /**\n     * 使用 DataStore 提供缓存实现；需依赖 DataStore\n     */\n    Provider.DATA_STORE_CACHE\n    /**\n     * 使用 SharedPreferences 提供缓存实现\n     */\n    Provider.SHARED_PREFERENCES_CACHE\n    /**\n     * 使用 Memory 提供缓存实现\n     */\n    Provider.MEMORY_CACHE\n```\n\n\u003e 初始化 KVCache 时，如果不传 provider, 则会自动决定缓存实现：优先级从高到低依次为： MMKV -\u003e DataStore -\u003e SharedPreferences -\u003e Memory\n\n\u003e 当需要进行单元测试时，使用`Memory` 是一个非常不错的选择。\n\n### KVCache 基本使用示例\n\nKVCache的主要函数大多以`put`和`get`开头，基本都是通过`put`缓存键值对，通过`get`获取键对应的缓存；使用方式和 **bundle** 类似；\n\n```kotlin\n\n    val f = 1.0F\n    KVCache.put(\"float\", f)\n    Log.d(TAG, \"$cacheProvider: float = ${KVCache.getFloat(\"float\")}\")\n\n    val i = 2\n    KVCache.put(\"int\", i)\n    Log.d(TAG, \"$cacheProvider: int = ${KVCache.getInt(\"int\")}\")\n\n    val d = 3.0\n    KVCache.put(\"double\", d)\n    Log.d(TAG, \"$cacheProvider: double = ${KVCache.getDouble(\"double\")}\")\n\n    val l = 4L\n    KVCache.put(\"long\", l)\n    Log.d(TAG, \"$cacheProvider: long = ${KVCache.getLong(\"long\")}\")\n\n    val b = true\n    KVCache.put(\"boolean\", b)\n    Log.d(TAG, \"$cacheProvider: boolean = ${KVCache.getBoolean(\"boolean\")}\")\n\n    val s = KVCache::class.java.simpleName\n    KVCache.put(\"string\", s)\n    Log.d(TAG, \"$cacheProvider: string = ${KVCache.getString(\"string\")}\")\n\n    val stringSet = setOf(\"A\", \"B\", \"C\")\n    KVCache.put(\"stringSet\", stringSet)\n    Log.d(TAG, \"$cacheProvider: stringSet = ${KVCache.getStringSet(\"stringSet\")}\")\n\n    val byteArray: ByteArray = byteArrayOf(1, 2, 3)\n    KVCache.put(\"byteArray\", byteArray)\n    Log.d(TAG, \"$cacheProvider: byteArray = ${KVCache.getByteArray(\"byteArray\")?.toList()}\")\n\n    val p = ParcelableBean(\"ParcelableBean\", 10, true)\n    KVCache.put(\"parcelable\", p)\n    Log.d(TAG, \"$cacheProvider: parcelable = ${KVCache.getParcelable\u003cParcelableBean\u003e(\"parcelable\")}\")\n\n```\n\n### KVCache 属性委托使用示例\n\nKVCache属性委托目前定义的函数都是 `kvCache` 开头，使用起来也比较简单；\n\n示例如下：\n\n```kotlin\n\n    // 属性委托：相当于 KVCache.getInt(\"arg1\"); 类型为：Int（key的默认值如果忽略或为空时，则默认值为变量的名称）\n    var arg1 by kvCacheInt()\n    // 属性委托：相当于 KVCache.getFloat(\"param1\"); 类型为：Float（默认值为：0F）\n    var arg2 : Float by kvCache(\"argFloat\", 0F)\n    // 属性委托：相当于 KVCache.getDouble(\"arg3\"); 类型为：Double（key的默认值如果忽略或为空时，则默认值为变量的名称）\n    var arg3 by kvCache(defaultValue =  0.0)\n    // 函数 kvCache 与 kvCacheXXX （XXX: 表示类型，如：kvCacheInt）用法基本一致，只是表现形式不同，由于 kvCache 有多个函数名称相同，所以需要传默认值，根据默认值的类型来推断使用的是哪个函数\n    var arg4 by kvCache(\"argBool\", false)\n    // 这里让 arg4 和 arg5 指向相同的key\n    var arg5 by kvCacheBoolean(\"argBool\")\n\n\n    //... 使用\n\n    // 属性委托：arg1 = 5 相当于：KVCache.put(\"arg1\", 5)，再打印查看 KVCache.getInt(\"arg1\") 的值\n    arg1 = 5\n    Log.d(TAG, \"$cacheProvider: kvCache -\u003e arg1 = ${KVCache.getInt(\"arg1\")}\")\n\n    // 属性委托：arg2 = 6F 相当于：KVCache.put(\"argFloat\", 6F)，再打印查看 KVCache.getFloat(\"argFloat\") 的值\n    arg2 = 6F\n    Log.d(TAG, \"$cacheProvider: kvCache -\u003e arg2 = ${KVCache.getFloat(\"argFloat\")}\")\n\n    // 属性委托：通过 KVCache 缓存值后，再打印查看 arg3 的值; 相当于：属性的 getValue 取 KVCache.getDouble(\"arg3\") 的值\n    KVCache.put(\"arg3\", 7.0)\n    Log.d(TAG, \"$cacheProvider: kvCache -\u003e arg3 = $arg3\")\n\n    // 属性委托：arg4 = true 相当于：KVCache.put(\"argBool\", true)，再打印查看 KVCache.getBoolean(\"argBool\") 的值\n    arg4 = true\n    Log.d(TAG, \"$cacheProvider: kvCache -\u003e arg4 = ${KVCache.getBoolean(\"argBool\")}\")\n\n    // 因为 arg4 和 arg5 使用相同的 key，所以改变 arg4 = true 后，arg5 的值也将改变；即都是取的 KVCache.getBoolean(\"argBool\") 的值\n    Log.d(TAG, \"$cacheProvider: kvCache -\u003e arg5 = $arg5\")\n```\n\n### 测试日志\n```logcatfilter\n// ---------------- Provider: null\nCacheProvider: MMKVCache\nMMKVCache: float = 1.0\nMMKVCache: int = 2\nMMKVCache: double = 3.0\nMMKVCache: long = 4\nMMKVCache: boolean = true\nMMKVCache: string = KVCache\nMMKVCache: stringSet = [A, B, C]\nMMKVCache: byteArray = [1, 2, 3]\nMMKVCache: parcelable = ParcelableBean(name=ParcelableBean, i=10, bool=true)\n// -------- kvCache\nMMKVCache: kvCache -\u003e arg1 = 5\nMMKVCache: kvCache -\u003e arg2 = 6.0\nMMKVCache: kvCache -\u003e arg3 = 7.0\nMMKVCache: kvCache -\u003e arg4 = true\nMMKVCache: kvCache -\u003e arg5 = true\n// ------------------------------------------------ //\n// ---------------- Provider: MMKVCache\nCacheProvider: MMKVCache\nMMKVCache: float = 1.0\nMMKVCache: int = 2\nMMKVCache: double = 3.0\nMMKVCache: long = 4\nMMKVCache: boolean = true\nMMKVCache: string = KVCache\nMMKVCache: stringSet = [A, B, C]\nMMKVCache: byteArray = [1, 2, 3]\nMMKVCache: parcelable = ParcelableBean(name=ParcelableBean, i=10, bool=true)\n// -------- kvCache\nMMKVCache: kvCache -\u003e arg1 = 5\nMMKVCache: kvCache -\u003e arg2 = 6.0\nMMKVCache: kvCache -\u003e arg3 = 7.0\nMMKVCache: kvCache -\u003e arg4 = true\nMMKVCache: kvCache -\u003e arg5 = true\n// ------------------------------------------------ //\n// ---------------- Provider: DataStoreCache\nCacheProvider: DataStoreCache\nDataStoreCache: float = 1.0\nDataStoreCache: int = 2\nDataStoreCache: double = 3.0\nDataStoreCache: long = 4\nDataStoreCache: boolean = true\nDataStoreCache: string = KVCache\nDataStoreCache: stringSet = [A, B, C]\nDataStoreCache: byteArray = [1, 2, 3]\nDataStoreCache: parcelable = ParcelableBean(name=ParcelableBean, i=10, bool=true)\n// -------- kvCache\nDataStoreCache: kvCache -\u003e arg1 = 5\nDataStoreCache: kvCache -\u003e arg2 = 6.0\nDataStoreCache: kvCache -\u003e arg3 = 7.0\nDataStoreCache: kvCache -\u003e arg4 = true\nDataStoreCache: kvCache -\u003e arg5 = true\n// ------------------------------------------------ //\n// ---------------- Provider: SharedPreferencesCache\nCacheProvider: SharedPreferencesCache\nSharedPreferencesCache: float = 1.0\nSharedPreferencesCache: int = 2\nSharedPreferencesCache: double = 3.0\nSharedPreferencesCache: long = 4\nSharedPreferencesCache: boolean = true\nSharedPreferencesCache: string = KVCache\nSharedPreferencesCache: stringSet = [A, B, C]\nSharedPreferencesCache: byteArray = [1, 2, 3]\nSharedPreferencesCache: parcelable = ParcelableBean(name=ParcelableBean, i=10, bool=true)\n// -------- kvCache\nSharedPreferencesCache: kvCache -\u003e arg1 = 5\nSharedPreferencesCache: kvCache -\u003e arg2 = 6.0\nSharedPreferencesCache: kvCache -\u003e arg3 = 7.0\nSharedPreferencesCache: kvCache -\u003e arg4 = true\nSharedPreferencesCache: kvCache -\u003e arg5 = true\n// ------------------------------------------------ //\n// ---------------- Provider: MemoryCache\nCacheProvider: MemoryCache\nMemoryCache: float = 1.0\nMemoryCache: int = 2\nMemoryCache: double = 3.0\nMemoryCache: long = 4\nMemoryCache: boolean = true\nMemoryCache: string = KVCache\nMemoryCache: stringSet = [A, B, C]\nMemoryCache: byteArray = [1, 2, 3]\nMemoryCache: parcelable = ParcelableBean(name=ParcelableBean, i=10, bool=true)\n// -------- kvCache\nMemoryCache: kvCache -\u003e arg1 = 5\nMemoryCache: kvCache -\u003e arg2 = 6.0\nMemoryCache: kvCache -\u003e arg3 = 7.0\nMemoryCache: kvCache -\u003e arg4 = true\nMemoryCache: kvCache -\u003e arg5 = true\n// ------------------------------------------------ //\n```\n\n更多使用详情，请查看[app](app)中的源码使用示例或直接查看[API帮助文档](https://jenly1314.github.io/KVCache/api/)\n\n## 相关推荐\n\n#### [AndroidKTX](https://github.com/AndroidKTX/AndroidKTX) 一个简化 Android 开发的 Kotlin 工具类集\n\n\u003c!-- end --\u003e\n\n## 版本日志\n\n#### v1.3.0：2024-2-3\n* 更新Gradle至v8.0\n* 新增 `getOrDefault` 函数\n* 优化Java调用方式\n\n#### v1.2.0：2023-2-26\n* 支持清空缓存（新增 `clear()` 函数）\n* 优化细节\n\n#### v1.1.0：2022-12-16\n* 新增`MemoryCache`\n\n#### v1.0.1：2022-7-21\n* 支持属性委托\n\n#### v1.0.0：2022-7-19\n* KVCache初始版本\n\n## 赞赏\n如果你喜欢KVCache，或感觉KVCache帮助到了你，可以点右上角“Star”支持一下，你的支持就是我的动力，谢谢 :smiley:\n\u003cp\u003e您也可以扫描下面的二维码，请作者喝杯咖啡 :coffee:\n\n\u003cdiv\u003e\n   \u003cimg src=\"https://jenly1314.github.io/image/page/rewardcode.png\"\u003e\n\u003c/div\u003e\n\n## 关于我\n\n| 我的博客                                                                                | GitHub                                                                                  | Gitee                                                                                  | CSDN                                                                                 | 博客园                                                                            |\n|:------------------------------------------------------------------------------------|:----------------------------------------------------------------------------------------|:---------------------------------------------------------------------------------------|:-------------------------------------------------------------------------------------|:-------------------------------------------------------------------------------|\n| \u003ca title=\"我的博客\" href=\"https://jenly1314.github.io\" target=\"_blank\"\u003eJenly's Blog\u003c/a\u003e | \u003ca title=\"GitHub开源项目\" href=\"https://github.com/jenly1314\" target=\"_blank\"\u003ejenly1314\u003c/a\u003e | \u003ca title=\"Gitee开源项目\" href=\"https://gitee.com/jenly1314\" target=\"_blank\"\u003ejenly1314\u003c/a\u003e  | \u003ca title=\"CSDN博客\" href=\"http://blog.csdn.net/jenly121\" target=\"_blank\"\u003ejenly121\u003c/a\u003e  | \u003ca title=\"博客园\" href=\"https://www.cnblogs.com/jenly\" target=\"_blank\"\u003ejenly\u003c/a\u003e  |\n\n## 联系我\n\n| 微信公众号        | Gmail邮箱                                                                          | QQ邮箱                                                                              | QQ群                                                                                                                       | QQ群                                                                                                                       |\n|:-------------|:---------------------------------------------------------------------------------|:----------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------------------------|\n| [Jenly666](http://weixin.qq.com/r/wzpWTuPEQL4-ract92-R) | \u003ca title=\"给我发邮件\" href=\"mailto:jenly1314@gmail.com\" target=\"_blank\"\u003ejenly1314\u003c/a\u003e | \u003ca title=\"给我发邮件\" href=\"mailto:jenly1314@vip.qq.com\" target=\"_blank\"\u003ejenly1314\u003c/a\u003e | \u003ca title=\"点击加入QQ群\" href=\"https://qm.qq.com/cgi-bin/qm/qr?k=6_RukjAhwjAdDHEk2G7nph-o8fBFFzZz\" target=\"_blank\"\u003e20867961\u003c/a\u003e | \u003ca title=\"点击加入QQ群\" href=\"https://qm.qq.com/cgi-bin/qm/qr?k=Z9pobM8bzAW7tM_8xC31W8IcbIl0A-zT\" target=\"_blank\"\u003e64020761\u003c/a\u003e |\n\n\u003cdiv\u003e\n   \u003cimg src=\"https://jenly1314.github.io/image/page/footer.png\"\u003e\n\u003c/div\u003e\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjenly1314%2Fkvcache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjenly1314%2Fkvcache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjenly1314%2Fkvcache/lists"}