Skip to content

Commit

Permalink
perf: improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
areyouok committed Aug 21, 2024
1 parent b275897 commit 984aa2a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

import java.lang.ref.WeakReference;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
* Created on 2017/2/28.
Expand All @@ -16,38 +15,27 @@
*/
class Cleaner {

static LinkedList<WeakReference<LinkedHashMapCache>> linkedHashMapCaches = new LinkedList<>();
private static final ReentrantLock reentrantLock = new ReentrantLock();
static ConcurrentLinkedQueue<WeakReference<LinkedHashMapCache>> linkedHashMapCaches = new ConcurrentLinkedQueue<>();

static {
ScheduledExecutorService executorService = JetCacheExecutor.defaultExecutor();
executorService.scheduleWithFixedDelay(() -> run(), 60, 60, TimeUnit.SECONDS);
}

static void add(LinkedHashMapCache cache) {
reentrantLock.lock();
try{
linkedHashMapCaches.add(new WeakReference<>(cache));
}finally {
reentrantLock.unlock();
}
linkedHashMapCaches.add(new WeakReference<>(cache));
}

static void run() {
reentrantLock.lock();
try{
Iterator<WeakReference<LinkedHashMapCache>> it = linkedHashMapCaches.iterator();
while (it.hasNext()) {
WeakReference<LinkedHashMapCache> ref = it.next();
LinkedHashMapCache c = ref.get();
if (c == null) {
it.remove();
} else {
c.cleanExpiredEntry();
}
Iterator<WeakReference<LinkedHashMapCache>> it = linkedHashMapCaches.iterator();
while (it.hasNext()) {
WeakReference<LinkedHashMapCache> ref = it.next();
LinkedHashMapCache c = ref.get();
if (c == null) {
it.remove();
} else {
c.cleanExpiredEntry();
}
}finally {
reentrantLock.unlock();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,30 @@ protected boolean removeEldestEntry(Map.Entry eldest) {
void cleanExpiredEntry() {
Lock lock = readWriteLock.writeLock();
lock.lock();
try{
for (Iterator it = entrySet().iterator(); it.hasNext();) {
long t = System.currentTimeMillis();
try {
for (Iterator it = entrySet().iterator(); it.hasNext(); ) {
Map.Entry en = (Map.Entry) it.next();
Object value = en.getValue();
if (value != null && value instanceof CacheValueHolder) {
CacheValueHolder h = (CacheValueHolder) value;
if (System.currentTimeMillis() >= h.getExpireTime()) {
if (value != null) {
CacheValueHolder h;
try {
h = (CacheValueHolder) value;
} catch (ClassCastException e) {
// assert false
logger.error("value of key " + en.getKey() + " is not a CacheValueHolder. type=" + value.getClass());
it.remove();
continue;
}
if (t >= h.getExpireTime()) {
it.remove();
}
} else {
// assert false
if (value == null) {
logger.error("key " + en.getKey() + " is null");
} else {
logger.error("value of key " + en.getKey() + " is not a CacheValueHolder. type=" + value.getClass());
}
logger.error("key " + en.getKey() + " is null");
}
}
}finally {
} finally {
lock.unlock();
}
}
Expand Down

0 comments on commit 984aa2a

Please sign in to comment.