Skip to content

Commit 4e4f1cb

Browse files
author
alafighting
committed
增加更多的DiskJournal实现(FIFO\LR\Unlimited)
1 parent 12aa15f commit 4e4f1cb

File tree

7 files changed

+197
-69
lines changed

7 files changed

+197
-69
lines changed

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,34 @@
1212
*/
1313
public class CacheEntry implements Serializable {
1414
public static final String COL_KEY = "key";
15+
public static final String COL_CREATE_TIME = "create_time";
16+
public static final String COL_USE_TIME = "use_time";
17+
public static final String COL_EXPIRY_TIME = "expiry_time";
18+
public static final String COL_TARGET = "expiry_target";
1519

1620
/** KEY */
1721
@PrimaryKey(AssignType.BY_MYSELF)
1822
@Column(COL_KEY)
1923
private final String key;
2024
/** 创建时间 */
25+
@Column(COL_CREATE_TIME)
2126
private long createTime;
27+
/** 最后使用时间 */
28+
@Column(COL_USE_TIME)
29+
private long useTime;
2230
/** 过期时间 */
31+
@Column(COL_EXPIRY_TIME)
2332
private long expiryTime;
2433
/** 缓存目标 */
34+
@Column(COL_TARGET)
2535
private CacheTarget target;
2636
// TODO 有待商讨
2737
// private long size;
2838

29-
public CacheEntry(String key, long createTime, long expiryTime, CacheTarget target) {
39+
public CacheEntry(String key, long createTime, long useTime, long expiryTime, CacheTarget target) {
3040
this.key = key;
3141
this.createTime = createTime;
42+
this.useTime = useTime;
3243
this.expiryTime = expiryTime;
3344
this.target = target;
3445
}
@@ -47,6 +58,7 @@ public String toString() {
4758
return "CacheEntry{" +
4859
"key='" + key + '\'' +
4960
", createTime=" + createTime +
61+
", useTime=" + useTime +
5062
", expiryTime=" + expiryTime +
5163
", target=" + target +
5264
'}';
@@ -65,20 +77,28 @@ public void setCreateTime(long createTime) {
6577
this.createTime = createTime;
6678
}
6779

68-
public long getExpiryTime() {
69-
return expiryTime;
80+
public long getUseTime() {
81+
return useTime;
7082
}
7183

72-
public void setExpiryTime(long expiryTime) {
73-
this.expiryTime = expiryTime;
84+
public void setUseTime(long useTime) {
85+
this.useTime = useTime;
7486
}
7587

76-
public CacheTarget getCacheTarget() {
88+
public CacheTarget getTarget() {
7789
return target;
7890
}
7991

80-
public void setCacheTarget(CacheTarget target) {
92+
public void setTarget(CacheTarget target) {
8193
this.target = target;
8294
}
8395

96+
public long getExpiryTime() {
97+
return expiryTime;
98+
}
99+
100+
public void setExpiryTime(long expiryTime) {
101+
this.expiryTime = expiryTime;
102+
}
103+
84104
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ protected <T> void doSave(String key, T value, int expires, CacheTarget target)
7171
mConverter.writer(sink, value);
7272
Utils.close(sink);
7373

74-
long createTime = System.currentTimeMillis();
75-
long expiresTime = createTime + expires;
76-
mJournal.put(key, new CacheEntry(key, createTime, expiresTime, target));
74+
long currentTime = System.currentTimeMillis();
75+
long expiresTime = currentTime + expires;
76+
mJournal.put(key, new CacheEntry(key, currentTime, currentTime, expiresTime, target));
7777
}
7878
}
7979

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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.common.exception.NullException;
5+
import com.im4j.kakacache.rxjava.common.utils.Utils;
6+
import com.im4j.kakacache.rxjava.core.CacheEntry;
7+
import com.litesuits.orm.LiteOrm;
8+
import com.litesuits.orm.db.assit.WhereBuilder;
9+
10+
import java.io.IOException;
11+
import java.util.Collection;
12+
13+
/**
14+
* 缓存日志-基类
15+
* @version alafighting 2016-07
16+
*/
17+
public abstract class BasicDiskJournal implements IDiskJournal {
18+
19+
private final LiteOrm mLiteOrm;
20+
21+
public BasicDiskJournal(LiteOrm liteOrm) {
22+
this.mLiteOrm = liteOrm;
23+
}
24+
25+
final LiteOrm getDb() {
26+
return mLiteOrm;
27+
}
28+
29+
@Override
30+
public CacheEntry get(String key) {
31+
if (Utils.isEmpty(key)) {
32+
throw new NullException("key == null");
33+
}
34+
35+
CacheEntry entry = mLiteOrm.queryById(key, CacheEntry.class);
36+
if (entry != null) {
37+
// 有效期内,才记录最后使用时间
38+
if (entry.isExpiry()) {
39+
entry.setUseTime(System.currentTimeMillis());
40+
mLiteOrm.update(entry);
41+
}
42+
return entry;
43+
} else {
44+
return null;
45+
}
46+
}
47+
48+
@Override
49+
public void put(String key, CacheEntry entry) {
50+
if (Utils.isEmpty(key) || entry == null) {
51+
throw new NullException("key == null || value == null");
52+
}
53+
if (entry.isExpiry()) {
54+
entry.setUseTime(System.currentTimeMillis());
55+
mLiteOrm.save(entry);
56+
} else {
57+
remove(key);
58+
}
59+
}
60+
61+
@Override
62+
public final boolean containsKey(String key) {
63+
CacheEntry entry = get(key);
64+
return entry != null;
65+
}
66+
67+
@Override
68+
public abstract String getLoseKey() throws CacheException;
69+
70+
@Override
71+
public final void remove(String key) {
72+
mLiteOrm.delete(new WhereBuilder(CacheEntry.class)
73+
.where(CacheEntry.COL_KEY + " = ?", new String[]{"%"+key+"%"}));
74+
}
75+
76+
@Override
77+
public final void clear() {
78+
mLiteOrm.deleteAll(CacheEntry.class);
79+
}
80+
81+
@Override
82+
public final Collection<CacheEntry> snapshot() {
83+
return mLiteOrm.query(CacheEntry.class);
84+
}
85+
86+
@Override
87+
public void close() throws IOException {
88+
// TODO Nothing
89+
//mLiteOrm.close();
90+
}
91+
92+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.common.exception.NullException;
5+
import com.im4j.kakacache.rxjava.common.utils.Utils;
6+
import com.im4j.kakacache.rxjava.core.CacheEntry;
7+
import com.litesuits.orm.LiteOrm;
8+
import com.litesuits.orm.db.assit.QueryBuilder;
9+
import com.litesuits.orm.db.assit.WhereBuilder;
10+
11+
import java.io.IOException;
12+
import java.util.Collection;
13+
import java.util.List;
14+
15+
/**
16+
* FIFO缓存日志
17+
* @version alafighting 2016-07
18+
*/
19+
public class FIFODiskJournal extends BasicDiskJournal {
20+
21+
public FIFODiskJournal(LiteOrm liteOrm) {
22+
super(liteOrm);
23+
}
24+
25+
@Override
26+
public String getLoseKey() throws CacheException {
27+
QueryBuilder query = new QueryBuilder(CacheEntry.class);
28+
query.orderBy(CacheEntry.COL_CREATE_TIME);
29+
query.limit(0, 1);
30+
List<CacheEntry> list = getDb().query(query);
31+
if (list != null && list.size() >0) {
32+
return list.get(0).getKey();
33+
} else {
34+
return null;
35+
}
36+
}
37+
38+
}

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

Lines changed: 4 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -15,75 +15,23 @@
1515
* LRU缓存日志
1616
* @version alafighting 2016-07
1717
*/
18-
public class LRUDiskJournal implements IDiskJournal {
19-
20-
private final LiteOrm mLiteOrm;
18+
public class LRUDiskJournal extends BasicDiskJournal {
2119

2220
public LRUDiskJournal(LiteOrm liteOrm) {
23-
this.mLiteOrm = liteOrm;
24-
}
25-
26-
@Override
27-
public CacheEntry get(String key) {
28-
if (key == null) {
29-
throw new NullException("key == null");
30-
}
31-
32-
CacheEntry entry = mLiteOrm.queryById(key, CacheEntry.class);
33-
if (entry != null && !entry.isExpiry()) {
34-
return entry;
35-
} else {
36-
return null;
37-
}
38-
}
39-
40-
@Override
41-
public void put(String key, CacheEntry entry) {
42-
if (key == null || entry == null) {
43-
throw new NullException("key == null || value == null");
44-
}
45-
mLiteOrm.save(entry);
46-
}
47-
48-
@Override
49-
public boolean containsKey(String key) {
50-
CacheEntry entry = get(key);
51-
return entry != null && !entry.isExpiry();
21+
super(liteOrm);
5222
}
5323

5424
@Override
5525
public String getLoseKey() throws CacheException {
5626
QueryBuilder query = new QueryBuilder(CacheEntry.class);
57-
query.orderBy(CacheEntry.COL_KEY);
27+
query.orderBy(CacheEntry.COL_USE_TIME);
5828
query.limit(0, 1);
59-
List<CacheEntry> list = mLiteOrm.query(query);
29+
List<CacheEntry> list = getDb().query(query);
6030
if (list != null && list.size() >0) {
6131
return list.get(0).getKey();
6232
} else {
6333
return null;
6434
}
6535
}
6636

67-
@Override
68-
public void remove(String key) {
69-
mLiteOrm.delete(new WhereBuilder(CacheEntry.class)
70-
.where(CacheEntry.COL_KEY + " = ?", new String[]{"%1%"}));
71-
}
72-
73-
@Override
74-
public void clear() {
75-
mLiteOrm.deleteAll(CacheEntry.class);
76-
}
77-
78-
@Override
79-
public Collection<CacheEntry> snapshot() {
80-
return mLiteOrm.query(CacheEntry.class);
81-
}
82-
83-
@Override
84-
public void close() throws IOException {
85-
// TODO Nothing
86-
//mLiteOrm.close();
87-
}
88-
8937
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
7+
/**
8+
* Unlimited缓存日志
9+
* @version alafighting 2016-07
10+
*/
11+
public class UnlimitedDiskJournal extends BasicDiskJournal {
12+
13+
public UnlimitedDiskJournal(LiteOrm liteOrm) {
14+
super(liteOrm);
15+
}
16+
17+
@Override
18+
public CacheEntry get(String key) {
19+
// 有效期造假,可以欺骗不被清理
20+
// cacheEntry.setExpiryTime()
21+
return super.get(key);
22+
}
23+
24+
// 永不清除有效的缓存(过期依旧会被清理)
25+
@Override
26+
public String getLoseKey() throws CacheException {
27+
return null;
28+
}
29+
30+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ protected <T> void doSave(String key, T value, int expires, CacheTarget target)
4242

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

5050
@Override

0 commit comments

Comments
 (0)