├── .gitignore ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── safframework │ │ └── rxcache4a │ │ └── demo │ │ └── MainActivity.java │ └── res │ ├── drawable-v24 │ └── ic_launcher_foreground.xml │ ├── drawable │ └── ic_launcher_background.xml │ ├── layout │ └── activity_main.xml │ ├── mipmap-anydpi-v26 │ ├── ic_launcher.xml │ └── ic_launcher_round.xml │ ├── mipmap-hdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-mdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ └── values │ ├── colors.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── rxcache4a ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── safframework │ │ │ └── rxcache4a │ │ │ └── memory │ │ │ └── impl │ │ │ └── LRUCacheImpl.java │ └── res │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── com │ └── safframework │ └── rxcache4a │ └── ExampleUnitTest.java ├── rxcache4a_greenDAO ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── safframework │ │ └── rxcache4a │ │ └── persistence │ │ └── db │ │ ├── Address.java │ │ ├── GreenDAOImplTest.java │ │ └── User.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── safframework │ │ │ └── rxcache4a │ │ │ └── persistence │ │ │ └── db │ │ │ └── greendao │ │ │ ├── CacheEntity.java │ │ │ ├── DBService.java │ │ │ └── GreenDAOImpl.java │ └── res │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── com │ └── safframework │ └── rxcache4a │ └── persistence │ └── db │ └── ExampleUnitTest.java ├── rxcache4a_livedata ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── safframework │ │ └── rxcache4a │ │ └── livedata │ │ ├── Address.java │ │ ├── LiveDataCacheTest.kt │ │ └── User.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── safframework │ │ │ └── rxcache4a │ │ │ └── livedata │ │ │ └── LiveDataCache.kt │ └── res │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── com │ └── safframework │ └── rxcache4a │ └── livedata │ └── ExampleUnitTest.java ├── rxcache4a_mmkv ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── safframework │ │ └── rxcache4a │ │ ├── MMKVCacheImplTest.java │ │ └── User.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── safframework │ │ │ └── rxcache4a │ │ │ └── persistence │ │ │ └── disk │ │ │ └── MMKVImpl.java │ └── res │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── com │ └── safframework │ └── rxcache4a │ └── ExampleUnitTest.java ├── rxcache4a_objectbox ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── safframework │ │ └── rxcache4a │ │ └── persistence │ │ └── db │ │ └── objectbox │ │ ├── Address.java │ │ ├── ObjectBoxImplTest.java │ │ └── User.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── safframework │ │ │ └── rxcache4a │ │ │ └── persistence │ │ │ └── db │ │ │ └── objectbox │ │ │ ├── CacheEntity.java │ │ │ └── ObjectBoxImpl.java │ └── res │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── com │ └── safframework │ └── rxcache4a │ └── persistence │ └── db │ └── objectbox │ └── ExampleUnitTest.java ├── rxcache4a_room ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── safframework │ │ └── rxcache4a │ │ └── persistence │ │ └── db │ │ ├── Address.java │ │ ├── RoomImplTest.java │ │ └── User.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── safframework │ │ │ └── rxcache4a │ │ │ └── persistence │ │ │ └── db │ │ │ └── room │ │ │ ├── AppDatabase.java │ │ │ ├── CacheEntity.java │ │ │ ├── CacheEntityDao.java │ │ │ └── RoomImpl.java │ └── res │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── com │ └── safframework │ └── rxcache4a │ └── persistence │ └── db │ └── ExampleUnitTest.java └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RxCache4a 2 | 3 | [![@Tony沈哲 on weibo](https://img.shields.io/badge/weibo-%40Tony%E6%B2%88%E5%93%B2-blue.svg)](http://www.weibo.com/fengzhizi715) 4 | [![License](https://img.shields.io/badge/license-Apache%202-lightgrey.svg)](https://www.apache.org/licenses/LICENSE-2.0.html) 5 | [![](https://jitpack.io/v/fengzhizi715/RxCache4a.svg)](https://jitpack.io/#fengzhizi715/RxCache4a) 6 | 7 | # 最新版本 8 | 9 | 模块|最新版本 10 | ---|:-------------: 11 | rxcache4a|[![](https://jitpack.io/v/fengzhizi715/RxCache4a.svg)](https://jitpack.io/#fengzhizi715/RxCache4a) 12 | rxcache4a-greenDAO|[![](https://jitpack.io/v/fengzhizi715/RxCache4a.svg)](https://jitpack.io/#fengzhizi715/RxCache4a) 13 | rxcache4a-mmkv|[![](https://jitpack.io/v/fengzhizi715/RxCache4a.svg)](https://jitpack.io/#fengzhizi715/RxCache4a) 14 | rxcache4a-room|[![](https://jitpack.io/v/fengzhizi715/RxCache4a.svg)](https://jitpack.io/#fengzhizi715/RxCache4a) 15 | 16 | 详见:https://www.jianshu.com/p/308d503e7482 17 | 18 | # 下载: 19 | 20 | 将它添加到项目的 root build.gradle 中: 21 | 22 | ```groovy 23 | allprojects { 24 | repositories { 25 | ... 26 | maven { url 'https://jitpack.io' } 27 | } 28 | } 29 | ``` 30 | 31 | rxcache4a 32 | 33 | ```groovy 34 | implementation 'com.github.fengzhizi715.RxCache4a:rxcache4a:' 35 | ``` 36 | 37 | rxcache4a-greenDAO 38 | 39 | ```groovy 40 | implementation 'com.github.fengzhizi715.RxCache4a:rxcache4a_greenDAO:' 41 | ``` 42 | 43 | rxcache4a-mmkv 44 | 45 | ```groovy 46 | implementation 'com.github.fengzhizi715.RxCache4a:rxcache4a_mmkv:' 47 | ``` 48 | 49 | rxcache4a-room 50 | 51 | ```groovy 52 | implementation 'com.github.fengzhizi715.RxCache4a:rxcache4a_room:' 53 | ``` 54 | 55 | # 联系方式: 56 | 57 | Wechat:fengzhizi715 58 | 59 | > Java与Android技术栈:每周更新推送原创技术文章,欢迎扫描下方的公众号二维码并关注,期待与您的共同成长和进步。 60 | 61 | ![](https://github.com/fengzhizi715/NetDiscovery/blob/master/images/gzh.jpeg) 62 | 63 | 64 | License 65 | ------- 66 | 67 | Copyright (C) 2018 - present, Tony Shen. 68 | 69 | Licensed under the Apache License, Version 2.0 (the "License"); 70 | you may not use this file except in compliance with the License. 71 | You may obtain a copy of the License at 72 | 73 | http://www.apache.org/licenses/LICENSE-2.0 74 | 75 | Unless required by applicable law or agreed to in writing, software 76 | distributed under the License is distributed on an "AS IS" BASIS, 77 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 78 | See the License for the specific language governing permissions and 79 | limitations under the License. 80 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'org.greenrobot.greendao' 3 | 4 | def cfg = rootProject.ext.configuration // 配置 5 | def libs = rootProject.ext.libraries // 库 6 | 7 | android { 8 | compileSdkVersion cfg.compileVersion 9 | buildToolsVersion cfg.buildToolsVersion 10 | 11 | defaultConfig { 12 | applicationId "com.safframework.rxcache4a.demo" 13 | minSdkVersion cfg.minSdk 14 | targetSdkVersion cfg.targetSdk 15 | versionCode 1 16 | versionName "1.0" 17 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 18 | 19 | javaCompileOptions { 20 | annotationProcessorOptions { 21 | includeCompileClasspath true 22 | } 23 | } 24 | } 25 | buildTypes { 26 | release { 27 | minifyEnabled false 28 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 29 | } 30 | } 31 | compileOptions { 32 | sourceCompatibility 1.8 33 | targetCompatibility 1.8 34 | } 35 | } 36 | 37 | dependencies { 38 | implementation fileTree(dir: 'libs', include: ['*.jar']) 39 | implementation "androidx.appcompat:appcompat:${libs.androidx_appcompat}" 40 | implementation "androidx.constraintlayout:constraintlayout:2.1.3" 41 | testImplementation 'junit:junit:4.12' 42 | implementation "androidx.core:core-ktx:1.3.2" 43 | } 44 | 45 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/safframework/rxcache4a/demo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a.demo; 2 | 3 | import android.os.Bundle; 4 | 5 | import androidx.appcompat.app.AppCompatActivity; 6 | 7 | public class MainActivity extends AppCompatActivity { 8 | 9 | @Override 10 | protected void onCreate(Bundle savedInstanceState) { 11 | super.onCreate(savedInstanceState); 12 | setContentView(R.layout.activity_main); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 17 | 18 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengzhizi715/RxCache4a/a56f5f206050c9651eb6b74b6d97f8a9db7b1043/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengzhizi715/RxCache4a/a56f5f206050c9651eb6b74b6d97f8a9db7b1043/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengzhizi715/RxCache4a/a56f5f206050c9651eb6b74b6d97f8a9db7b1043/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengzhizi715/RxCache4a/a56f5f206050c9651eb6b74b6d97f8a9db7b1043/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengzhizi715/RxCache4a/a56f5f206050c9651eb6b74b6d97f8a9db7b1043/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengzhizi715/RxCache4a/a56f5f206050c9651eb6b74b6d97f8a9db7b1043/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengzhizi715/RxCache4a/a56f5f206050c9651eb6b74b6d97f8a9db7b1043/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengzhizi715/RxCache4a/a56f5f206050c9651eb6b74b6d97f8a9db7b1043/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengzhizi715/RxCache4a/a56f5f206050c9651eb6b74b6d97f8a9db7b1043/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengzhizi715/RxCache4a/a56f5f206050c9651eb6b74b6d97f8a9db7b1043/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #008577 4 | #00574B 5 | #D81B60 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | RxCache4a 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | ext { 3 | configuration = [ 4 | buildToolsVersion: "29.0.2", 5 | compileVersion : 29, 6 | minSdk : 15, 7 | targetSdk : 29 8 | ] 9 | 10 | libraries = [ 11 | androidx_appcompat : "1.3.1", 12 | androidx_junit : "1.1.3", 13 | androidx_espresso : "3.4.0", 14 | 15 | rxcache_core : "v2.1.4", 16 | bytekit : "v1.3.0" 17 | ] 18 | } 19 | 20 | buildscript { 21 | ext.objectboxVersion = '3.1.1' 22 | ext.kotlin_version = '1.5.31' 23 | ext.rxcache4a_version = '1.1.0' 24 | 25 | repositories { 26 | google() 27 | mavenCentral() 28 | maven { url "https://jitpack.io" } 29 | } 30 | dependencies { 31 | classpath 'com.android.tools.build:gradle:4.2.2' 32 | classpath 'org.greenrobot:greendao-gradle-plugin:3.3.0' 33 | classpath "io.objectbox:objectbox-gradle-plugin:$objectboxVersion" 34 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 35 | 36 | // NOTE: Do not place your application dependencies here; they belong 37 | // in the individual module build.gradle files 38 | } 39 | } 40 | 41 | allprojects { 42 | repositories { 43 | google() 44 | maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' } 45 | maven { url "https://jitpack.io" } 46 | mavenCentral() 47 | } 48 | 49 | //加上这些 50 | tasks.withType(Javadoc) { 51 | options{ encoding "UTF-8" 52 | charSet 'UTF-8' 53 | links "http://docs.oracle.com/javase/7/docs/api" 54 | } 55 | } 56 | } 57 | 58 | task clean(type: Delete) { 59 | delete rootProject.buildDir 60 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | 15 | android.useAndroidX=true -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengzhizi715/RxCache4a/a56f5f206050c9651eb6b74b6d97f8a9db7b1043/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sat Jul 04 22:42:58 CST 2020 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /rxcache4a/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /rxcache4a/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | def cfg = rootProject.ext.configuration // 配置 4 | def libs = rootProject.ext.libraries // 库 5 | 6 | android { 7 | compileSdkVersion cfg.compileVersion 8 | buildToolsVersion cfg.buildToolsVersion 9 | 10 | defaultConfig { 11 | minSdkVersion cfg.minSdk 12 | targetSdkVersion cfg.targetSdk 13 | versionCode 1 14 | versionName "1.0" 15 | 16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 17 | } 18 | 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | 26 | lintOptions { 27 | abortOnError false 28 | } 29 | 30 | compileOptions { 31 | sourceCompatibility 1.8 32 | targetCompatibility 1.8 33 | } 34 | } 35 | 36 | dependencies { 37 | implementation fileTree(dir: 'libs', include: ['*.jar']) 38 | 39 | implementation "androidx.appcompat:appcompat:${libs.androidx_appcompat}" 40 | testImplementation 'junit:junit:4.12' 41 | androidTestImplementation "androidx.test.ext:junit:${libs.androidx_junit}" 42 | androidTestImplementation "androidx.test.espresso:espresso-core:${libs.androidx_espresso}" 43 | 44 | implementation "com.github.fengzhizi715.RxCache:core:${libs.rxcache_core}" 45 | } 46 | 47 | repositories { 48 | mavenCentral() 49 | maven { url "https://jitpack.io" } 50 | } 51 | 52 | allprojects { 53 | repositories { 54 | mavenCentral() 55 | maven { url "https://jitpack.io" } 56 | } 57 | //加上这些 58 | tasks.withType(Javadoc) { 59 | options{ encoding "UTF-8" 60 | charSet 'UTF-8' 61 | links "http://docs.oracle.com/javase/7/docs/api" 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /rxcache4a/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /rxcache4a/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /rxcache4a/src/main/java/com/safframework/rxcache4a/memory/impl/LRUCacheImpl.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a.memory.impl; 2 | 3 | import android.util.LruCache; 4 | 5 | import com.safframework.rxcache.config.Constant; 6 | import com.safframework.rxcache.domain.CacheStatistics; 7 | import com.safframework.rxcache.domain.Record; 8 | import com.safframework.rxcache.domain.Source; 9 | import com.safframework.rxcache.memory.impl.AbstractMemoryImpl; 10 | 11 | import java.util.Set; 12 | 13 | /** 14 | * @FileName: com.safframework.rxcache4a.memory.impl.LRUCacheImpl 15 | * @author: Tony Shen 16 | * @date: 2018-10-03 00:17 17 | * @version: V1.0 <描述当前版本功能> 18 | */ 19 | public class LRUCacheImpl extends AbstractMemoryImpl { 20 | 21 | private LruCache cache; 22 | 23 | public LRUCacheImpl(int maxSize) { 24 | 25 | super(maxSize); 26 | 27 | int memCacheSize = (int) (Runtime.getRuntime().maxMemory() / (1024 * 8)); 28 | cache = new LruCache(memCacheSize); 29 | } 30 | 31 | @Override 32 | public Record getIfPresent(String key) { 33 | 34 | T result = null; 35 | if (expireTimeMap.get(key) != null) { 36 | if (expireTimeMap.get(key) < 0) { // 缓存的数据从不过期 37 | 38 | result = (T) cache.get(key); 39 | } else { 40 | 41 | if (timestampMap.get(key) + expireTimeMap.get(key) > System.currentTimeMillis()) { // 缓存的数据还没有过期 42 | 43 | result = (T) cache.get(key); 44 | } else { // 缓存的数据已经过期 45 | 46 | evict(key); 47 | } 48 | } 49 | } 50 | return result != null ? new Record<>(Source.MEMORY, key, result, timestampMap.get(key), expireTimeMap.get(key)) : null; 51 | } 52 | 53 | @Override 54 | public void put(String key, T value) { 55 | 56 | put(key, value, Constant.NEVER_EXPIRE); 57 | } 58 | 59 | @Override 60 | public void put(String key, T value, long expireTime) { 61 | 62 | cache.put(key, value); 63 | timestampMap.put(key, System.currentTimeMillis()); 64 | expireTimeMap.put(key, expireTime); 65 | } 66 | 67 | @Override 68 | public Set keySet() { 69 | 70 | return cache.snapshot().keySet(); 71 | } 72 | 73 | @Override 74 | public boolean containsKey(String key) { 75 | 76 | return cache.snapshot().containsKey(key); 77 | } 78 | 79 | @Override 80 | public void evict(String key) { 81 | 82 | cache.remove(key); 83 | 84 | timestampMap.remove(key); 85 | expireTimeMap.remove(key); 86 | } 87 | 88 | @Override 89 | public void evictAll() { 90 | 91 | cache.evictAll(); 92 | 93 | timestampMap.clear(); 94 | expireTimeMap.clear(); 95 | } 96 | 97 | public CacheStatistics getCacheStatistics() { 98 | 99 | return new CacheStatistics((int)maxSize, cache.putCount(), cache.evictionCount(), cache.hitCount(), cache.missCount()); 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /rxcache4a/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | library 3 | 4 | -------------------------------------------------------------------------------- /rxcache4a/src/test/java/com/safframework/rxcache4a/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /rxcache4a_greenDAO/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /rxcache4a_greenDAO/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'org.greenrobot.greendao' 3 | 4 | def cfg = rootProject.ext.configuration // 配置 5 | def libs = rootProject.ext.libraries // 库 6 | 7 | android { 8 | compileSdkVersion cfg.compileVersion 9 | buildToolsVersion cfg.buildToolsVersion 10 | 11 | defaultConfig { 12 | minSdkVersion cfg.minSdk 13 | targetSdkVersion cfg.targetSdk 14 | versionCode 1 15 | versionName "1.0" 16 | 17 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 18 | } 19 | 20 | buildTypes { 21 | release { 22 | minifyEnabled false 23 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 24 | } 25 | } 26 | 27 | lintOptions { 28 | abortOnError false 29 | } 30 | 31 | compileOptions { 32 | sourceCompatibility 1.8 33 | targetCompatibility 1.8 34 | } 35 | } 36 | 37 | dependencies { 38 | implementation fileTree(dir: 'libs', include: ['*.jar']) 39 | 40 | implementation "androidx.appcompat:appcompat:${libs.androidx_appcompat}" 41 | testImplementation 'junit:junit:4.12' 42 | androidTestImplementation "androidx.test.ext:junit:${libs.androidx_junit}" 43 | androidTestImplementation "androidx.test.espresso:espresso-core:${libs.androidx_espresso}" 44 | 45 | implementation "com.github.fengzhizi715.RxCache:core:${libs.rxcache_core}" 46 | implementation "com.github.fengzhizi715.bytekit:core:${libs.bytekit}" 47 | implementation 'org.greenrobot:greendao:3.3.0' 48 | } 49 | 50 | repositories { 51 | mavenCentral() 52 | maven { url "https://jitpack.io" } 53 | } 54 | 55 | allprojects { 56 | repositories { 57 | mavenCentral() 58 | maven { url "https://jitpack.io" } 59 | } 60 | //加上这些 61 | tasks.withType(Javadoc) { 62 | options{ encoding "UTF-8" 63 | charSet 'UTF-8' 64 | links "http://docs.oracle.com/javase/7/docs/api" 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /rxcache4a_greenDAO/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /rxcache4a_greenDAO/src/androidTest/java/com/safframework/rxcache4a/persistence/db/Address.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a.persistence.db; 2 | 3 | /** 4 | * @FileName: com.safframework.rxcache4a.persistence.db.Address 5 | * @author: Tony Shen 6 | * @date: 2018-10-15 21:28 7 | * @version: V1.0 <描述当前版本功能> 8 | */ 9 | public class Address { 10 | 11 | public String city; 12 | public String province; 13 | public String area; 14 | public String street; 15 | } 16 | -------------------------------------------------------------------------------- /rxcache4a_greenDAO/src/androidTest/java/com/safframework/rxcache4a/persistence/db/GreenDAOImplTest.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a.persistence.db; 2 | 3 | import android.content.Context; 4 | 5 | import androidx.test.InstrumentationRegistry; 6 | import androidx.test.ext.junit.runners.AndroidJUnit4; 7 | 8 | import com.safframework.rxcache.RxCache; 9 | import com.safframework.rxcache.domain.Record; 10 | import com.safframework.rxcache4a.persistence.db.greendao.CacheEntityDao; 11 | import com.safframework.rxcache4a.persistence.db.greendao.DBService; 12 | import com.safframework.rxcache4a.persistence.db.greendao.GreenDAOImpl; 13 | 14 | import org.junit.Before; 15 | import org.junit.Test; 16 | import org.junit.runner.RunWith; 17 | 18 | import static org.junit.Assert.assertEquals; 19 | import static org.junit.Assert.assertNull; 20 | 21 | /** 22 | * @FileName: com.safframework.rxcache4a.GreenDAOImplTest 23 | * @author: Tony Shen 24 | * @date: 2018-10-15 18:51 25 | * @version: V1.0 <描述当前版本功能> 26 | */ 27 | @RunWith(AndroidJUnit4.class) 28 | public class GreenDAOImplTest { 29 | 30 | Context appContext; 31 | DBService dbService; 32 | 33 | @Before 34 | public void setUp() { 35 | appContext = InstrumentationRegistry.getTargetContext(); 36 | dbService = DBService.getInstance(appContext); 37 | } 38 | 39 | @Test 40 | public void testWithObject() { 41 | 42 | CacheEntityDao dao = dbService.getCacheEntityDao(); 43 | GreenDAOImpl impl = new GreenDAOImpl(dao); 44 | impl.evictAll(); 45 | 46 | RxCache.config(new RxCache.Builder().persistence(impl)); 47 | 48 | RxCache rxCache = RxCache.getRxCache(); 49 | 50 | Address address = new Address(); 51 | address.province = "Jiangsu"; 52 | address.city = "Suzhou"; 53 | address.area = "Gusu"; 54 | address.street = "ren ming road"; 55 | 56 | User u = new User(); 57 | u.name = "tony"; 58 | u.password = "123456"; 59 | u.address = address; 60 | 61 | rxCache.save("user",u); 62 | 63 | Record record = rxCache.get("user", User.class); 64 | 65 | assertEquals(u.name, record.getData().name); 66 | assertEquals(u.password, record.getData().password); 67 | assertEquals(address.city, record.getData().address.city); 68 | 69 | rxCache.save("address",address); 70 | 71 | Record
record2 = rxCache.get("address", Address.class); 72 | assertEquals(address.city, record2.getData().city); 73 | } 74 | 75 | @Test 76 | public void testWithExpireTime() { 77 | 78 | CacheEntityDao dao = dbService.getCacheEntityDao(); 79 | GreenDAOImpl impl = new GreenDAOImpl(dao); 80 | impl.evictAll(); 81 | 82 | RxCache.config(new RxCache.Builder().persistence(impl)); 83 | 84 | RxCache rxCache = RxCache.getRxCache(); 85 | 86 | User u = new User(); 87 | u.name = "tony"; 88 | u.password = "123456"; 89 | rxCache.save("test",u,2000); 90 | 91 | try { 92 | Thread.sleep(2500); 93 | } catch (InterruptedException e) { 94 | e.printStackTrace(); 95 | } 96 | 97 | Record record = rxCache.get("test", User.class); 98 | 99 | assertNull(record); 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /rxcache4a_greenDAO/src/androidTest/java/com/safframework/rxcache4a/persistence/db/User.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a.persistence.db; 2 | 3 | import java.util.List; 4 | 5 | public class User { 6 | 7 | public String name; 8 | public String password; 9 | public Address address; 10 | } 11 | -------------------------------------------------------------------------------- /rxcache4a_greenDAO/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /rxcache4a_greenDAO/src/main/java/com/safframework/rxcache4a/persistence/db/greendao/CacheEntity.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a.persistence.db.greendao; 2 | 3 | import org.greenrobot.greendao.annotation.Entity; 4 | import org.greenrobot.greendao.annotation.Id; 5 | import org.greenrobot.greendao.annotation.Generated; 6 | 7 | /** 8 | * @FileName: com.safframework.rxcache4a.persistence.db.greendao.CacheEntity 9 | * @author: Tony Shen 10 | * @date: 2018-10-15 01:12 11 | * @version: V1.0 <描述当前版本功能> 12 | */ 13 | @Entity 14 | public class CacheEntity { 15 | 16 | @Id(autoincrement = true) 17 | private Long id; 18 | 19 | public String key; 20 | 21 | public String data;// 对象转换的 json 字符串 22 | 23 | public Long timestamp; 24 | 25 | public Long expireTime; 26 | 27 | @Generated(hash = 67720095) 28 | public CacheEntity(Long id, String key, String data, Long timestamp, 29 | Long expireTime) { 30 | this.id = id; 31 | this.key = key; 32 | this.data = data; 33 | this.timestamp = timestamp; 34 | this.expireTime = expireTime; 35 | } 36 | 37 | @Generated(hash = 1391258017) 38 | public CacheEntity() { 39 | } 40 | 41 | public Long getId() { 42 | return this.id; 43 | } 44 | 45 | public void setId(Long id) { 46 | this.id = id; 47 | } 48 | 49 | public String getKey() { 50 | return this.key; 51 | } 52 | 53 | public void setKey(String key) { 54 | this.key = key; 55 | } 56 | 57 | public String getData() { 58 | return this.data; 59 | } 60 | 61 | public void setData(String data) { 62 | this.data = data; 63 | } 64 | 65 | public Long getTimestamp() { 66 | return this.timestamp; 67 | } 68 | 69 | public void setTimestamp(Long timestamp) { 70 | this.timestamp = timestamp; 71 | } 72 | 73 | public Long getExpireTime() { 74 | return this.expireTime; 75 | } 76 | 77 | public void setExpireTime(Long expireTime) { 78 | this.expireTime = expireTime; 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /rxcache4a_greenDAO/src/main/java/com/safframework/rxcache4a/persistence/db/greendao/DBService.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a.persistence.db.greendao; 2 | 3 | import android.content.Context; 4 | 5 | /** 6 | * Created by Tony Shen on 2017/7/25. 7 | */ 8 | 9 | public class DBService { 10 | 11 | private static final String DB_NAME = "cache.db"; 12 | private static volatile DBService defaultInstance; 13 | private DaoSession daoSession; 14 | 15 | private DBService(Context context) { 16 | 17 | DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, DB_NAME); 18 | 19 | DaoMaster daoMaster = new DaoMaster(helper.getWritableDatabase()); 20 | 21 | daoSession = daoMaster.newSession(); 22 | } 23 | 24 | public static DBService getInstance(Context context) { 25 | if (defaultInstance == null) { 26 | synchronized (DBService.class) { 27 | if (defaultInstance == null) { 28 | defaultInstance = new DBService(context.getApplicationContext()); 29 | } 30 | } 31 | } 32 | return defaultInstance; 33 | } 34 | 35 | public CacheEntityDao getCacheEntityDao(){ 36 | return daoSession.getCacheEntityDao(); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /rxcache4a_greenDAO/src/main/java/com/safframework/rxcache4a/persistence/db/greendao/GreenDAOImpl.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a.persistence.db.greendao; 2 | 3 | import com.safframework.bytekit.utils.Preconditions; 4 | import com.safframework.rxcache.config.Constant; 5 | import com.safframework.rxcache.domain.Record; 6 | import com.safframework.rxcache.domain.Source; 7 | import com.safframework.rxcache.persistence.converter.Converter; 8 | import com.safframework.rxcache.persistence.converter.GsonConverter; 9 | import com.safframework.rxcache.persistence.db.DB; 10 | 11 | import java.lang.reflect.Type; 12 | import java.util.ArrayList; 13 | import java.util.HashSet; 14 | import java.util.List; 15 | import java.util.Set; 16 | 17 | /** 18 | * @FileName: com.safframework.rxcache4a.persistence.db.greendao.GreenDAOImpl 19 | * @author: Tony Shen 20 | * @date: 2018-10-15 11:50 21 | * @version: V1.0 <描述当前版本功能> 22 | */ 23 | public class GreenDAOImpl implements DB { 24 | 25 | private CacheEntityDao dao; 26 | private Converter converter; 27 | 28 | public GreenDAOImpl(CacheEntityDao dao) { 29 | 30 | this(dao,new GsonConverter()); 31 | } 32 | 33 | public GreenDAOImpl(CacheEntityDao dao, Converter converter) { 34 | 35 | this.dao = dao; 36 | this.converter = converter; 37 | } 38 | 39 | @Override 40 | public Record retrieve(String key, Type type) { 41 | 42 | CacheEntity entity = dao.queryBuilder().where(CacheEntityDao.Properties.Key.eq(key)).unique(); 43 | 44 | if (entity==null) return null; 45 | 46 | long timestamp = entity.timestamp; 47 | long expireTime = entity.expireTime; 48 | T result = null; 49 | 50 | if (expireTime<0) { // 缓存的数据从不过期 51 | 52 | String json = entity.data; 53 | 54 | result = converter.fromJson(json,type); 55 | } else { 56 | 57 | if (timestamp + expireTime > System.currentTimeMillis()) { // 缓存的数据还没有过期 58 | 59 | String json = entity.data; 60 | 61 | result = converter.fromJson(json,type); 62 | } else { // 缓存的数据已经过期 63 | 64 | evict(key); 65 | } 66 | } 67 | 68 | return result != null ? new Record<>(Source.PERSISTENCE, key, result, timestamp, expireTime) : null; 69 | } 70 | 71 | @Override 72 | public String getStringData(String key) { 73 | 74 | CacheEntity entity = dao.queryBuilder().where(CacheEntityDao.Properties.Key.eq(key)).unique(); 75 | 76 | if (entity==null) return null; 77 | 78 | long timestamp = entity.timestamp; 79 | long expireTime = entity.expireTime; 80 | String json = null; 81 | 82 | if (expireTime<0) { // 缓存的数据从不过期 83 | 84 | json = entity.data; 85 | } else { 86 | 87 | if (timestamp + expireTime > System.currentTimeMillis()) { // 缓存的数据还没有过期 88 | 89 | json = entity.data; 90 | } else { // 缓存的数据已经过期 91 | 92 | evict(key); 93 | } 94 | } 95 | 96 | return json; 97 | } 98 | 99 | @Override 100 | public void save(String key, T value) { 101 | 102 | save(key,value, Constant.NEVER_EXPIRE); 103 | } 104 | 105 | @Override 106 | public void save(String key, T value, long expireTime) { 107 | 108 | CacheEntity entity = new CacheEntity(); 109 | entity.setKey(key); 110 | entity.setTimestamp(System.currentTimeMillis()); 111 | entity.setExpireTime(expireTime); 112 | entity.setData(converter.toJson(value)); 113 | dao.save(entity); 114 | } 115 | 116 | @Override 117 | public Set keySet() { 118 | 119 | List list = dao.loadAll(); 120 | 121 | Set result = new HashSet<>(); 122 | 123 | for (CacheEntity entity:list) { 124 | 125 | result.add(entity.key); 126 | } 127 | 128 | return result; 129 | } 130 | 131 | @Override 132 | public boolean containsKey(String key) { 133 | 134 | Set keys = keySet(); 135 | 136 | return Preconditions.isNotBlank(keys) ? keys.contains(key) : false; 137 | } 138 | 139 | @Override 140 | public void evict(String key) { 141 | 142 | CacheEntity entity = dao.queryBuilder().where(CacheEntityDao.Properties.Key.eq(key)).unique(); 143 | 144 | if (entity!=null) { 145 | 146 | dao.delete(entity); 147 | } 148 | 149 | } 150 | 151 | @Override 152 | public void evictAll() { 153 | 154 | dao.deleteAll(); 155 | } 156 | } 157 | -------------------------------------------------------------------------------- /rxcache4a_greenDAO/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | rxcache4a_greendao 3 | 4 | -------------------------------------------------------------------------------- /rxcache4a_greenDAO/src/test/java/com/safframework/rxcache4a/persistence/db/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a.persistence.db; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /rxcache4a_livedata/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /rxcache4a_livedata/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'kotlin-android' 3 | 4 | def cfg = rootProject.ext.configuration // 配置 5 | def libs = rootProject.ext.libraries // 库 6 | 7 | android { 8 | compileSdkVersion cfg.compileVersion 9 | buildToolsVersion cfg.buildToolsVersion 10 | 11 | defaultConfig { 12 | minSdkVersion cfg.minSdk 13 | targetSdkVersion cfg.targetSdk 14 | versionCode 1 15 | versionName "1.0" 16 | 17 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 18 | } 19 | 20 | buildTypes { 21 | release { 22 | minifyEnabled false 23 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 24 | } 25 | } 26 | 27 | packagingOptions { 28 | exclude 'META-INF/proguard/androidx-annotations.pro' 29 | } 30 | 31 | lintOptions { 32 | abortOnError false 33 | } 34 | 35 | compileOptions { 36 | sourceCompatibility 1.8 37 | targetCompatibility 1.8 38 | } 39 | } 40 | 41 | dependencies { 42 | implementation fileTree(dir: 'libs', include: ['*.jar']) 43 | 44 | implementation "androidx.appcompat:appcompat:${libs.androidx_appcompat}" 45 | testImplementation 'junit:junit:4.12' 46 | androidTestImplementation "androidx.test.ext:junit:${libs.androidx_junit}" 47 | androidTestImplementation "androidx.test.espresso:espresso-core:${libs.androidx_espresso}" 48 | 49 | implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 50 | implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" 51 | 52 | implementation "com.github.fengzhizi715.RxCache:core:${libs.rxcache_core}" 53 | implementation "androidx.lifecycle:lifecycle-livedata:2.3.0" 54 | 55 | } 56 | 57 | repositories { 58 | mavenCentral() 59 | maven { url "https://jitpack.io" } 60 | } 61 | 62 | allprojects { 63 | repositories { 64 | mavenCentral() 65 | maven { url "https://jitpack.io" } 66 | } 67 | //加上这些 68 | tasks.withType(Javadoc) { 69 | options{ encoding "UTF-8" 70 | charSet 'UTF-8' 71 | links "http://docs.oracle.com/javase/7/docs/api" 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /rxcache4a_livedata/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /rxcache4a_livedata/src/androidTest/java/com/safframework/rxcache4a/livedata/Address.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a.livedata; 2 | 3 | /** 4 | * @FileName: com.safframework.rxcache4a.persistence.db.Address 5 | * @author: Tony Shen 6 | * @date: 2018-10-15 21:28 7 | * @version: V1.0 <描述当前版本功能> 8 | */ 9 | public class Address { 10 | 11 | public String city; 12 | public String province; 13 | public String area; 14 | public String street; 15 | } 16 | -------------------------------------------------------------------------------- /rxcache4a_livedata/src/androidTest/java/com/safframework/rxcache4a/livedata/LiveDataCacheTest.kt: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a.livedata 2 | 3 | import android.content.Context 4 | import android.os.Handler 5 | import android.os.Looper 6 | import androidx.lifecycle.LiveData 7 | import androidx.lifecycle.Observer 8 | import androidx.test.ext.junit.runners.AndroidJUnit4 9 | import com.safframework.rxcache.RxCache 10 | import org.junit.Assert.assertEquals 11 | import org.junit.Before 12 | import org.junit.Test 13 | import org.junit.runner.RunWith 14 | import java.util.concurrent.CountDownLatch 15 | import java.util.concurrent.TimeUnit 16 | 17 | /** 18 | * 19 | * @FileName: 20 | * com.safframework.rxcache4a.livedata.LiveDataCacheTest 21 | * @author: Tony Shen 22 | * @date: 2018-10-24 14:13 23 | * @version: V1.0 <描述当前版本功能> 24 | */ 25 | @RunWith(AndroidJUnit4::class) 26 | class LiveDataCacheTest { 27 | 28 | lateinit var appContext: Context 29 | lateinit var handler: Handler 30 | 31 | @Before 32 | fun setUp() { 33 | appContext = androidx.test.InstrumentationRegistry.getTargetContext() 34 | Looper.prepare() 35 | handler = Handler() 36 | } 37 | 38 | @Test 39 | fun testWithObject() { 40 | 41 | RxCache.config(RxCache.Builder()) 42 | 43 | val rxCache = RxCache.getRxCache() 44 | 45 | val address = Address() 46 | address.province = "Jiangsu" 47 | address.city = "Suzhou" 48 | address.area = "Gusu" 49 | address.street = "ren ming road" 50 | 51 | val u = User() 52 | u.name = "tony" 53 | u.password = "123456" 54 | u.address = address 55 | 56 | rxCache.save("user", u) 57 | 58 | val livedata1 = rxCache.toLiveData().get("user", User::class.java) 59 | 60 | handler.post{ 61 | 62 | val u2 = livedata1.blockingObserve()!! 63 | 64 | assertEquals(u.name, u2.name) 65 | } 66 | 67 | val livedata2 = rxCache.toLiveData
().save("address",address) 68 | 69 | handler.post{ 70 | 71 | val address2 = livedata2.blockingObserve()!! 72 | 73 | assertEquals(address.city, address2.city) 74 | } 75 | 76 | } 77 | 78 | private fun LiveData.blockingObserve(): T? { 79 | 80 | var value: T? = null 81 | val latch = CountDownLatch(1) 82 | 83 | val observer = Observer { t -> 84 | value = t 85 | latch.countDown() 86 | } 87 | 88 | latch.await(2, TimeUnit.SECONDS) 89 | return value 90 | } 91 | 92 | } -------------------------------------------------------------------------------- /rxcache4a_livedata/src/androidTest/java/com/safframework/rxcache4a/livedata/User.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a.livedata; 2 | 3 | public class User { 4 | 5 | public String name; 6 | public String password; 7 | public Address address; 8 | } 9 | -------------------------------------------------------------------------------- /rxcache4a_livedata/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /rxcache4a_livedata/src/main/java/com/safframework/rxcache4a/livedata/LiveDataCache.kt: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a.livedata 2 | 3 | import androidx.lifecycle.LiveData 4 | import androidx.lifecycle.MediatorLiveData 5 | import androidx.lifecycle.MutableLiveData 6 | import com.safframework.rxcache.RxCache 7 | import com.safframework.rxcache.config.Constant 8 | import java.lang.reflect.Type 9 | 10 | /** 11 | * 12 | * @FileName: 13 | * com.safframework.rxcache4a.livedata.LiveDataCache.kt 14 | * @author: Tony Shen 15 | * @date: 2018-10-23 17:29 16 | * @version: V1.0 <描述当前版本功能> 17 | */ 18 | class LiveDataCache(private val cache: RxCache) { 19 | 20 | var mediatorLiveData: MediatorLiveData 21 | 22 | init { 23 | 24 | mediatorLiveData = MediatorLiveData() 25 | } 26 | 27 | fun get(key: String,type:Type): LiveData { 28 | 29 | val liveData = MutableLiveData() 30 | 31 | liveData.postValue(cache.get(key,type).data) 32 | 33 | mediatorLiveData.addSource(liveData){ 34 | 35 | mediatorLiveData.postValue(it) 36 | } 37 | 38 | return mediatorLiveData 39 | } 40 | 41 | fun save(key: String, value: T): LiveData = save(key,value, Constant.NEVER_EXPIRE) 42 | 43 | fun save(key: String, value: T, expireTime: Long): LiveData { 44 | 45 | val liveData = MutableLiveData() 46 | 47 | cache.save(key, value, expireTime) 48 | 49 | liveData.postValue(value) 50 | 51 | mediatorLiveData.addSource(liveData){ 52 | 53 | mediatorLiveData.postValue(it) 54 | } 55 | 56 | return mediatorLiveData 57 | } 58 | 59 | fun remove(key: String): LiveData { 60 | 61 | val liveData = MutableLiveData() 62 | 63 | cache.remove(key) 64 | 65 | mediatorLiveData.addSource(liveData){ 66 | mediatorLiveData.value = null 67 | } 68 | 69 | return mediatorLiveData 70 | } 71 | } 72 | 73 | fun RxCache.toLiveData() = LiveDataCache(this) -------------------------------------------------------------------------------- /rxcache4a_livedata/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | rxcache4a_livedata 3 | 4 | -------------------------------------------------------------------------------- /rxcache4a_livedata/src/test/java/com/safframework/rxcache4a/livedata/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a.livedata; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /rxcache4a_mmkv/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /rxcache4a_mmkv/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | def cfg = rootProject.ext.configuration // 配置 4 | def libs = rootProject.ext.libraries // 库 5 | 6 | android { 7 | compileSdkVersion cfg.compileVersion 8 | buildToolsVersion cfg.buildToolsVersion 9 | 10 | defaultConfig { 11 | minSdkVersion 16 12 | targetSdkVersion cfg.targetSdk 13 | versionCode 1 14 | versionName "1.0" 15 | 16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 17 | } 18 | 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | 26 | lintOptions { 27 | abortOnError false 28 | } 29 | 30 | compileOptions { 31 | sourceCompatibility 1.8 32 | targetCompatibility 1.8 33 | } 34 | } 35 | 36 | dependencies { 37 | implementation fileTree(dir: 'libs', include: ['*.jar']) 38 | 39 | implementation "androidx.appcompat:appcompat:${libs.androidx_appcompat}" 40 | testImplementation 'junit:junit:4.12' 41 | androidTestImplementation "androidx.test.ext:junit:${libs.androidx_junit}" 42 | androidTestImplementation "androidx.test.espresso:espresso-core:${libs.androidx_espresso}" 43 | 44 | implementation "com.github.fengzhizi715.RxCache:core:${libs.rxcache_core}" 45 | implementation 'com.tencent:mmkv-static:1.2.12' 46 | } 47 | 48 | repositories { 49 | mavenCentral() 50 | maven { url "https://jitpack.io" } 51 | } 52 | 53 | allprojects { 54 | repositories { 55 | mavenCentral() 56 | maven { url "https://jitpack.io" } 57 | } 58 | //加上这些 59 | tasks.withType(Javadoc) { 60 | options{ encoding "UTF-8" 61 | charSet 'UTF-8' 62 | links "http://docs.oracle.com/javase/7/docs/api" 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /rxcache4a_mmkv/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /rxcache4a_mmkv/src/androidTest/java/com/safframework/rxcache4a/MMKVCacheImplTest.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a; 2 | 3 | import android.content.Context; 4 | 5 | import androidx.test.InstrumentationRegistry; 6 | import androidx.test.ext.junit.runners.AndroidJUnit4; 7 | 8 | import com.safframework.rxcache.domain.Record; 9 | import com.safframework.rxcache4a.persistence.disk.MMKVImpl; 10 | import com.tencent.mmkv.MMKV; 11 | 12 | import org.junit.Before; 13 | import org.junit.Test; 14 | import org.junit.runner.RunWith; 15 | 16 | import static org.junit.Assert.assertEquals; 17 | import static org.junit.Assert.assertFalse; 18 | import static org.junit.Assert.assertTrue; 19 | 20 | /** 21 | * Instrumented test, which will execute on an Android device. 22 | * 23 | * @see Testing documentation 24 | */ 25 | @RunWith(AndroidJUnit4.class) 26 | public class MMKVCacheImplTest { 27 | 28 | Context appContext; 29 | 30 | @Before 31 | public void setUp() { 32 | appContext = InstrumentationRegistry.getTargetContext(); 33 | 34 | MMKV.initialize(appContext); 35 | } 36 | 37 | @Test 38 | public void testAll() { 39 | testObject(); 40 | testString(); 41 | testBoolean(); 42 | testInt(); 43 | testLong(); 44 | testFloat(); 45 | 46 | } 47 | 48 | @Test 49 | public void testObject() { 50 | MMKVImpl mmkv = new MMKVImpl(); 51 | mmkv.evictAll(); 52 | 53 | assertFalse(mmkv.containsKey("object")); 54 | 55 | User u = new User(); 56 | u.name = "tony"; 57 | u.password = "123456"; 58 | mmkv.save("object", u); 59 | 60 | assertTrue(mmkv.containsKey("object")); 61 | 62 | Record record = mmkv.retrieve("object", User.class); 63 | assertEquals(u.name, record.getData().name); 64 | //清除所有数据 65 | mmkv.evictAll(); 66 | assertFalse(mmkv.containsKey("object")); 67 | } 68 | 69 | @Test 70 | public void testString() { 71 | 72 | MMKVImpl mmkv = new MMKVImpl(); 73 | mmkv.evictAll(); 74 | 75 | //没有test数据 76 | assertFalse(mmkv.containsKey("string")); 77 | mmkv.save("string", "我是test的string!!!"); 78 | 79 | //有test数据 80 | assertTrue(mmkv.containsKey("string")); 81 | 82 | Record record= mmkv.retrieve("string", String.class); 83 | assertEquals("我是test的string!!!", record.getData()); 84 | 85 | //清除test数据 86 | mmkv.evict("string"); 87 | assertFalse(mmkv.containsKey("string")); 88 | 89 | } 90 | 91 | @Test 92 | public void testBoolean() { 93 | 94 | MMKVImpl mmkv = new MMKVImpl(); 95 | mmkv.evictAll(); 96 | 97 | //没有test数据 98 | assertFalse(mmkv.containsKey("bool")); 99 | mmkv.save("bool", true); 100 | 101 | //有test数据 102 | assertTrue(mmkv.containsKey("bool")); 103 | 104 | Record record = mmkv.retrieve("bool", Boolean.class); 105 | assertEquals(true, record.getData()); 106 | 107 | //清除test数据 108 | mmkv.evict("bool"); 109 | assertFalse(mmkv.containsKey("bool")); 110 | 111 | } 112 | 113 | @Test 114 | public void testInt() { 115 | 116 | MMKVImpl mmkv = new MMKVImpl(); 117 | mmkv.evictAll(); 118 | 119 | //没有test数据 120 | assertFalse(mmkv.containsKey("int")); 121 | mmkv.save("int", 0); 122 | 123 | //有test数据 124 | assertTrue(mmkv.containsKey("int")); 125 | 126 | Record record = mmkv.retrieve("int", Integer.class); 127 | assertEquals((Integer) 0, record.getData()); 128 | 129 | //清除test数据 130 | mmkv.evict("int"); 131 | assertFalse(mmkv.containsKey("int")); 132 | 133 | } 134 | 135 | @Test 136 | public void testLong() { 137 | 138 | MMKVImpl mmkv = new MMKVImpl(); 139 | mmkv.evictAll(); 140 | 141 | //没有test数据 142 | assertFalse(mmkv.containsKey("long")); 143 | mmkv.save("long", 0L); 144 | 145 | //有test数据 146 | assertTrue(mmkv.containsKey("long")); 147 | 148 | Record record = mmkv.retrieve("long", Long.class); 149 | assertEquals((Long) 0L, record.getData()); 150 | 151 | //清除test数据 152 | mmkv.evict("long"); 153 | assertFalse(mmkv.containsKey("long")); 154 | 155 | } 156 | 157 | @Test 158 | public void testFloat() { 159 | 160 | MMKVImpl mmkv = new MMKVImpl(); 161 | mmkv.evictAll(); 162 | 163 | //没有test数据 164 | assertFalse(mmkv.containsKey("float")); 165 | mmkv.save("float", 1.0F); 166 | 167 | //有test数据 168 | assertTrue(mmkv.containsKey("float")); 169 | 170 | Record record = mmkv.retrieve("float", Float.class); 171 | assertEquals((Float) 1.0F, record.getData()); 172 | 173 | //清除test数据 174 | mmkv.evict("float"); 175 | assertFalse(mmkv.containsKey("float")); 176 | 177 | } 178 | } 179 | -------------------------------------------------------------------------------- /rxcache4a_mmkv/src/androidTest/java/com/safframework/rxcache4a/User.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a; 2 | 3 | public class User { 4 | 5 | public String name; 6 | public String password; 7 | } 8 | -------------------------------------------------------------------------------- /rxcache4a_mmkv/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /rxcache4a_mmkv/src/main/java/com/safframework/rxcache4a/persistence/disk/MMKVImpl.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a.persistence.disk; 2 | 3 | import com.safframework.rxcache.config.Constant; 4 | import com.safframework.rxcache.domain.CacheHolder; 5 | import com.safframework.rxcache.domain.Record; 6 | import com.safframework.rxcache.domain.Source; 7 | import com.safframework.rxcache.persistence.converter.Converter; 8 | import com.safframework.rxcache.persistence.converter.GsonConverter; 9 | import com.safframework.rxcache.persistence.disk.Disk; 10 | import com.tencent.mmkv.MMKV; 11 | 12 | import java.lang.reflect.Type; 13 | import java.util.Arrays; 14 | import java.util.HashSet; 15 | import java.util.List; 16 | import java.util.Set; 17 | 18 | /** 19 | * @FileName: com.safframework.rxcache4a.persistence.disk.MMKVImpl 20 | * @author: Tony Shen 21 | * @date: 2018-10-06 10:37 22 | * @version: V1.0 <描述当前版本功能> 23 | */ 24 | public class MMKVImpl implements Disk { 25 | 26 | private MMKV kv = null; 27 | private Converter converter; 28 | 29 | public MMKVImpl() { 30 | 31 | this(MMKV.defaultMMKV(),new GsonConverter()); 32 | } 33 | 34 | public MMKVImpl(String mmkvID,Converter converter) { 35 | 36 | this.kv = MMKV.mmkvWithID(mmkvID); 37 | this.converter = converter; 38 | } 39 | 40 | public MMKVImpl(MMKV kv,Converter converter) { 41 | 42 | this.kv = kv; 43 | this.converter = converter; 44 | } 45 | 46 | @Override 47 | public int storedMB() { 48 | 49 | double megabytes = Math.ceil((double) kv.totalSize() / 1024 / 1024); 50 | return (int) megabytes; 51 | } 52 | 53 | @Override 54 | public Record retrieve(String key, Type type) { 55 | 56 | CacheHolder holder = converter.fromJson(kv.decodeString(key),CacheHolder.class); 57 | 58 | if (holder==null) return null; 59 | 60 | long timestamp = holder.getTimestamp(); 61 | long expireTime = holder.getExpireTime(); 62 | 63 | T result = null; 64 | 65 | if (expireTime<0) { // 缓存的数据从不过期 66 | 67 | String json = holder.getData(); 68 | 69 | result = converter.fromJson(json,type); 70 | } else { 71 | 72 | if (timestamp + expireTime > System.currentTimeMillis()) { // 缓存的数据还没有过期 73 | 74 | String json = holder.getData(); 75 | 76 | result = converter.fromJson(json,type); 77 | } else { // 缓存的数据已经过期 78 | 79 | evict(key); 80 | } 81 | } 82 | 83 | return result != null ? new Record<>(Source.PERSISTENCE, key, result, timestamp, expireTime) : null; 84 | } 85 | 86 | @Override 87 | public String getStringData(String key) { 88 | 89 | CacheHolder holder = converter.fromJson(kv.decodeString(key),CacheHolder.class); 90 | 91 | if (holder==null) return null; 92 | 93 | long timestamp = holder.getTimestamp(); 94 | long expireTime = holder.getExpireTime(); 95 | 96 | String json = null; 97 | 98 | if (expireTime<0) { // 缓存的数据从不过期 99 | 100 | json = holder.getData(); 101 | } else { 102 | 103 | if (timestamp + expireTime > System.currentTimeMillis()) { // 缓存的数据还没有过期 104 | 105 | json = holder.getData(); 106 | } else { // 缓存的数据已经过期 107 | 108 | evict(key); 109 | } 110 | } 111 | 112 | return json; 113 | } 114 | 115 | @Override 116 | public void save(String key, T value) { 117 | save(key, value, Constant.NEVER_EXPIRE); 118 | } 119 | 120 | @Override 121 | public void save(String key, T value, long expireTime) { 122 | 123 | CacheHolder holder = new CacheHolder(converter.toJson(value),System.currentTimeMillis(),expireTime,converter.converterName()); 124 | kv.encode(key, converter.toJson(holder)); 125 | } 126 | 127 | @Override 128 | public Set keySet() { 129 | 130 | String[] list = kv.allKeys(); 131 | 132 | Set result = new HashSet<>(); 133 | 134 | for (String item:list) { 135 | 136 | result.add(item); 137 | } 138 | 139 | return result; 140 | } 141 | 142 | @Override 143 | public boolean containsKey(String key) { 144 | 145 | return kv.containsKey(key); 146 | } 147 | 148 | @Override 149 | public void evict(String key) { 150 | 151 | kv.removeValueForKey(key); 152 | } 153 | 154 | @Override 155 | public void evictAll() { 156 | 157 | kv.clearAll(); 158 | } 159 | } 160 | -------------------------------------------------------------------------------- /rxcache4a_mmkv/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | rxcache4a-mmkv 3 | 4 | -------------------------------------------------------------------------------- /rxcache4a_mmkv/src/test/java/com/safframework/rxcache4a/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /rxcache4a_objectbox/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | /objectbox-models 3 | -------------------------------------------------------------------------------- /rxcache4a_objectbox/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'io.objectbox' 3 | 4 | def cfg = rootProject.ext.configuration // 配置 5 | def libs = rootProject.ext.libraries // 库 6 | 7 | android { 8 | compileSdkVersion cfg.compileVersion 9 | buildToolsVersion cfg.buildToolsVersion 10 | 11 | defaultConfig { 12 | minSdkVersion cfg.minSdk 13 | targetSdkVersion cfg.targetSdk 14 | versionCode 1 15 | versionName "1.0" 16 | 17 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 18 | } 19 | 20 | buildTypes { 21 | release { 22 | minifyEnabled false 23 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 24 | } 25 | } 26 | 27 | lintOptions { 28 | abortOnError false 29 | } 30 | 31 | compileOptions { 32 | sourceCompatibility 1.8 33 | targetCompatibility 1.8 34 | } 35 | 36 | } 37 | 38 | dependencies { 39 | implementation fileTree(dir: 'libs', include: ['*.jar']) 40 | 41 | implementation "androidx.appcompat:appcompat:${libs.androidx_appcompat}" 42 | testImplementation 'junit:junit:4.12' 43 | androidTestImplementation "androidx.test.ext:junit:${libs.androidx_junit}" 44 | androidTestImplementation "androidx.test.espresso:espresso-core:${libs.androidx_espresso}" 45 | 46 | implementation "com.github.fengzhizi715.RxCache:core:${libs.rxcache_core}" 47 | implementation "com.github.fengzhizi715.bytekit:core:${libs.bytekit}" 48 | 49 | testImplementation "io.objectbox:objectbox-linux:$objectboxVersion" 50 | testImplementation "io.objectbox:objectbox-macos:$objectboxVersion" 51 | testImplementation "io.objectbox:objectbox-windows:$objectboxVersion" 52 | } 53 | 54 | repositories { 55 | mavenCentral() 56 | maven { url "https://jitpack.io" } 57 | } 58 | 59 | allprojects { 60 | repositories { 61 | mavenCentral() 62 | maven { url "https://jitpack.io" } 63 | } 64 | //加上这些 65 | tasks.withType(Javadoc) { 66 | options{ encoding "UTF-8" 67 | charSet 'UTF-8' 68 | links "http://docs.oracle.com/javase/7/docs/api" 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /rxcache4a_objectbox/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /rxcache4a_objectbox/src/androidTest/java/com/safframework/rxcache4a/persistence/db/objectbox/Address.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a.persistence.db.objectbox; 2 | 3 | /** 4 | * @FileName: com.safframework.rxcache4a.persistence.db.Address 5 | * @author: Tony Shen 6 | * @date: 2018-10-15 21:28 7 | * @version: V1.0 <描述当前版本功能> 8 | */ 9 | public class Address { 10 | 11 | public String city; 12 | public String province; 13 | public String area; 14 | public String street; 15 | } 16 | -------------------------------------------------------------------------------- /rxcache4a_objectbox/src/androidTest/java/com/safframework/rxcache4a/persistence/db/objectbox/ObjectBoxImplTest.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a.persistence.db.objectbox; 2 | 3 | import android.content.Context; 4 | 5 | import androidx.test.InstrumentationRegistry; 6 | import androidx.test.ext.junit.runners.AndroidJUnit4; 7 | 8 | import com.safframework.rxcache.RxCache; 9 | import com.safframework.rxcache.domain.Record; 10 | 11 | import org.junit.Before; 12 | import org.junit.Test; 13 | import org.junit.runner.RunWith; 14 | 15 | import io.objectbox.BoxStore; 16 | 17 | import static org.junit.Assert.assertEquals; 18 | 19 | /** 20 | * @FileName: com.safframework.rxcache4a.ObjectBoxImplTest 21 | * @author: Tony Shen 22 | * @date: 2018-10-15 18:51 23 | * @version: V1.0 <描述当前版本功能> 24 | */ 25 | @RunWith(AndroidJUnit4.class) 26 | public class ObjectBoxImplTest { 27 | 28 | Context appContext; 29 | BoxStore boxStore; 30 | 31 | @Before 32 | public void setUp() { 33 | appContext = InstrumentationRegistry.getTargetContext(); 34 | boxStore = MyObjectBox.builder().androidContext(appContext).build(); 35 | } 36 | 37 | @Test 38 | public void testWithObject() { 39 | 40 | ObjectBoxImpl impl = new ObjectBoxImpl(boxStore); 41 | impl.evictAll(); 42 | 43 | RxCache.config(new RxCache.Builder().persistence(impl)); 44 | 45 | RxCache rxCache = RxCache.getRxCache(); 46 | 47 | Address address = new Address(); 48 | address.province = "Jiangsu"; 49 | address.city = "Suzhou"; 50 | address.area = "Gusu"; 51 | address.street = "ren ming road"; 52 | 53 | User u = new User(); 54 | u.name = "tony"; 55 | u.password = "123456"; 56 | u.address = address; 57 | 58 | rxCache.save("user",u); 59 | 60 | Record record = rxCache.get("user", User.class); 61 | 62 | assertEquals(u.name, record.getData().name); 63 | assertEquals(u.password, record.getData().password); 64 | assertEquals(address.city, record.getData().address.city); 65 | 66 | rxCache.save("address",address); 67 | 68 | Record
record2 = rxCache.get("address", Address.class); 69 | assertEquals(address.city, record2.getData().city); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /rxcache4a_objectbox/src/androidTest/java/com/safframework/rxcache4a/persistence/db/objectbox/User.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a.persistence.db.objectbox; 2 | 3 | public class User { 4 | 5 | public String name; 6 | public String password; 7 | public Address address; 8 | } 9 | -------------------------------------------------------------------------------- /rxcache4a_objectbox/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /rxcache4a_objectbox/src/main/java/com/safframework/rxcache4a/persistence/db/objectbox/CacheEntity.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a.persistence.db.objectbox; 2 | 3 | import io.objectbox.annotation.Entity; 4 | import io.objectbox.annotation.Id; 5 | 6 | /** 7 | * @FileName: com.safframework.rxcache4a.persistence.db.objectbox.CacheEntity 8 | * @author: Tony Shen 9 | * @date: 2018-10-19 08:54 10 | * @version: V1.0 <描述当前版本功能> 11 | */ 12 | @Entity 13 | public class CacheEntity { 14 | 15 | @Id 16 | public long id; 17 | 18 | public String key; 19 | 20 | public String data;// 对象转换的 json 字符串 21 | 22 | public Long timestamp; 23 | 24 | public Long expireTime; 25 | } 26 | -------------------------------------------------------------------------------- /rxcache4a_objectbox/src/main/java/com/safframework/rxcache4a/persistence/db/objectbox/ObjectBoxImpl.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a.persistence.db.objectbox; 2 | 3 | import com.safframework.bytekit.utils.Preconditions; 4 | import com.safframework.rxcache.config.Constant; 5 | import com.safframework.rxcache.domain.Record; 6 | import com.safframework.rxcache.domain.Source; 7 | import com.safframework.rxcache.persistence.converter.Converter; 8 | import com.safframework.rxcache.persistence.converter.GsonConverter; 9 | import com.safframework.rxcache.persistence.db.DB; 10 | 11 | import java.lang.reflect.Type; 12 | import java.util.ArrayList; 13 | import java.util.HashSet; 14 | import java.util.List; 15 | import java.util.Set; 16 | 17 | import io.objectbox.Box; 18 | import io.objectbox.BoxStore; 19 | 20 | /** 21 | * @FileName: com.safframework.rxcache4a.persistence.db.objectbox.ObjectBoxImpl 22 | * @author: Tony Shen 23 | * @date: 2018-10-19 09:03 24 | * @version: V1.0 <描述当前版本功能> 25 | */ 26 | public class ObjectBoxImpl implements DB { 27 | 28 | private BoxStore boxStore; 29 | private Converter converter; 30 | private Box cacheEntityBox; 31 | 32 | public ObjectBoxImpl(BoxStore boxStore) { 33 | 34 | this(boxStore,new GsonConverter()); 35 | } 36 | 37 | public ObjectBoxImpl(BoxStore boxStore, Converter converter) { 38 | 39 | this.boxStore = boxStore; 40 | this.converter = converter; 41 | this.cacheEntityBox= boxStore.boxFor(CacheEntity.class); 42 | } 43 | 44 | @Override 45 | public Record retrieve(String key, Type type) { 46 | 47 | // CacheEntity entity = cacheEntityBox.query().equal(CacheEntity_.key, key).build().findUnique(); 48 | CacheEntity entity = cacheEntityBox.query(CacheEntity_.key.equal(key)).build().findUnique(); 49 | 50 | if (entity==null) return null; 51 | 52 | long timestamp = entity.timestamp; 53 | long expireTime = entity.expireTime; 54 | T result = null; 55 | 56 | if (expireTime<0) { // 缓存的数据从不过期 57 | 58 | String json = entity.data; 59 | 60 | result = converter.fromJson(json,type); 61 | } else { 62 | 63 | if (timestamp + expireTime > System.currentTimeMillis()) { // 缓存的数据还没有过期 64 | 65 | String json = entity.data; 66 | 67 | result = converter.fromJson(json,type); 68 | } else { // 缓存的数据已经过期 69 | 70 | evict(key); 71 | } 72 | } 73 | 74 | return result != null ? new Record<>(Source.PERSISTENCE, key, result, timestamp, expireTime) : null; 75 | } 76 | 77 | @Override 78 | public String getStringData(String key) { 79 | 80 | // CacheEntity entity = cacheEntityBox.query().equal(CacheEntity_.key, key).build().findUnique(); 81 | CacheEntity entity = cacheEntityBox.query(CacheEntity_.key.equal(key)).build().findUnique(); 82 | 83 | if (entity==null) return null; 84 | 85 | long timestamp = entity.timestamp; 86 | long expireTime = entity.expireTime; 87 | 88 | String json = null; 89 | 90 | if (expireTime<0) { // 缓存的数据从不过期 91 | 92 | json = entity.data; 93 | } else { 94 | 95 | if (timestamp + expireTime > System.currentTimeMillis()) { // 缓存的数据还没有过期 96 | 97 | json = entity.data; 98 | } else { // 缓存的数据已经过期 99 | 100 | evict(key); 101 | } 102 | } 103 | 104 | return json; 105 | } 106 | 107 | @Override 108 | public void save(String key, T value) { 109 | 110 | save(key,value, Constant.NEVER_EXPIRE); 111 | } 112 | 113 | @Override 114 | public void save(String key, T value, long expireTime) { 115 | 116 | CacheEntity entity = new CacheEntity(); 117 | entity.key = key; 118 | entity.timestamp = System.currentTimeMillis(); 119 | entity.expireTime = expireTime; 120 | entity.data = converter.toJson(value); 121 | cacheEntityBox.put(entity); 122 | } 123 | 124 | @Override 125 | public Set keySet() { 126 | 127 | List list = cacheEntityBox.getAll(); 128 | 129 | Set result = new HashSet<>(); 130 | 131 | for (CacheEntity entity:list) { 132 | 133 | result.add(entity.key); 134 | } 135 | 136 | return result; 137 | } 138 | 139 | @Override 140 | public boolean containsKey(String key) { 141 | 142 | Set keys = keySet(); 143 | 144 | return Preconditions.isNotBlank(keys) ? keys.contains(key) : false; 145 | } 146 | 147 | @Override 148 | public void evict(String key) { 149 | 150 | // CacheEntity entity = cacheEntityBox.query().equal(CacheEntity_.key, key).build().findUnique(); 151 | CacheEntity entity = cacheEntityBox.query(CacheEntity_.key.equal(key)).build().findUnique(); 152 | 153 | if (entity!=null) { 154 | 155 | cacheEntityBox.remove(entity); 156 | } 157 | } 158 | 159 | @Override 160 | public void evictAll() { 161 | 162 | cacheEntityBox.removeAll(); 163 | } 164 | } 165 | -------------------------------------------------------------------------------- /rxcache4a_objectbox/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | rxcache4a_objectbox 3 | 4 | -------------------------------------------------------------------------------- /rxcache4a_objectbox/src/test/java/com/safframework/rxcache4a/persistence/db/objectbox/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a.persistence.db.objectbox; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /rxcache4a_room/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /rxcache4a_room/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | def cfg = rootProject.ext.configuration // 配置 4 | def libs = rootProject.ext.libraries // 库 5 | 6 | android { 7 | compileSdkVersion cfg.compileVersion 8 | buildToolsVersion cfg.buildToolsVersion 9 | 10 | defaultConfig { 11 | minSdkVersion cfg.minSdk 12 | targetSdkVersion cfg.targetSdk 13 | versionCode 1 14 | versionName "1.0" 15 | 16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 17 | } 18 | 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | 26 | lintOptions { 27 | abortOnError false 28 | } 29 | 30 | compileOptions { 31 | sourceCompatibility 1.8 32 | targetCompatibility 1.8 33 | } 34 | } 35 | 36 | dependencies { 37 | implementation fileTree(dir: 'libs', include: ['*.jar']) 38 | 39 | implementation "androidx.appcompat:appcompat:${libs.androidx_appcompat}" 40 | testImplementation 'junit:junit:4.12' 41 | androidTestImplementation "androidx.test.ext:junit:${libs.androidx_junit}" 42 | androidTestImplementation "androidx.test.espresso:espresso-core:${libs.androidx_espresso}" 43 | 44 | def room_version = "2.3.0" 45 | 46 | implementation "com.github.fengzhizi715.RxCache:core:${libs.rxcache_core}" 47 | implementation "com.github.fengzhizi715.bytekit:core:${libs.bytekit}" 48 | 49 | implementation "androidx.room:room-runtime:$room_version" 50 | annotationProcessor "androidx.room:room-compiler:$room_version" 51 | } 52 | 53 | repositories { 54 | mavenCentral() 55 | maven { url "https://jitpack.io" } 56 | } 57 | 58 | allprojects { 59 | repositories { 60 | mavenCentral() 61 | maven { url "https://jitpack.io" } 62 | } 63 | //加上这些 64 | tasks.withType(Javadoc) { 65 | options{ encoding "UTF-8" 66 | charSet 'UTF-8' 67 | links "http://docs.oracle.com/javase/7/docs/api" 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /rxcache4a_room/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /rxcache4a_room/src/androidTest/java/com/safframework/rxcache4a/persistence/db/Address.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a.persistence.db; 2 | 3 | /** 4 | * @FileName: com.safframework.rxcache4a.persistence.db.Address 5 | * @author: Tony Shen 6 | * @date: 2018-10-15 21:28 7 | * @version: V1.0 <描述当前版本功能> 8 | */ 9 | public class Address { 10 | 11 | public String city; 12 | public String province; 13 | public String area; 14 | public String street; 15 | } 16 | -------------------------------------------------------------------------------- /rxcache4a_room/src/androidTest/java/com/safframework/rxcache4a/persistence/db/RoomImplTest.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a.persistence.db; 2 | 3 | import android.content.Context; 4 | 5 | import androidx.test.InstrumentationRegistry; 6 | import androidx.test.ext.junit.runners.AndroidJUnit4; 7 | 8 | import com.safframework.rxcache.RxCache; 9 | import com.safframework.rxcache.domain.Record; 10 | import com.safframework.rxcache4a.persistence.db.room.RoomImpl; 11 | 12 | import org.junit.Before; 13 | import org.junit.Test; 14 | import org.junit.runner.RunWith; 15 | 16 | import static org.junit.Assert.assertEquals; 17 | import static org.junit.Assert.assertNull; 18 | 19 | /** 20 | * @FileName: com.safframework.rxcache4a.persistence.db.RoomImplTest 21 | * @author: Tony Shen 22 | * @date: 2018-10-15 19:31 23 | * @version: V1.0 <描述当前版本功能> 24 | */ 25 | @RunWith(AndroidJUnit4.class) 26 | public class RoomImplTest { 27 | 28 | Context appContext; 29 | 30 | 31 | @Before 32 | public void setUp() { 33 | appContext = InstrumentationRegistry.getTargetContext(); 34 | } 35 | 36 | @Test 37 | public void testWithObject() { 38 | 39 | RoomImpl impl = new RoomImpl(appContext); 40 | impl.evictAll(); 41 | 42 | RxCache.config(new RxCache.Builder().persistence(impl)); 43 | 44 | RxCache rxCache = RxCache.getRxCache(); 45 | 46 | Address address = new Address(); 47 | address.province = "Jiangsu"; 48 | address.city = "Suzhou"; 49 | address.area = "Gusu"; 50 | address.street = "ren ming road"; 51 | 52 | User u = new User(); 53 | u.name = "tony"; 54 | u.password = "123456"; 55 | u.address = address; 56 | 57 | rxCache.save("user",u); 58 | 59 | Record record = rxCache.get("user", User.class); 60 | 61 | assertEquals(u.name, record.getData().name); 62 | assertEquals(u.password, record.getData().password); 63 | assertEquals(address.city, record.getData().address.city); 64 | 65 | rxCache.save("address",address); 66 | 67 | Record
record2 = rxCache.get("address", Address.class); 68 | assertEquals(address.city, record2.getData().city); 69 | } 70 | 71 | @Test 72 | public void testWithExpireTime() { 73 | 74 | RoomImpl impl = new RoomImpl(appContext); 75 | impl.evictAll(); 76 | 77 | RxCache.config(new RxCache.Builder().persistence(impl)); 78 | 79 | RxCache rxCache = RxCache.getRxCache(); 80 | 81 | User u = new User(); 82 | u.name = "tony"; 83 | u.password = "123456"; 84 | rxCache.save("test",u,2000); 85 | 86 | try { 87 | Thread.sleep(2500); 88 | } catch (InterruptedException e) { 89 | e.printStackTrace(); 90 | } 91 | 92 | Record record = rxCache.get("test", User.class); 93 | 94 | assertNull(record); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /rxcache4a_room/src/androidTest/java/com/safframework/rxcache4a/persistence/db/User.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a.persistence.db; 2 | 3 | public class User { 4 | 5 | public String name; 6 | public String password; 7 | public Address address; 8 | } 9 | -------------------------------------------------------------------------------- /rxcache4a_room/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /rxcache4a_room/src/main/java/com/safframework/rxcache4a/persistence/db/room/AppDatabase.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a.persistence.db.room; 2 | 3 | import androidx.room.Database; 4 | import androidx.room.RoomDatabase; 5 | 6 | /** 7 | * @FileName: com.safframework.rxcache4a.persistence.db.room.AppDatabase 8 | * @author: Tony Shen 9 | * @date: 2018-10-15 16:40 10 | * @version: V1.0 <描述当前版本功能> 11 | */ 12 | @Database(entities = {CacheEntity.class}, version = 1) 13 | public abstract class AppDatabase extends RoomDatabase { 14 | 15 | public abstract CacheEntityDao cacheEntityDao(); 16 | } 17 | -------------------------------------------------------------------------------- /rxcache4a_room/src/main/java/com/safframework/rxcache4a/persistence/db/room/CacheEntity.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a.persistence.db.room; 2 | 3 | import androidx.room.Entity; 4 | import androidx.room.PrimaryKey; 5 | 6 | /** 7 | * @FileName: com.safframework.rxcache4a.persistence.db.room.CacheEntity 8 | * @author: Tony Shen 9 | * @date: 2018-10-15 16:26 10 | * @version: V1.0 <描述当前版本功能> 11 | */ 12 | @Entity 13 | public class CacheEntity { 14 | 15 | @PrimaryKey(autoGenerate = true) 16 | private Long id; 17 | 18 | public String key; 19 | 20 | public String data;// 对象转换的 json 字符串 21 | 22 | public Long timestamp; 23 | 24 | public Long expireTime; 25 | 26 | public Long getId() { 27 | return id; 28 | } 29 | 30 | public void setId(Long id) { 31 | this.id = id; 32 | } 33 | 34 | public String getKey() { 35 | return key; 36 | } 37 | 38 | public void setKey(String key) { 39 | this.key = key; 40 | } 41 | 42 | public String getData() { 43 | return data; 44 | } 45 | 46 | public void setData(String data) { 47 | this.data = data; 48 | } 49 | 50 | public Long getTimestamp() { 51 | return timestamp; 52 | } 53 | 54 | public void setTimestamp(Long timestamp) { 55 | this.timestamp = timestamp; 56 | } 57 | 58 | public Long getExpireTime() { 59 | return expireTime; 60 | } 61 | 62 | public void setExpireTime(Long expireTime) { 63 | this.expireTime = expireTime; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /rxcache4a_room/src/main/java/com/safframework/rxcache4a/persistence/db/room/CacheEntityDao.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a.persistence.db.room; 2 | 3 | import java.util.List; 4 | 5 | import androidx.room.Dao; 6 | import androidx.room.Delete; 7 | import androidx.room.Insert; 8 | import androidx.room.Query; 9 | 10 | import static androidx.room.OnConflictStrategy.IGNORE; 11 | 12 | /** 13 | * @FileName: com.safframework.rxcache4a.persistence.db.room.CacheEntityDao 14 | * @author: Tony Shen 15 | * @date: 2018-10-15 16:44 16 | * @version: V1.0 <描述当前版本功能> 17 | */ 18 | @Dao 19 | public interface CacheEntityDao { 20 | 21 | @Query("SELECT * FROM cacheentity") 22 | List getAll(); 23 | 24 | @Query("SELECT * FROM cacheentity WHERE `key` = :key LIMIT 0,1") 25 | CacheEntity findByKey(String key); 26 | 27 | @Insert(onConflict = IGNORE) 28 | void insert(CacheEntity entity); 29 | 30 | @Delete 31 | void delete(CacheEntity entity); 32 | 33 | @Query("DELETE FROM cacheentity") 34 | void deleteAll(); 35 | } 36 | -------------------------------------------------------------------------------- /rxcache4a_room/src/main/java/com/safframework/rxcache4a/persistence/db/room/RoomImpl.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a.persistence.db.room; 2 | 3 | import android.content.Context; 4 | 5 | import com.safframework.bytekit.utils.Preconditions; 6 | import com.safframework.rxcache.config.Constant; 7 | import com.safframework.rxcache.domain.Record; 8 | import com.safframework.rxcache.domain.Source; 9 | import com.safframework.rxcache.persistence.converter.Converter; 10 | import com.safframework.rxcache.persistence.converter.GsonConverter; 11 | import com.safframework.rxcache.persistence.db.DB; 12 | 13 | import java.lang.reflect.Type; 14 | import java.util.ArrayList; 15 | import java.util.HashSet; 16 | import java.util.List; 17 | import java.util.Set; 18 | 19 | import androidx.room.Room; 20 | 21 | /** 22 | * @FileName: com.safframework.rxcache4a.persistence.db.room.RoomImpl 23 | * @author: Tony Shen 24 | * @date: 2018-10-15 16:46 25 | * @version: V1.0 <描述当前版本功能> 26 | */ 27 | public class RoomImpl implements DB { 28 | 29 | private AppDatabase db; 30 | private Converter converter; 31 | private static final String DB_NAME = "cache"; 32 | 33 | public RoomImpl(Context context) { 34 | 35 | this(context,new GsonConverter()); 36 | } 37 | 38 | public RoomImpl(Context context, Converter converter) { 39 | 40 | this.db = Room.databaseBuilder(context, AppDatabase.class, DB_NAME).build(); 41 | this.converter = converter; 42 | } 43 | 44 | @Override 45 | public Record retrieve(String key, Type type) { 46 | 47 | CacheEntity entity = db.cacheEntityDao().findByKey(key); 48 | 49 | if (entity==null) return null; 50 | 51 | long timestamp = entity.timestamp; 52 | long expireTime = entity.expireTime; 53 | T result = null; 54 | 55 | if (expireTime<0) { // 缓存的数据从不过期 56 | 57 | String json = entity.data; 58 | 59 | result = converter.fromJson(json,type); 60 | } else { 61 | 62 | if (timestamp + expireTime > System.currentTimeMillis()) { // 缓存的数据还没有过期 63 | 64 | String json = entity.data; 65 | 66 | result = converter.fromJson(json,type); 67 | } else { // 缓存的数据已经过期 68 | 69 | evict(key); 70 | } 71 | } 72 | 73 | return result != null ? new Record<>(Source.PERSISTENCE, key, result, timestamp, expireTime) : null; 74 | } 75 | 76 | @Override 77 | public String getStringData(String key) { 78 | 79 | CacheEntity entity = db.cacheEntityDao().findByKey(key); 80 | 81 | if (entity==null) return null; 82 | 83 | long timestamp = entity.timestamp; 84 | long expireTime = entity.expireTime; 85 | 86 | String json = null; 87 | 88 | if (expireTime<0) { // 缓存的数据从不过期 89 | 90 | json = entity.data; 91 | } else { 92 | 93 | if (timestamp + expireTime > System.currentTimeMillis()) { // 缓存的数据还没有过期 94 | 95 | json = entity.data; 96 | } else { // 缓存的数据已经过期 97 | 98 | evict(key); 99 | } 100 | } 101 | 102 | return json; 103 | } 104 | 105 | @Override 106 | public void save(String key, T value) { 107 | 108 | save(key,value, Constant.NEVER_EXPIRE); 109 | } 110 | 111 | @Override 112 | public void save(String key, T value, long expireTime) { 113 | 114 | CacheEntity entity = new CacheEntity(); 115 | entity.setKey(key); 116 | entity.setTimestamp(System.currentTimeMillis()); 117 | entity.setExpireTime(expireTime); 118 | entity.setData(converter.toJson(value)); 119 | db.cacheEntityDao().insert(entity); 120 | } 121 | 122 | @Override 123 | public Set keySet() { 124 | 125 | List list = db.cacheEntityDao().getAll(); 126 | 127 | Set result = new HashSet<>(); 128 | 129 | for (CacheEntity entity:list) { 130 | 131 | result.add(entity.key); 132 | } 133 | 134 | return result; 135 | } 136 | 137 | @Override 138 | public boolean containsKey(String key) { 139 | 140 | Set keys = keySet(); 141 | 142 | return Preconditions.isNotBlank(keys) ? keys.contains(key) : false; 143 | } 144 | 145 | @Override 146 | public void evict(String key) { 147 | 148 | CacheEntity entity = db.cacheEntityDao().findByKey(key); 149 | 150 | if (entity!=null) { 151 | 152 | db.cacheEntityDao().delete(entity); 153 | } 154 | } 155 | 156 | @Override 157 | public void evictAll() { 158 | 159 | db.cacheEntityDao().deleteAll(); 160 | } 161 | } 162 | -------------------------------------------------------------------------------- /rxcache4a_room/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | rxcache4a_room 3 | 4 | -------------------------------------------------------------------------------- /rxcache4a_room/src/test/java/com/safframework/rxcache4a/persistence/db/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcache4a.persistence.db; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | include ':rxcache4a' 3 | include ':rxcache4a_mmkv' 4 | include ':rxcache4a_greenDAO' 5 | include ':rxcache4a_room' 6 | include ':rxcache4a_objectbox' 7 | include ':rxcache4a_livedata' --------------------------------------------------------------------------------