Skip to content

Commit 5f3431e

Browse files
author
alafighting
committed
增加更多的MemoryJournal实现(FIFO\LRU\LFU)
1 parent e8201a9 commit 5f3431e

File tree

6 files changed

+178
-54
lines changed

6 files changed

+178
-54
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* 日志项
1111
* @version 0.1 king 2016-04
1212
*/
13-
public class CacheEntry implements Serializable {
13+
public class CacheEntry implements Serializable, Cloneable {
1414
public static final String COL_KEY = "key";
1515
public static final String COL_CREATE_TIME = "create_time";
1616
public static final String COL_LAST_USE_TIME = "last_use_time";
@@ -53,6 +53,14 @@ public class CacheEntry implements Serializable {
5353
// private long size;
5454

5555

56+
CacheEntry(CacheEntry entry) {
57+
this.key = entry.key;
58+
this.createTime = entry.createTime;
59+
this.lastUseTime = entry.lastUseTime;
60+
this.expiryTime = entry.expiryTime;
61+
this.target = entry.target;
62+
this.useCount = entry.useCount;
63+
}
5664
public CacheEntry(String key, long maxAge, CacheTarget target) {
5765
long currentTime = System.currentTimeMillis();
5866

@@ -73,6 +81,11 @@ public boolean isExpiry() {
7381
}
7482

7583

84+
@Override
85+
public CacheEntry clone() {
86+
return new CacheEntry(this);
87+
}
88+
7689
@Override
7790
public String toString() {
7891
return "CacheEntry{" +

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ final LiteOrm getDb() {
2727
}
2828

2929
@Override
30-
public CacheEntry get(String key) {
30+
public final CacheEntry get(String key) {
3131
if (Utils.isEmpty(key)) {
3232
throw new NullException("key == null");
3333
}
@@ -47,7 +47,7 @@ public CacheEntry get(String key) {
4747
}
4848

4949
@Override
50-
public void put(String key, CacheEntry entry) {
50+
public final void put(String key, CacheEntry entry) {
5151
if (Utils.isEmpty(key) || entry == null) {
5252
throw new NullException("key == null || value == null");
5353
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.im4j.kakacache.rxjava.core.memory.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+
8+
import java.io.IOException;
9+
import java.util.Collection;
10+
import java.util.LinkedHashMap;
11+
12+
/**
13+
* 缓存日志-基类
14+
* @version alafighting 2016-07
15+
*/
16+
public abstract class BasicMemoryJournal implements IMemoryJournal {
17+
18+
private final LinkedHashMap<String, CacheEntry> mKeyValues;
19+
20+
public BasicMemoryJournal() {
21+
this.mKeyValues = new LinkedHashMap<>(0, 0.75f, true);
22+
}
23+
24+
final LinkedHashMap<String, CacheEntry> getKeyValues() {
25+
return mKeyValues;
26+
}
27+
28+
@Override
29+
public CacheEntry get(String key) {
30+
if (Utils.isEmpty(key)) {
31+
throw new NullException("key == null");
32+
}
33+
34+
CacheEntry entry = mKeyValues.get(key);
35+
if (entry != null) {
36+
// 有效期内,才记录最后使用时间
37+
if (entry.isExpiry()) {
38+
entry.setLastUseTime(System.currentTimeMillis());
39+
entry.setUseCount(entry.getUseCount() + 1);
40+
}
41+
return entry.clone();
42+
} else {
43+
return null;
44+
}
45+
}
46+
47+
@Override
48+
public final void put(String key, CacheEntry entry) {
49+
if (Utils.isEmpty(key) || entry == null) {
50+
throw new NullException("key == null || value == null");
51+
}
52+
53+
if (entry.isExpiry()) {
54+
entry.setLastUseTime(System.currentTimeMillis());
55+
entry.setUseCount(1);
56+
mKeyValues.put(key, entry);
57+
} else {
58+
remove(key);
59+
}
60+
mKeyValues.put(key, entry);
61+
}
62+
63+
@Override
64+
public boolean containsKey(String key) {
65+
if (Utils.isEmpty(key)) {
66+
throw new NullException("key == null");
67+
}
68+
69+
CacheEntry entry = mKeyValues.get(key);
70+
return entry != null;
71+
}
72+
73+
@Override
74+
public abstract String getLoseKey() throws CacheException;
75+
76+
@Override
77+
public void remove(String key) {
78+
if (Utils.isEmpty(key)) {
79+
throw new NullException("key == null");
80+
}
81+
82+
mKeyValues.remove(key);
83+
}
84+
85+
@Override
86+
public void clear() {
87+
mKeyValues.clear();
88+
}
89+
90+
@Override
91+
public Collection<CacheEntry> snapshot() {
92+
return mKeyValues.values();
93+
}
94+
95+
@Override
96+
public void close() throws IOException {
97+
// TODO Nothing
98+
}
99+
100+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.im4j.kakacache.rxjava.core.memory.journal;
2+
3+
import com.im4j.kakacache.rxjava.common.exception.CacheException;
4+
import com.im4j.kakacache.rxjava.core.CacheEntry;
5+
6+
/**
7+
* FIFO缓存日志
8+
* @version alafighting 2016-07
9+
*/
10+
public class FIFOMemoryJournal extends BasicMemoryJournal {
11+
12+
@Override
13+
public String getLoseKey() throws CacheException {
14+
CacheEntry entry = null;
15+
for (CacheEntry item : getKeyValues().values()) {
16+
if (entry == null || entry.getCreateTime() > item.getCreateTime()) {
17+
entry = item;
18+
}
19+
}
20+
if (entry != null) {
21+
return entry.getKey();
22+
} else {
23+
return null;
24+
}
25+
}
26+
27+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.im4j.kakacache.rxjava.core.memory.journal;
2+
3+
import com.im4j.kakacache.rxjava.common.exception.CacheException;
4+
import com.im4j.kakacache.rxjava.core.CacheEntry;
5+
6+
/**
7+
* LFU缓存日志
8+
* @version alafighting 2016-07
9+
*/
10+
public class LFUMemoryJournal extends BasicMemoryJournal {
11+
12+
@Override
13+
public String getLoseKey() throws CacheException {
14+
CacheEntry entry = null;
15+
for (CacheEntry item : getKeyValues().values()) {
16+
if (entry == null || entry.getUseCount() > item.getUseCount()) {
17+
entry = item;
18+
} else {
19+
if (entry.getUseCount() == item.getUseCount()
20+
&& entry.getLastUseTime() > item.getLastUseTime()) {
21+
entry = item;
22+
}
23+
}
24+
}
25+
if (entry != null) {
26+
return entry.getKey();
27+
} else {
28+
return null;
29+
}
30+
}
31+
32+
}

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

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,61 +10,13 @@
1010

1111
/**
1212
* LRU缓存日志
13-
* @version alafighting 2016-06
13+
* @version alafighting 2016-07
1414
*/
15-
public class LRUMemoryJournal implements IMemoryJournal {
16-
17-
private final LinkedHashMap<String, CacheEntry> mKeyValue;
18-
19-
public LRUMemoryJournal() {
20-
this.mKeyValue = new LinkedHashMap<>(0, 0.75f, true);
21-
}
22-
23-
@Override
24-
public CacheEntry get(String key) {
25-
if (key == null) {
26-
throw new NullException("key == null");
27-
}
28-
return mKeyValue.get(key);
29-
}
30-
31-
@Override
32-
public void put(String key, CacheEntry entry) {
33-
if (key == null || entry == null) {
34-
throw new NullException("key == null || value == null");
35-
}
36-
mKeyValue.put(key, entry);
37-
}
38-
39-
@Override
40-
public boolean containsKey(String key) {
41-
CacheEntry entry = mKeyValue.get(key);
42-
return entry != null;
43-
}
15+
public class LRUMemoryJournal extends BasicMemoryJournal {
4416

4517
@Override
4618
public String getLoseKey() throws CacheException {
47-
return mKeyValue.entrySet().iterator().next().getKey();
48-
}
49-
50-
@Override
51-
public void remove(String key) {
52-
mKeyValue.remove(key);
53-
}
54-
55-
@Override
56-
public void clear() {
57-
mKeyValue.clear();
58-
}
59-
60-
@Override
61-
public Collection<CacheEntry> snapshot() {
62-
return mKeyValue.values();
63-
}
64-
65-
@Override
66-
public void close() throws IOException {
67-
// TODO Nothing
19+
return getKeyValues().entrySet().iterator().next().getKey();
6820
}
6921

7022
}

0 commit comments

Comments
 (0)