From c557706d23e5e9d2bfd323145266c2e27ecc343d Mon Sep 17 00:00:00 2001 From: tongtongstudio Date: Wed, 11 Nov 2026 20:21:18 +0800 Subject: [PATCH] =?UTF-8?q?refactor(cache):=20=E6=B3=9B=E5=90=8D=20Utils?= =?UTF-8?q?=20=E9=87=8D=E5=91=BD=E5=90=8D=E4=B8=BA=20DiskCacheUtils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 该类只服务磁盘缓存(版本号、缓存键哈希、Bitmap/Drawable 转换),原名 Utils 语义不清;仅 CacheHelper 使用,调用点同步更新。 验证::app:assembleOfficialZhanRuiRelease 编译通过。 --- .../sn/core/data/local/cache/CacheHelper.java | 18 +++++++++--------- .../cache/{Utils.java => DiskCacheUtils.java} | 5 ++++- 2 files changed, 13 insertions(+), 10 deletions(-) rename app/src/main/java/com/fuying/sn/core/data/local/cache/{Utils.java => DiskCacheUtils.java} (96%) diff --git a/app/src/main/java/com/fuying/sn/core/data/local/cache/CacheHelper.java b/app/src/main/java/com/fuying/sn/core/data/local/cache/CacheHelper.java index 8a8a0c8..d695c64 100644 --- a/app/src/main/java/com/fuying/sn/core/data/local/cache/CacheHelper.java +++ b/app/src/main/java/com/fuying/sn/core/data/local/cache/CacheHelper.java @@ -69,7 +69,7 @@ public class CacheHelper { dir + " is not a directory or does not exists. "); } - int appVersion = context == null ? DEFAULT_APP_VERSION : Utils.getAppVersion(context); + int appVersion = context == null ? DEFAULT_APP_VERSION : DiskCacheUtils.getAppVersion(context); try { return DiskLruCache.open( @@ -87,7 +87,7 @@ public class CacheHelper { try { return DiskLruCache.open( getDiskCacheDir(context, dirName), - Utils.getAppVersion(context), + DiskCacheUtils.getAppVersion(context), DEFAULT_APP_VERSION, maxCount); } catch (IOException e) { @@ -340,7 +340,7 @@ public class CacheHelper { // ============== bitmap 数据 读写 ============= // ======================================= public void put(String key, Bitmap bitmap) { - put(key, Utils.bitmap2Bytes(bitmap)); + put(key, DiskCacheUtils.bitmap2Bytes(bitmap)); } public Bitmap getAsBitmap(String key) { @@ -348,14 +348,14 @@ public class CacheHelper { if (bytes == null) { return null; } - return Utils.bytes2Bitmap(bytes); + return DiskCacheUtils.bytes2Bitmap(bytes); } // ======================================= // ============= drawable 数据 读写 ============= // ======================================= public void put(String key, Drawable value) { - put(key, Utils.drawable2Bitmap(value)); + put(key, DiskCacheUtils.drawable2Bitmap(value)); } public Drawable getAsDrawable(String key) { @@ -363,7 +363,7 @@ public class CacheHelper { if (bytes == null) { return null; } - return Utils.bitmap2Drawable(Utils.bytes2Bitmap(bytes)); + return DiskCacheUtils.bitmap2Drawable(DiskCacheUtils.bytes2Bitmap(bytes)); } // ======================================= @@ -371,7 +371,7 @@ public class CacheHelper { // ======================================= public boolean remove(String key) { try { - String hashKey = Utils.hashKeyForDisk(key); + String hashKey = DiskCacheUtils.hashKeyForDisk(key); return mDiskLruCache.remove(hashKey); } catch (IOException e) { e.printStackTrace(); @@ -418,7 +418,7 @@ public class CacheHelper { //basic editor public DiskLruCache.Editor editor(String key) { try { - String hashKey = Utils.hashKeyForDisk(key); + String hashKey = DiskCacheUtils.hashKeyForDisk(key); //wirte DIRTY DiskLruCache.Editor edit = mDiskLruCache.edit(hashKey); //edit maybe null :the entry is editing @@ -438,7 +438,7 @@ public class CacheHelper { //basic get public InputStream get(String key) { try { - DiskLruCache.Snapshot snapshot = mDiskLruCache.get(Utils.hashKeyForDisk(key)); + DiskLruCache.Snapshot snapshot = mDiskLruCache.get(DiskCacheUtils.hashKeyForDisk(key)); if (snapshot == null) //not find entry , or entry.readable = false { Log.e(TAG, "not find entry , or entry.readable = false"); diff --git a/app/src/main/java/com/fuying/sn/core/data/local/cache/Utils.java b/app/src/main/java/com/fuying/sn/core/data/local/cache/DiskCacheUtils.java similarity index 96% rename from app/src/main/java/com/fuying/sn/core/data/local/cache/Utils.java rename to app/src/main/java/com/fuying/sn/core/data/local/cache/DiskCacheUtils.java index d8ddd97..3dbbe1f 100644 --- a/app/src/main/java/com/fuying/sn/core/data/local/cache/Utils.java +++ b/app/src/main/java/com/fuying/sn/core/data/local/cache/DiskCacheUtils.java @@ -14,7 +14,10 @@ import java.io.ByteArrayOutputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; -public class Utils { +/** + * 磁盘缓存底层工具:版本号、缓存键哈希与 Bitmap/Drawable 互转。 + */ +public class DiskCacheUtils { public static int getAppVersion(Context context) { try { PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);