Skip to content

Commit daa4c51

Browse files
author
alafighting
committed
改进线程锁处理,更好的支持多线程操作
1 parent fd7d858 commit daa4c51

File tree

5 files changed

+71
-28
lines changed

5 files changed

+71
-28
lines changed

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

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.im4j.kakacache.rxjava.common.utils.Utils;
55

66
import java.util.Collection;
7+
import java.util.concurrent.locks.ReadWriteLock;
8+
import java.util.concurrent.locks.ReentrantReadWriteLock;
79

810
/**
911
* 缓存基类
@@ -13,6 +15,7 @@ public abstract class Cache {
1315

1416
protected final long mMaxSize;
1517
protected final long mMaxQuantity;
18+
private final ReadWriteLock mLock = new ReentrantReadWriteLock();
1619

1720
public Cache(long maxSize, long maxQuantity) {
1821
this.mMaxSize = maxSize;
@@ -39,8 +42,13 @@ public final <T> T load(String key) throws CacheException {
3942
return null;
4043
}
4144

42-
// 读取缓存
43-
return doLoad(key);
45+
mLock.readLock().lock();
46+
try {
47+
// 读取缓存
48+
return doLoad(key);
49+
} finally {
50+
mLock.readLock().unlock();
51+
}
4452
}
4553

4654
/**
@@ -65,8 +73,13 @@ public final <T> void save(String key, T value, int maxAge, CacheTarget target)
6573

6674
// TODO 先写入,后清理。会超出限定条件,需要一定交换空间
6775

68-
// 写入缓存
69-
doSave(key, value, maxAge, target);
76+
mLock.writeLock().lock();
77+
try {
78+
// 写入缓存
79+
doSave(key, value, maxAge, target);
80+
} finally {
81+
mLock.writeLock().unlock();
82+
}
7083

7184
// 清理无用数据
7285
clearUnused();
@@ -89,18 +102,57 @@ public final <T> void save(String key, T value, int maxAge, CacheTarget target)
89102
* @param key
90103
* @return
91104
*/
92-
public abstract boolean containsKey(String key);
105+
public final boolean containsKey(String key) {
106+
mLock.writeLock().lock();
107+
try {
108+
return doContainsKey(key);
109+
} finally {
110+
mLock.writeLock().unlock();
111+
}
112+
}
113+
114+
/**
115+
* 删除缓存
116+
* @param key
117+
*/
118+
public final void remove(String key) throws CacheException {
119+
mLock.writeLock().lock();
120+
try {
121+
doRemove(key);
122+
} finally {
123+
mLock.writeLock().unlock();
124+
}
125+
}
126+
127+
/**
128+
* 清空缓存
129+
*/
130+
public final void clear() throws CacheException {
131+
mLock.writeLock().lock();
132+
try {
133+
doClear();
134+
} finally {
135+
mLock.writeLock().unlock();
136+
}
137+
}
138+
139+
/**
140+
* 是否包含
141+
* @param key
142+
* @return
143+
*/
144+
protected abstract boolean doContainsKey(String key);
93145

94146
/**
95147
* 删除缓存
96148
* @param key
97149
*/
98-
public abstract void remove(String key) throws CacheException;
150+
protected abstract void doRemove(String key) throws CacheException;
99151

100152
/**
101153
* 清空缓存
102154
*/
103-
public abstract void clear() throws CacheException;
155+
protected abstract void doClear() throws CacheException;
104156

105157

106158
/**

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
@@ -82,18 +82,18 @@ protected boolean isExpiry(String key) {
8282
}
8383

8484
@Override
85-
public boolean containsKey(String key) {
85+
protected boolean doContainsKey(String key) {
8686
return mJournal.containsKey(key);
8787
}
8888

8989
@Override
90-
public void remove(String key) throws CacheException {
90+
protected void doRemove(String key) throws CacheException {
9191
mStorage.remove(key);
9292
mJournal.remove(key);
9393
}
9494

9595
@Override
96-
public void clear() throws CacheException {
96+
protected void doClear() throws CacheException {
9797
mStorage.clear();
9898
mJournal.clear();
9999
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,25 @@ protected boolean isExpiry(String key) {
5252
}
5353

5454
@Override
55-
public boolean containsKey(String key) {
55+
protected boolean doContainsKey(String key) {
5656
return mJournal.containsKey(key);
5757
}
5858

5959
/**
6060
* 删除缓存
6161
* @param key
6262
*/
63-
public void remove(String key) throws CacheException {
63+
@Override
64+
protected void doRemove(String key) throws CacheException {
6465
mStorage.remove(key);
6566
mJournal.remove(key);
6667
}
6768

6869
/**
6970
* 清空缓存
7071
*/
71-
public void clear() throws CacheException {
72+
@Override
73+
protected void doClear() throws CacheException {
7274
mStorage.clear();
7375
mJournal.clear();
7476
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import java.util.Map;
1313

1414
/**
15-
* 简单的磁盘存储
15+
* 简单的内存存储
1616
* @version alafighting 2016-06
1717
*/
1818
public class SimpleMemoryStorage implements IMemoryStorage {

library/src/main/java/com/im4j/kakacache/rxjava/manager/RxCacheManager.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ public final void call(Subscriber<? super T> subscriber) {
3939
}
4040

4141

42-
private final Object lock = new Object();
43-
4442
private CacheCore cache;
4543
private int defaultExpires;
4644

@@ -61,7 +59,6 @@ public <T> rx.Observable<T> load(final String key) {
6159
return rx.Observable.create(new SimpleSubscribe<T>() {
6260
@Override
6361
T execute() {
64-
// TODO 是否需要加锁?避免脏数据
6562
LogUtils.debug("loadCache key="+key);
6663
return cache.load(key);
6764
}
@@ -82,10 +79,7 @@ public <T> rx.Observable<Boolean> save(final String key, final T value, final in
8279
return rx.Observable.create(new SimpleSubscribe<Boolean>() {
8380
@Override
8481
Boolean execute() throws Throwable {
85-
// 同步
86-
synchronized(lock) {
87-
cache.save(key, value, expires, target);
88-
}
82+
cache.save(key, value, expires, target);
8983
return true;
9084
}
9185
});
@@ -114,9 +108,7 @@ public rx.Observable<Boolean> remove(final String key) {
114108
return rx.Observable.create(new SimpleSubscribe<Boolean>() {
115109
@Override
116110
Boolean execute() throws Throwable {
117-
synchronized(lock) {
118-
cache.remove(key);
119-
}
111+
cache.remove(key);
120112
return true;
121113
}
122114
});
@@ -129,10 +121,7 @@ public rx.Observable<Boolean> clear() throws CacheException {
129121
return rx.Observable.create(new SimpleSubscribe<Boolean>() {
130122
@Override
131123
Boolean execute() throws Throwable {
132-
// 同步
133-
synchronized(lock) {
134-
cache.clear();
135-
}
124+
cache.clear();
136125
return true;
137126
}
138127
});

0 commit comments

Comments
 (0)