From 1920b882493d99c15256b79f9d3f112a93a9fbe5 Mon Sep 17 00:00:00 2001 From: xuexiangjys Date: Tue, 2 Apr 2019 00:23:38 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E7=A3=81=E7=9B=98=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E6=B8=85=E9=99=A4=E5=90=8E=EF=BC=8C=E7=BB=A7=E7=BB=AD?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=AF=BC=E8=87=B4=E5=B4=A9=E6=BA=83=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/xuexiang/xaopdemo/LoginActivity.java | 3 --- .../xaop/cache/core/LruDiskCache.java | 23 +++++++++++++++---- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/xuexiang/xaopdemo/LoginActivity.java b/app/src/main/java/com/xuexiang/xaopdemo/LoginActivity.java index 9f81425..b785477 100644 --- a/app/src/main/java/com/xuexiang/xaopdemo/LoginActivity.java +++ b/app/src/main/java/com/xuexiang/xaopdemo/LoginActivity.java @@ -32,9 +32,6 @@ public class LoginActivity extends AppCompatActivity { public static boolean sIsLogined = false; - - - @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/xaop-runtime/src/main/java/com/xuexiang/xaop/cache/core/LruDiskCache.java b/xaop-runtime/src/main/java/com/xuexiang/xaop/cache/core/LruDiskCache.java index 886d45e..152995e 100644 --- a/xaop-runtime/src/main/java/com/xuexiang/xaop/cache/core/LruDiskCache.java +++ b/xaop-runtime/src/main/java/com/xuexiang/xaop/cache/core/LruDiskCache.java @@ -48,6 +48,11 @@ public class LruDiskCache extends BaseDiskCache { */ private DiskLruCache mDiskLruCache; + private File mDiskDir; + private int mAppVersion; + private long mDiskMaxSize; + + /** * 初始化磁盘缓存 * @@ -58,8 +63,18 @@ public class LruDiskCache extends BaseDiskCache { */ public LruDiskCache(IDiskConverter diskConverter, File diskDir, int appVersion, long diskMaxSize) { mDiskConverter = Utils.checkNotNull(diskConverter, "mDiskConverter ==null"); + mDiskDir = Utils.checkNotNull(diskDir, "mDiskDir ==null"); + mAppVersion = appVersion; + mDiskMaxSize = diskMaxSize; + openCache(); + } + + /** + * 打开磁盘缓存 + */ + private void openCache() { try { - mDiskLruCache = DiskLruCache.open(diskDir, appVersion, 1, diskMaxSize); + mDiskLruCache = DiskLruCache.open(mDiskDir, mAppVersion, 1, mDiskMaxSize); } catch (IOException e) { e.printStackTrace(); XLogger.e(e); @@ -148,6 +163,7 @@ protected boolean doClear() { boolean status = false; try { mDiskLruCache.delete(); + openCache(); //清除缓存后需要重新打开缓存 status = true; } catch (IOException e) { e.printStackTrace(); @@ -163,9 +179,8 @@ protected boolean isExpiry(String key, long existTime) { if (existTime > CACHE_NEVER_EXPIRE) {//-1表示永久性存储 不用进行过期校验 //为什么这么写,请了解DiskLruCache,看它的源码 File file = new File(mDiskLruCache.getDirectory(), key + "." + 0); - if (isCacheDataFailure(file, existTime)) {//没有获取到缓存,或者缓存已经过期! - return true; - } + //没有获取到缓存,或者缓存已经过期! + return isCacheDataFailure(file, existTime); } return false; }