Skip to content

Commit f2eb436

Browse files
author
alafighting
committed
增加更多的DiskJournal实现;DiskJournal增加缓存使用次数的记录
1 parent 4e4f1cb commit f2eb436

File tree

7 files changed

+97
-42
lines changed

7 files changed

+97
-42
lines changed

library/src/main/java/com/im4j/kakacache/rxjava/core/Cache.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ public final <T> T load(String key) throws CacheException {
5353

5454
/**
5555
* 保存
56-
* @param expires 有效期(单位:秒)
56+
* @param maxAge 最大有效期时长(单位:秒)
5757
*/
58-
public final <T> void save(String key, T value, int expires, CacheTarget target) throws CacheException {
58+
public final <T> void save(String key, T value, int maxAge, CacheTarget target) throws CacheException {
5959
Utils.checkNotNull(key);
6060

6161
if (value == null) {
@@ -66,17 +66,17 @@ public final <T> void save(String key, T value, int expires, CacheTarget target)
6666
// TODO 先写入,后清理。会超出限定条件,需要一定交换空间
6767

6868
// 写入缓存
69-
doSave(key, value, expires, target);
69+
doSave(key, value, maxAge, target);
7070

7171
// 清理无用数据
7272
clearUnused();
7373
}
7474

7575
/**
7676
* 保存
77-
* @param expires 有效期(单位:毫秒)
77+
* @param maxAge 最长有效期时长(单位:毫秒)
7878
*/
79-
protected abstract <T> void doSave(String key, T value, int expires, CacheTarget target) throws CacheException;
79+
protected abstract <T> void doSave(String key, T value, int maxAge, CacheTarget target) throws CacheException;
8080

8181

8282
/**

library/src/main/java/com/im4j/kakacache/rxjava/core/CacheEntry.java

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,55 @@
1313
public class CacheEntry implements Serializable {
1414
public static final String COL_KEY = "key";
1515
public static final String COL_CREATE_TIME = "create_time";
16-
public static final String COL_USE_TIME = "use_time";
16+
public static final String COL_LAST_USE_TIME = "last_use_time";
17+
public static final String COL_USE_COUNT = "use_count";
1718
public static final String COL_EXPIRY_TIME = "expiry_time";
1819
public static final String COL_TARGET = "expiry_target";
1920

20-
/** KEY */
21+
/**
22+
* KEY
23+
*/
2124
@PrimaryKey(AssignType.BY_MYSELF)
2225
@Column(COL_KEY)
2326
private final String key;
24-
/** 创建时间 */
27+
/**
28+
* 创建时间
29+
*/
2530
@Column(COL_CREATE_TIME)
2631
private long createTime;
27-
/** 最后使用时间 */
28-
@Column(COL_USE_TIME)
29-
private long useTime;
30-
/** 过期时间 */
32+
/**
33+
* 最后使用时间
34+
*/
35+
@Column(COL_LAST_USE_TIME)
36+
private long lastUseTime;
37+
/**
38+
* 总使用次数
39+
*/
40+
@Column(COL_USE_COUNT)
41+
private long useCount;
42+
/**
43+
* 过期时间
44+
*/
3145
@Column(COL_EXPIRY_TIME)
3246
private long expiryTime;
33-
/** 缓存目标 */
47+
/**
48+
* 缓存目标
49+
*/
3450
@Column(COL_TARGET)
3551
private CacheTarget target;
3652
// TODO 有待商讨
3753
// private long size;
3854

39-
public CacheEntry(String key, long createTime, long useTime, long expiryTime, CacheTarget target) {
55+
56+
public CacheEntry(String key, long maxAge, CacheTarget target) {
57+
long currentTime = System.currentTimeMillis();
58+
4059
this.key = key;
41-
this.createTime = createTime;
42-
this.useTime = useTime;
43-
this.expiryTime = expiryTime;
60+
this.createTime = currentTime;
61+
this.lastUseTime = currentTime;
62+
this.expiryTime = currentTime + maxAge;
4463
this.target = target;
64+
this.useCount = 1;
4565
}
4666

4767

@@ -58,7 +78,8 @@ public String toString() {
5878
return "CacheEntry{" +
5979
"key='" + key + '\'' +
6080
", createTime=" + createTime +
61-
", useTime=" + useTime +
81+
", lastUseTime=" + lastUseTime +
82+
", useCount=" + useCount +
6283
", expiryTime=" + expiryTime +
6384
", target=" + target +
6485
'}';
@@ -77,20 +98,20 @@ public void setCreateTime(long createTime) {
7798
this.createTime = createTime;
7899
}
79100

80-
public long getUseTime() {
81-
return useTime;
101+
public long getLastUseTime() {
102+
return lastUseTime;
82103
}
83104

84-
public void setUseTime(long useTime) {
85-
this.useTime = useTime;
105+
public void setLastUseTime(long lastUseTime) {
106+
this.lastUseTime = lastUseTime;
86107
}
87108

88-
public CacheTarget getTarget() {
89-
return target;
109+
public long getUseCount() {
110+
return useCount;
90111
}
91112

92-
public void setTarget(CacheTarget target) {
93-
this.target = target;
113+
public void setUseCount(long useCount) {
114+
this.useCount = useCount;
94115
}
95116

96117
public long getExpiryTime() {
@@ -101,4 +122,11 @@ public void setExpiryTime(long expiryTime) {
101122
this.expiryTime = expiryTime;
102123
}
103124

125+
public CacheTarget getTarget() {
126+
return target;
127+
}
128+
129+
public void setTarget(CacheTarget target) {
130+
this.target = target;
131+
}
104132
}

library/src/main/java/com/im4j/kakacache/rxjava/core/disk/DiskCache.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ protected <T> T doLoad(String key) throws CacheException {
5757

5858
/**
5959
* 保存
60-
* @param expires 有效期(单位:毫秒)
60+
* @param maxAge 最大有效期时长(单位:毫秒)
6161
*/
6262
@Override
63-
protected <T> void doSave(String key, T value, int expires, CacheTarget target) throws CacheException {
63+
protected <T> void doSave(String key, T value, int maxAge, CacheTarget target) throws CacheException {
6464
if (target == null || target == CacheTarget.NONE || target == CacheTarget.Memory) {
6565
return;
6666
}
@@ -71,9 +71,7 @@ protected <T> void doSave(String key, T value, int expires, CacheTarget target)
7171
mConverter.writer(sink, value);
7272
Utils.close(sink);
7373

74-
long currentTime = System.currentTimeMillis();
75-
long expiresTime = currentTime + expires;
76-
mJournal.put(key, new CacheEntry(key, currentTime, currentTime, expiresTime, target));
74+
mJournal.put(key, new CacheEntry(key, maxAge, target));
7775
}
7876
}
7977

library/src/main/java/com/im4j/kakacache/rxjava/core/disk/journal/BasicDiskJournal.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ public CacheEntry get(String key) {
3636
if (entry != null) {
3737
// 有效期内,才记录最后使用时间
3838
if (entry.isExpiry()) {
39-
entry.setUseTime(System.currentTimeMillis());
39+
entry.setLastUseTime(System.currentTimeMillis());
40+
entry.setUseCount(entry.getUseCount() + 1);
4041
mLiteOrm.update(entry);
4142
}
4243
return entry;
@@ -51,7 +52,8 @@ public void put(String key, CacheEntry entry) {
5152
throw new NullException("key == null || value == null");
5253
}
5354
if (entry.isExpiry()) {
54-
entry.setUseTime(System.currentTimeMillis());
55+
entry.setLastUseTime(System.currentTimeMillis());
56+
entry.setUseCount(1);
5557
mLiteOrm.save(entry);
5658
} else {
5759
remove(key);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.im4j.kakacache.rxjava.core.disk.journal;
2+
3+
import com.im4j.kakacache.rxjava.common.exception.CacheException;
4+
import com.im4j.kakacache.rxjava.core.CacheEntry;
5+
import com.litesuits.orm.LiteOrm;
6+
import com.litesuits.orm.db.assit.QueryBuilder;
7+
8+
import java.util.List;
9+
10+
/**
11+
* LFU缓存日志
12+
* @version alafighting 2016-07
13+
*/
14+
public class LFUDiskJournal extends BasicDiskJournal {
15+
16+
public LFUDiskJournal(LiteOrm liteOrm) {
17+
super(liteOrm);
18+
}
19+
20+
@Override
21+
public String getLoseKey() throws CacheException {
22+
QueryBuilder query = new QueryBuilder(CacheEntry.class);
23+
query.orderBy(CacheEntry.COL_USE_COUNT).appendOrderAscBy(CacheEntry.COL_LAST_USE_TIME);
24+
query.limit(0, 1);
25+
List<CacheEntry> list = getDb().query(query);
26+
if (list != null && list.size() >0) {
27+
return list.get(0).getKey();
28+
} else {
29+
return null;
30+
}
31+
}
32+
33+
}

library/src/main/java/com/im4j/kakacache/rxjava/core/disk/journal/LRUDiskJournal.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
package com.im4j.kakacache.rxjava.core.disk.journal;
22

33
import com.im4j.kakacache.rxjava.common.exception.CacheException;
4-
import com.im4j.kakacache.rxjava.common.exception.NullException;
54
import com.im4j.kakacache.rxjava.core.CacheEntry;
65
import com.litesuits.orm.LiteOrm;
76
import com.litesuits.orm.db.assit.QueryBuilder;
8-
import com.litesuits.orm.db.assit.WhereBuilder;
97

10-
import java.io.IOException;
11-
import java.util.Collection;
128
import java.util.List;
139

1410
/**
@@ -24,7 +20,7 @@ public LRUDiskJournal(LiteOrm liteOrm) {
2420
@Override
2521
public String getLoseKey() throws CacheException {
2622
QueryBuilder query = new QueryBuilder(CacheEntry.class);
27-
query.orderBy(CacheEntry.COL_USE_TIME);
23+
query.orderBy(CacheEntry.COL_LAST_USE_TIME);
2824
query.limit(0, 1);
2925
List<CacheEntry> list = getDb().query(query);
3026
if (list != null && list.size() >0) {

library/src/main/java/com/im4j/kakacache/rxjava/core/memory/MemoryCache.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,14 @@ protected <T> T doLoad(String key) throws CacheException {
3535
}
3636

3737
@Override
38-
protected <T> void doSave(String key, T value, int expires, CacheTarget target) throws CacheException {
38+
protected <T> void doSave(String key, T value, int maxAge, CacheTarget target) throws CacheException {
3939
if (target == null || target == CacheTarget.NONE || target == CacheTarget.Disk) {
4040
return;
4141
}
4242

4343
// 写入缓存
4444
mStorage.save(key, value);
45-
long currentTime = System.currentTimeMillis();
46-
long expiresTime = currentTime + expires;
47-
mJournal.put(key, new CacheEntry(key, currentTime, currentTime, expiresTime, target));
45+
mJournal.put(key, new CacheEntry(key, maxAge, target));
4846
}
4947

5048
@Override

0 commit comments

Comments
 (0)