mirror of
https://github.com/LamGC/ContentGrabbingJi.git
synced 2025-04-30 06:37:36 +00:00
[Change] CacheObject, LocalHashCacheStore 合并类;
This commit is contained in:
parent
d8d4784c0f
commit
ac38e1b5cf
@ -1,40 +0,0 @@
|
|||||||
package net.lamgc.cgj.bot.cache;
|
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
|
||||||
|
|
||||||
public class CacheObject<T> {
|
|
||||||
|
|
||||||
private AtomicReference<T> value;
|
|
||||||
private AtomicReference<Date> 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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,8 +1,11 @@
|
|||||||
package net.lamgc.cgj.bot.cache;
|
package net.lamgc.cgj.bot.cache;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Hashtable;
|
import java.util.Hashtable;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
public class LocalHashCacheStore<T> implements CacheStore<T> {
|
public class LocalHashCacheStore<T> implements CacheStore<T> {
|
||||||
|
|
||||||
@ -99,4 +102,43 @@ public class LocalHashCacheStore<T> implements CacheStore<T> {
|
|||||||
public boolean supportedList() {
|
public boolean supportedList() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static class CacheObject<T> implements Comparable<CacheObject<T>> {
|
||||||
|
|
||||||
|
private AtomicReference<T> value;
|
||||||
|
private AtomicReference<Date> 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<T> o) {
|
||||||
|
return this.getExpireDate().after(o.getExpireDate()) ? -1 : 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user