CachedLocalSource
This commit is contained in:
@@ -52,7 +52,7 @@ public class CachedLocalSource implements Service {
|
|||||||
this.scheduler = Utility.newScheduledExecutor(
|
this.scheduler = Utility.newScheduledExecutor(
|
||||||
1, "Redkale-" + CachedLocalSource.class.getSimpleName() + "-Expirer-Thread");
|
1, "Redkale-" + CachedLocalSource.class.getSimpleName() + "-Expirer-Thread");
|
||||||
final List<String> keys = new ArrayList<>();
|
final List<String> keys = new ArrayList<>();
|
||||||
int interval = 30;
|
int interval = 15;
|
||||||
scheduler.scheduleWithFixedDelay(
|
scheduler.scheduleWithFixedDelay(
|
||||||
() -> {
|
() -> {
|
||||||
try {
|
try {
|
||||||
@@ -69,7 +69,11 @@ public class CachedLocalSource implements Service {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
logger.log(Level.SEVERE, "CachedLocalSource schedule(interval=" + interval + "s) error", t);
|
logger.log(
|
||||||
|
Level.SEVERE,
|
||||||
|
CachedLocalSource.class.getSimpleName() + " schedule(interval=" + interval
|
||||||
|
+ "s) error",
|
||||||
|
t);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
interval,
|
interval,
|
||||||
@@ -120,11 +124,21 @@ public class CachedLocalSource implements Service {
|
|||||||
return CompletableFuture.runAsync(() -> set(name, key, localLimit, millis, type, value));
|
return CompletableFuture.runAsync(() -> set(name, key, localLimit, millis, type, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getKeyCount(String name) {
|
public int getSize(String name) {
|
||||||
CacheMap map = container.get(name);
|
CacheMap map = container.get(name);
|
||||||
return map == null ? -1 : map.size();
|
return map == null ? -1 : map.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int updateLimit(String name, int limit) {
|
||||||
|
CacheMap map = container.get(name);
|
||||||
|
if (map == null) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
int old = map.limit;
|
||||||
|
map.limit = limit;
|
||||||
|
return old;
|
||||||
|
}
|
||||||
|
|
||||||
protected static class CacheMap {
|
protected static class CacheMap {
|
||||||
|
|
||||||
protected final ReentrantLock lock = new ReentrantLock();
|
protected final ReentrantLock lock = new ReentrantLock();
|
||||||
@@ -169,7 +183,7 @@ public class CachedLocalSource implements Service {
|
|||||||
return map.size();
|
return map.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
void checkLimit() {
|
protected void checkLimit() {
|
||||||
int l = limit;
|
int l = limit;
|
||||||
if (l > 0 && map.size() > l) {
|
if (l > 0 && map.size() > l) {
|
||||||
lock.lock();
|
lock.lock();
|
||||||
@@ -197,7 +211,7 @@ public class CachedLocalSource implements Service {
|
|||||||
protected String value;
|
protected String value;
|
||||||
|
|
||||||
// 为0表示永久, 大于0表示有过期时间
|
// 为0表示永久, 大于0表示有过期时间
|
||||||
private long endMillis;
|
protected long endMillis;
|
||||||
|
|
||||||
private long createTime = System.currentTimeMillis();
|
private long createTime = System.currentTimeMillis();
|
||||||
|
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ public class CachedManagerTest {
|
|||||||
manager.localGetSet("group", "key2", CachingBean.class, true, 2, expire, () -> new CachingBean("v2", "r2"));
|
manager.localGetSet("group", "key2", CachingBean.class, true, 2, expire, () -> new CachingBean("v2", "r2"));
|
||||||
Utility.sleep(2);
|
Utility.sleep(2);
|
||||||
manager.localGetSet("group", "key3", CachingBean.class, true, 2, expire, () -> new CachingBean("v3", "r3"));
|
manager.localGetSet("group", "key3", CachingBean.class, true, 2, expire, () -> new CachingBean("v3", "r3"));
|
||||||
Assertions.assertEquals(2, manager.getLocalSource().getKeyCount("group"));
|
Assertions.assertEquals(2, manager.getLocalSource().getSize("group"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user