Skip to content

Commit 0fcf76f

Browse files
committed
see 05/27 log
1 parent b5e778d commit 0fcf76f

File tree

11 files changed

+301
-54
lines changed

11 files changed

+301
-54
lines changed

README-CN.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ getBitmap : 缓存中读取bitmap
6464
getDrawable : 缓存中读取drawable
6565
getParcelable : 缓存中读取Parcelable
6666
getSerializable: 缓存中读取Serializable
67-
getCacheFile : 获取缓存文件
67+
getCacheSize : 获取缓存大小
68+
getCacheCount : 获取缓存个数
6869
remove : 移除某个key
6970
clear : 清除所有缓存
7071
```
@@ -647,7 +648,7 @@ getEntries : 获取压缩文件中的文件对象
647648

648649
Gradle:
649650
``` groovy
650-
compile 'com.blankj:utilcode:1.6.1'
651+
compile 'com.blankj:utilcode:1.6.2'
651652
```
652653

653654

@@ -669,7 +670,7 @@ Utils.init(context);
669670

670671

671672

672-
[aucsvg]: https://img.shields.io/badge/AndroidUtilCode-v1.6.1-brightgreen.svg
673+
[aucsvg]: https://img.shields.io/badge/AndroidUtilCode-v1.6.2-brightgreen.svg
673674
[auc]: https://github.com/Blankj/AndroidUtilCode
674675

675676
[apisvg]: https://img.shields.io/badge/API-15+-brightgreen.svg

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ getBitmap
6464
getDrawable
6565
getParcelable
6666
getSerializable
67-
getCacheFile
67+
getCacheSize
68+
getCacheCount
6869
remove
6970
clear
7071
```
@@ -647,7 +648,7 @@ getEntries
647648

648649
Gradle:
649650
``` groovy
650-
compile 'com.blankj:utilcode:1.6.1'
651+
compile 'com.blankj:utilcode:1.6.2'
651652
```
652653

653654

@@ -669,7 +670,7 @@ Utils.init(context);
669670

670671

671672

672-
[aucsvg]: https://img.shields.io/badge/AndroidUtilCode-v1.6.1-brightgreen.svg
673+
[aucsvg]: https://img.shields.io/badge/AndroidUtilCode-v1.6.2-brightgreen.svg
673674
[auc]: https://github.com/Blankj/AndroidUtilCode
674675

675676
[apisvg]: https://img.shields.io/badge/API-15+-brightgreen.svg

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ android {
1010
applicationId "com.blankj.androidutilcode"
1111
minSdkVersion 15
1212
targetSdkVersion 16
13-
versionCode 33
14-
versionName "1.6.1"
13+
versionCode 34
14+
versionName "1.6.2"
1515
}
1616

1717
if (signPropertiesFile.exists()) {

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<activity android:name=".activity.SnackbarActivity"/>
7878
<activity android:name=".activity.SpannableActivity"/>
7979
<activity android:name=".activity.ToastActivity"/>
80+
<activity android:name=".activity.CacheActivity"/>
8081

8182
<service android:name=".service.LocationService"/>
8283

update_log.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
* 17/05/26 完善CacheUtil,发布1.6.0和1.6.1
1+
* 17/05/27 完善CacheUtils,发布1.6.2
2+
* 17/05/26 完善CacheUtils,发布1.6.0和1.6.1
23
* 17/05/25 完善FileIOUtils和CacheUtils
34
* 17/05/23 新增读取文件到字符数组中两种方式
45
* 17/05/19 LogUtils新增文件过滤和控制台开关

utilcode/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ android {
2626

2727
defaultConfig {
2828
minSdkVersion 15
29-
versionCode 33
30-
versionName "1.6.1"
29+
versionCode 34
30+
versionName "1.6.2"
3131
}
3232

3333
buildTypes {

utilcode/src/main/java/com/blankj/utilcode/util/CacheUtils.java

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
*/
4444
public class CacheUtils {
4545

46-
private static final int DEFAULT_MAX_SIZE = 104857600; // 100Mb
47-
private static final int DEFAULT_MAX_COUNT = Integer.MAX_VALUE;
46+
private static final long DEFAULT_MAX_SIZE = Long.MAX_VALUE;
47+
private static final int DEFAULT_MAX_COUNT = Integer.MAX_VALUE;
4848

4949
private static Map<String, CacheUtils> sCacheMap = new HashMap<>();
5050
private CacheManager mCacheManager;
@@ -164,8 +164,9 @@ public void put(@NonNull String key, @NonNull byte[] value) {
164164
public void put(@NonNull String key, @NonNull byte[] value, int saveTime) {
165165
if (value.length <= 0) return;
166166
if (saveTime >= 0) value = CacheHelper.newByteArrayWithTime(saveTime, value);
167-
File file = mCacheManager.getFileByKey(key);
167+
File file = mCacheManager.getFileBeforePut(key);
168168
CacheHelper.writeFileFromBytes(file, value);
169+
mCacheManager.updateModify(file);
169170
mCacheManager.put(file);
170171

171172
}
@@ -441,13 +442,22 @@ public Object getSerializable(@NonNull String key) {
441442
}
442443

443444
/**
444-
* 获取缓存文件
445+
* 获取缓存大小
446+
* <p>单位:字节</p>
445447
*
446-
* @param key 键
447-
* @return 缓存文件
448+
* @return 缓存大小
448449
*/
449-
public File getCacheFile(@NonNull String key) {
450-
return mCacheManager.getFileIfExists(key);
450+
public long getCacheSize() {
451+
return mCacheManager.getCacheSize();
452+
}
453+
454+
/**
455+
* 获取缓存个数
456+
*
457+
* @return 缓存个数
458+
*/
459+
public int getCacheCount() {
460+
return mCacheManager.getCacheCount();
451461
}
452462

453463
/**
@@ -467,7 +477,6 @@ public void clear() {
467477
mCacheManager.clear();
468478
}
469479

470-
471480
private class CacheManager {
472481
private final AtomicLong cacheSize;
473482
private final AtomicInteger cacheCount;
@@ -508,8 +517,21 @@ public void run() {
508517
}).start();
509518
}
510519

511-
private File getFileByKey(String key) {
512-
return new File(cacheDir, String.valueOf(key.hashCode()));
520+
private long getCacheSize() {
521+
return cacheSize.get();
522+
}
523+
524+
private int getCacheCount() {
525+
return cacheCount.get();
526+
}
527+
528+
private File getFileBeforePut(String key) {
529+
File file = new File(cacheDir, String.valueOf(key.hashCode()));
530+
if (file.exists()) {
531+
cacheCount.addAndGet(-1);
532+
cacheSize.addAndGet(-file.length());
533+
}
534+
return file;
513535
}
514536

515537
private File getFileIfExists(String key) {
@@ -525,7 +547,6 @@ private void put(File file) {
525547
cacheSize.addAndGet(-removeOldest());
526548
cacheCount.addAndGet(-1);
527549
}
528-
updateModify(file);
529550
}
530551

531552
private void updateModify(File file) {
@@ -547,15 +568,21 @@ private boolean removeByKey(String key) {
547568
private void clear() {
548569
File[] files = cacheDir.listFiles();
549570
if (files == null) return;
571+
boolean flag = true;
550572
for (File file : files) {
551-
if (!file.delete()) continue;
552-
cacheSize.addAndGet(file.length());
573+
if (!file.delete()) {
574+
flag = false;
575+
continue;
576+
}
577+
cacheSize.addAndGet(-file.length());
553578
cacheCount.addAndGet(-1);
554579
lastUsageDates.remove(file);
555580
}
556-
lastUsageDates.clear();
557-
cacheSize.set(0);
558-
cacheCount.set(0);
581+
if (flag) {
582+
lastUsageDates.clear();
583+
cacheSize.set(0);
584+
cacheCount.set(0);
585+
}
559586
}
560587

561588
/**
@@ -825,4 +852,4 @@ private static boolean isSpace(String s) {
825852
}
826853
return true;
827854
}
828-
}
855+
}

0 commit comments

Comments
 (0)