Skip to content

Commit

Permalink
解决磁盘缓存清除后,继续使用导致崩溃的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
xuexiangjys committed Apr 1, 2019
1 parent d52eff1 commit 1920b88
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
3 changes: 0 additions & 3 deletions app/src/main/java/com/xuexiang/xaopdemo/LoginActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ public class LoginActivity extends AppCompatActivity {

public static boolean sIsLogined = false;




@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public class LruDiskCache extends BaseDiskCache {
*/
private DiskLruCache mDiskLruCache;

private File mDiskDir;
private int mAppVersion;
private long mDiskMaxSize;


/**
* 初始化磁盘缓存
*
Expand All @@ -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);
Expand Down Expand Up @@ -148,6 +163,7 @@ protected boolean doClear() {
boolean status = false;
try {
mDiskLruCache.delete();
openCache(); //清除缓存后需要重新打开缓存
status = true;
} catch (IOException e) {
e.printStackTrace();
Expand All @@ -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;
}
Expand Down

0 comments on commit 1920b88

Please sign in to comment.