Skip to content

Commit 559f46c

Browse files
author
alafighting
committed
去除抛出异常的声明;增加执行操作的返回值
1 parent c1c23b0 commit 559f46c

25 files changed

+154
-176
lines changed

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

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.im4j.kakacache.rxjava.core;
22

3-
import com.im4j.kakacache.rxjava.common.exception.CacheException;
43
import com.im4j.kakacache.rxjava.common.utils.Utils;
54

65
import java.util.Collection;
@@ -11,13 +10,13 @@
1110
* 缓存基类
1211
* @version alafighting 2016-04
1312
*/
14-
public abstract class Cache {
13+
public abstract class BasicCache {
1514

1615
protected final long mMaxSize;
1716
protected final long mMaxQuantity;
1817
private final ReadWriteLock mLock = new ReentrantReadWriteLock();
1918

20-
public Cache(long maxSize, long maxQuantity) {
19+
public BasicCache(long maxSize, long maxQuantity) {
2120
this.mMaxSize = maxSize;
2221
this.mMaxQuantity = maxQuantity;
2322
}
@@ -29,7 +28,7 @@ public Cache(long maxSize, long maxQuantity) {
2928
* @param <T>
3029
* @return
3130
*/
32-
public final <T> T load(String key) throws CacheException {
31+
public final <T> T load(String key) {
3332
Utils.checkNotNull(key);
3433

3534
if (!containsKey(key)) {
@@ -57,39 +56,39 @@ public final <T> T load(String key) throws CacheException {
5756
* @param <T>
5857
* @return
5958
*/
60-
protected abstract <T> T doLoad(String key) throws CacheException;
59+
protected abstract <T> T doLoad(String key);
6160

6261
/**
6362
* 保存
6463
* @param maxAge 最大有效期时长(单位:秒)
6564
*/
66-
public final <T> void save(String key, T value, int maxAge, CacheTarget target) throws CacheException {
65+
public final <T> boolean save(String key, T value, int maxAge, CacheTarget target) {
6766
Utils.checkNotNull(key);
6867

6968
if (value == null) {
70-
remove(key);
71-
return;
69+
return remove(key);
7270
}
7371

7472
// TODO 先写入,后清理。会超出限定条件,需要一定交换空间
75-
73+
boolean status = false;
7674
mLock.writeLock().lock();
7775
try {
7876
// 写入缓存
79-
doSave(key, value, maxAge, target);
77+
status = doSave(key, value, maxAge, target);
8078
} finally {
8179
mLock.writeLock().unlock();
8280
}
8381

8482
// 清理无用数据
8583
clearUnused();
84+
return status;
8685
}
8786

8887
/**
8988
* 保存
9089
* @param maxAge 最长有效期时长(单位:毫秒)
9190
*/
92-
protected abstract <T> void doSave(String key, T value, int maxAge, CacheTarget target) throws CacheException;
91+
protected abstract <T> boolean doSave(String key, T value, int maxAge, CacheTarget target);
9392

9493

9594
/**
@@ -115,10 +114,10 @@ public final boolean containsKey(String key) {
115114
* 删除缓存
116115
* @param key
117116
*/
118-
public final void remove(String key) throws CacheException {
117+
public final boolean remove(String key) {
119118
mLock.writeLock().lock();
120119
try {
121-
doRemove(key);
120+
return doRemove(key);
122121
} finally {
123122
mLock.writeLock().unlock();
124123
}
@@ -127,10 +126,10 @@ public final void remove(String key) throws CacheException {
127126
/**
128127
* 清空缓存
129128
*/
130-
public final void clear() throws CacheException {
129+
public final boolean clear() {
131130
mLock.writeLock().lock();
132131
try {
133-
doClear();
132+
return doClear();
134133
} finally {
135134
mLock.writeLock().unlock();
136135
}
@@ -147,12 +146,12 @@ public final void clear() throws CacheException {
147146
* 删除缓存
148147
* @param key
149148
*/
150-
protected abstract void doRemove(String key) throws CacheException;
149+
protected abstract boolean doRemove(String key);
151150

152151
/**
153152
* 清空缓存
154153
*/
155-
protected abstract void doClear() throws CacheException;
154+
protected abstract boolean doClear();
156155

157156

158157
/**
@@ -165,7 +164,7 @@ public final void clear() throws CacheException {
165164
* 获取准备丢弃的Key
166165
* @return 准备丢弃的Key(如存储空间不足时,需要清理)
167166
*/
168-
public abstract String getLoseKey() throws CacheException;
167+
public abstract String getLoseKey();
169168

170169
/**
171170
* 缓存大小

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.im4j.kakacache.rxjava.core;
22

3-
import com.im4j.kakacache.rxjava.common.exception.CacheException;
43
import com.im4j.kakacache.rxjava.common.utils.Utils;
54
import com.im4j.kakacache.rxjava.core.disk.DiskCache;
65
import com.im4j.kakacache.rxjava.core.disk.converter.IDiskConverter;
@@ -29,7 +28,7 @@ private CacheCore(MemoryCache memory, DiskCache disk) {
2928
/**
3029
* 读取
3130
*/
32-
public <T> T load(String key) throws CacheException {
31+
public <T> T load(String key) {
3332
if (memory != null) {
3433
T result = (T) memory.load(key);
3534
if (result != null) {
@@ -52,11 +51,9 @@ public <T> T load(String key) throws CacheException {
5251
*
5352
* @param expires 有效期(单位:毫秒)
5453
*/
55-
public <T> void save(String key, T value, int expires, CacheTarget target) throws CacheException {
54+
public <T> boolean save(String key, T value, int expires, CacheTarget target) {
5655
if (value == null) {
57-
memory.remove(key);
58-
disk.remove(key);
59-
return;
56+
return memory.remove(key) && disk.remove(key);
6057
}
6158

6259
if (memory != null) {
@@ -66,8 +63,10 @@ public <T> void save(String key, T value, int expires, CacheTarget target) throw
6663
memory.save(key, value, expires, target);
6764
}
6865
if (disk != null) {
69-
disk.save(key, value, expires, target);
66+
return disk.save(key, value, expires, target);
7067
}
68+
69+
return false;
7170
}
7271

7372
/**
@@ -95,7 +94,7 @@ public boolean containsKey(String key) {
9594
*
9695
* @param key
9796
*/
98-
public void remove(String key) throws CacheException {
97+
public void remove(String key) {
9998
if (memory != null) {
10099
memory.remove(key);
101100
}
@@ -107,7 +106,7 @@ public void remove(String key) throws CacheException {
107106
/**
108107
* 清空缓存
109108
*/
110-
public void clear() throws CacheException {
109+
public void clear() {
111110
if (memory != null) {
112111
memory.clear();
113112
}

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ public class CacheEntry implements Serializable, Cloneable {
4949
*/
5050
@Column(COL_TARGET)
5151
private CacheTarget target;
52-
// TODO 有待商讨
53-
// private long size;
5452

5553

5654
CacheEntry(CacheEntry entry) {
@@ -86,9 +84,35 @@ public CacheEntry clone() {
8684
return new CacheEntry(this);
8785
}
8886

87+
@Override
88+
public boolean equals(Object o) {
89+
if (this == o) return true;
90+
if (o == null || getClass() != o.getClass()) return false;
91+
92+
CacheEntry entry = (CacheEntry) o;
93+
94+
if (createTime != entry.createTime) return false;
95+
if (lastUseTime != entry.lastUseTime) return false;
96+
if (useCount != entry.useCount) return false;
97+
if (expiryTime != entry.expiryTime) return false;
98+
if (key != null ? !key.equals(entry.key) : entry.key != null) return false;
99+
return target == entry.target;
100+
}
101+
102+
@Override
103+
public int hashCode() {
104+
int result = key != null ? key.hashCode() : 0;
105+
result = 31 * result + (int) (createTime ^ (createTime >>> 32));
106+
result = 31 * result + (int) (lastUseTime ^ (lastUseTime >>> 32));
107+
result = 31 * result + (int) (useCount ^ (useCount >>> 32));
108+
result = 31 * result + (int) (expiryTime ^ (expiryTime >>> 32));
109+
result = 31 * result + (target != null ? target.hashCode() : 0);
110+
return result;
111+
}
112+
89113
@Override
90114
public String toString() {
91-
return "CacheEntry{" +
115+
return "{" +
92116
"key='" + key + '\'' +
93117
", createTime=" + createTime +
94118
", lastUseTime=" + lastUseTime +

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package com.im4j.kakacache.rxjava.core.disk;
22

33
import com.google.gson.reflect.TypeToken;
4-
import com.im4j.kakacache.rxjava.common.exception.CacheException;
54
import com.im4j.kakacache.rxjava.common.utils.Utils;
6-
import com.im4j.kakacache.rxjava.core.Cache;
5+
import com.im4j.kakacache.rxjava.core.BasicCache;
76
import com.im4j.kakacache.rxjava.core.CacheEntry;
87
import com.im4j.kakacache.rxjava.core.CacheTarget;
98
import com.im4j.kakacache.rxjava.core.disk.converter.IDiskConverter;
@@ -18,7 +17,7 @@
1817
* 磁盘缓存
1918
* @version 0.1 king 2016-04
2019
*/
21-
public final class DiskCache extends Cache {
20+
public final class DiskCache extends BasicCache {
2221

2322
private final IDiskStorage mStorage;
2423
private final IDiskJournal mJournal;
@@ -44,7 +43,7 @@ public DiskCache(IDiskStorage storage,
4443
*/
4544

4645
@Override
47-
protected <T> T doLoad(String key) throws CacheException {
46+
protected <T> T doLoad(String key) {
4847
// 读取缓存
4948
InputStream source = mStorage.load(key);
5049
T value = null;
@@ -60,9 +59,9 @@ protected <T> T doLoad(String key) throws CacheException {
6059
* @param maxAge 最大有效期时长(单位:毫秒)
6160
*/
6261
@Override
63-
protected <T> void doSave(String key, T value, int maxAge, CacheTarget target) throws CacheException {
62+
protected <T> boolean doSave(String key, T value, int maxAge, CacheTarget target) {
6463
if (target == null || target == CacheTarget.NONE || target == CacheTarget.Memory) {
65-
return;
64+
return true;
6665
}
6766

6867
// 写入缓存
@@ -72,7 +71,10 @@ protected <T> void doSave(String key, T value, int maxAge, CacheTarget target) t
7271
Utils.close(sink);
7372

7473
mJournal.put(key, new CacheEntry(key, maxAge, target));
74+
return true;
7575
}
76+
77+
return false;
7678
}
7779

7880
@Override
@@ -87,15 +89,13 @@ protected boolean doContainsKey(String key) {
8789
}
8890

8991
@Override
90-
protected void doRemove(String key) throws CacheException {
91-
mStorage.remove(key);
92-
mJournal.remove(key);
92+
protected boolean doRemove(String key) {
93+
return mStorage.remove(key) && mJournal.remove(key);
9394
}
9495

9596
@Override
96-
protected void doClear() throws CacheException {
97-
mStorage.clear();
98-
mJournal.clear();
97+
protected boolean doClear() {
98+
return mStorage.clear() && mJournal.clear();
9999
}
100100

101101
@Override
@@ -104,7 +104,7 @@ public Collection<CacheEntry> snapshot() {
104104
}
105105

106106
@Override
107-
public String getLoseKey() throws CacheException {
107+
public String getLoseKey() {
108108
return mJournal.getLoseKey();
109109
}
110110

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,21 @@ public Object load(InputStream source, Type type) {
3939
}
4040

4141
@Override
42-
public void writer(OutputStream sink, Object data) {
42+
public boolean writer(OutputStream sink, Object data) {
4343
try {
4444
String json = gson.toJson(data);
4545
byte[] bytes = json.getBytes();
4646
sink.write(bytes, 0, bytes.length);
4747
sink.flush();
48+
return true;
4849
} catch (JsonIOException e) {
4950
LogUtils.log(e);
5051
} catch (JsonSyntaxException e) {
5152
LogUtils.log(e);
5253
} catch (IOException e) {
5354
LogUtils.log(e);
5455
}
56+
return false;
5557
}
5658

5759
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ public interface IDiskConverter {
2626
* @param data
2727
* @return
2828
*/
29-
void writer(OutputStream sink, Object data);
29+
boolean writer(OutputStream sink, Object data);
3030

3131
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.esotericsoftware.kryo.Kryo;
44
import com.esotericsoftware.kryo.io.Input;
55
import com.esotericsoftware.kryo.io.Output;
6+
import com.im4j.kakacache.rxjava.common.exception.Exception;
67
import com.im4j.kakacache.rxjava.common.utils.Utils;
78

89
import java.io.InputStream;
@@ -34,11 +35,14 @@ public Object load(InputStream source, Type type) {
3435
}
3536

3637
@Override
37-
public void writer(OutputStream sink, Object data) {
38+
public boolean writer(OutputStream sink, Object data) {
3839
Output output = null;
3940
try {
4041
output = new Output(sink);
4142
kryo.writeClassAndObject(output, data);
43+
return true;
44+
} catch (Exception ignored) {
45+
return false;
4246
} finally {
4347
Utils.close(output);
4448
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,16 @@ public Object load(InputStream source, Type type) {
3434
}
3535

3636
@Override
37-
public void writer(OutputStream sink, Object data) {
37+
public boolean writer(OutputStream sink, Object data) {
3838
ObjectOutputStream oos = null;
3939
try {
4040
oos = new ObjectOutputStream(sink);
4141
oos.writeObject(data);
4242
oos.flush(); //缓冲流
43+
return true;
4344
} catch (IOException e) {
4445
LogUtils.log(e);
46+
return false;
4547
} finally {
4648
Utils.close(oos);
4749
}

0 commit comments

Comments
 (0)