From ac38e1b5cf3337c9e8a563d0ae776f443f6753df Mon Sep 17 00:00:00 2001 From: LamGC Date: Thu, 16 Apr 2020 23:48:59 +0800 Subject: [PATCH] =?UTF-8?q?[Change]=20CacheObject,=20LocalHashCacheStore?= =?UTF-8?q?=20=E5=90=88=E5=B9=B6=E7=B1=BB;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../net/lamgc/cgj/bot/cache/CacheObject.java | 40 ------------------ .../cgj/bot/cache/LocalHashCacheStore.java | 42 +++++++++++++++++++ 2 files changed, 42 insertions(+), 40 deletions(-) delete mode 100644 src/main/java/net/lamgc/cgj/bot/cache/CacheObject.java diff --git a/src/main/java/net/lamgc/cgj/bot/cache/CacheObject.java b/src/main/java/net/lamgc/cgj/bot/cache/CacheObject.java deleted file mode 100644 index 281d95d..0000000 --- a/src/main/java/net/lamgc/cgj/bot/cache/CacheObject.java +++ /dev/null @@ -1,40 +0,0 @@ -package net.lamgc.cgj.bot.cache; - -import java.util.Date; -import java.util.concurrent.atomic.AtomicReference; - -public class CacheObject { - - private AtomicReference value; - private AtomicReference expire; - - public CacheObject() { - this(null, null); - } - - public CacheObject(T value, Date expire) { - this.value = new AtomicReference<>(value); - this.expire = new AtomicReference<>(expire); - } - - public synchronized void update(T value, Date newExpire) { - if(new Date().after(newExpire)) { - throw new IllegalArgumentException("Due earlier than current time"); - } - this.expire.set(newExpire); - this.value.set(value); - } - - public synchronized T get() { - return value.get(); - } - - public Date getExpireDate() { - return expire.get(); - } - - public boolean isExpire(Date time) { - Date expireDate = getExpireDate(); - return expireDate != null && expireDate.before(time); - } -} diff --git a/src/main/java/net/lamgc/cgj/bot/cache/LocalHashCacheStore.java b/src/main/java/net/lamgc/cgj/bot/cache/LocalHashCacheStore.java index 967c210..e2f560c 100644 --- a/src/main/java/net/lamgc/cgj/bot/cache/LocalHashCacheStore.java +++ b/src/main/java/net/lamgc/cgj/bot/cache/LocalHashCacheStore.java @@ -1,8 +1,11 @@ package net.lamgc.cgj.bot.cache; +import org.jetbrains.annotations.NotNull; + import java.util.Date; import java.util.Hashtable; import java.util.Objects; +import java.util.concurrent.atomic.AtomicReference; public class LocalHashCacheStore implements CacheStore { @@ -99,4 +102,43 @@ public class LocalHashCacheStore implements CacheStore { public boolean supportedList() { return false; } + + + public static class CacheObject implements Comparable> { + + private AtomicReference value; + private AtomicReference expire; + + public CacheObject(T value, Date expire) { + this.value = new AtomicReference<>(value); + this.expire = new AtomicReference<>(expire); + } + + public synchronized void update(T value, Date newExpire) { + if(new Date().after(newExpire)) { + throw new IllegalArgumentException("Due earlier than current time"); + } + this.expire.set(newExpire); + this.value.set(value); + } + + public synchronized T get() { + return value.get(); + } + + public Date getExpireDate() { + return expire.get(); + } + + public boolean isExpire(Date time) { + Date expireDate = getExpireDate(); + return expireDate != null && expireDate.before(time); + } + + @Override + public int compareTo(@NotNull CacheObject o) { + return this.getExpireDate().after(o.getExpireDate()) ? -1 : 1; + } + } + }