CacheDiskUtils
之前寫過一篇 你想要的 CacheUtils烙如,簡單介紹了下其可以完美替代 ASimpleCache面哼,而且修復了其中少許 BUG 并做了相應優(yōu)化比肄,相關 API 如下所示:
緩存相關 -> CacheUtils.java
getInstance : 獲取緩存實例
Instance.put : 緩存中寫入數據
Instance.getBytes : 緩存中讀取字節(jié)數組
Instance.getString : 緩存中讀取 String
Instance.getJSONObject : 緩存中讀取 JSONObject
Instance.getJSONArray : 緩存中讀取 JSONArray
Instance.getBitmap : 緩存中讀取 Bitmap
Instance.getDrawable : 緩存中讀取 Drawable
Instance.getParcelable : 緩存中讀取 Parcelable
Instance.getSerializable: 緩存中讀取 Serializable
Instance.getCacheSize : 獲取緩存大小
Instance.getCacheCount : 獲取緩存?zhèn)€數
Instance.remove : 根據鍵值移除緩存
Instance.clear : 清除所有緩存
其也就是所謂的硬盤緩存葡公,在 AndroidUtilCode 1.17.0 版本,該 CacheUtils 已被我標記廢棄汗侵,可替換為 CacheDiskUtils,下一個大版本1.18.x 可能就會移除 CacheUtils群发。
CacheMemoryUtils
講了磁盤緩存另一個就是內存緩存晰韵,內存緩存工具類 CacheMemoryUtils 原理是利用 LruCache 來實現的(LRU 是Least Recently Used的縮寫,即最近最少使用)熟妓,其 API 如下所示:
內存緩存相關 -> CacheMemoryUtils.java -> Test
getInstance : 獲取緩存實例
Instance.put : 緩存中寫入數據
Instance.get : 緩存中讀取字節(jié)數組
Instance.getCacheCount: 獲取緩存?zhèn)€數
Instance.remove : 根據鍵值移除緩存
Instance.clear : 清除所有緩存
CacheDoubleUtils
結合硬盤緩存工具類 CacheDiskUtils 和內存緩存工具類 CacheMemoryUtils雪猪,那么我們的二級緩存工具類 CacheDoubleUtils 便誕生了,其 API 如下所示:
二級緩存相關 -> CacheDoubleUtils.java -> Test
getInstance : 獲取緩存實例
Instance.put : 緩存中寫入數據
Instance.getBytes : 緩存中讀取字節(jié)數組
Instance.getString : 緩存中讀取 String
Instance.getJSONObject : 緩存中讀取 JSONObject
Instance.getJSONArray : 緩存中讀取 JSONArray
Instance.getBitmap : 緩存中讀取 Bitmap
Instance.getDrawable : 緩存中讀取 Drawable
Instance.getParcelable : 緩存中讀取 Parcelable
Instance.getSerializable : 緩存中讀取 Serializable
Instance.getCacheDiskSize : 獲取磁盤緩存大小
Instance.getCacheDiskCount : 獲取磁盤緩存?zhèn)€數
Instance.getCacheMemoryCount: 獲取內存緩存?zhèn)€數
Instance.remove : 根據鍵值移除緩存
Instance.clear : 清除所有緩存
借助以上三個緩存工具類起愈,那么 Android 端的緩存實現便再也不是什么難題了浪蹂,例如你想要實現 RxCache,那么借助 RxJava 的 compose 操作符和我的工具類告材,把數據放入緩存不就輕而易舉地實現了么坤次,更多風騷的姿勢可待你解鎖。