CacheSource增加setnxpx接口
This commit is contained in:
@@ -346,6 +346,11 @@ public final class CacheMemorySource extends AbstractCacheSource {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> boolean setnxex(String key, int expireSeconds, Convert convert, Type type, T value) {
|
public <T> boolean setnxex(String key, int expireSeconds, Convert convert, Type type, T value) {
|
||||||
|
return setnxpx(key, expireSeconds * 1000, convert, type, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> boolean setnxpx(String key, int milliSeconds, Convert convert, Type type, T value) {
|
||||||
CacheEntry entry = find(key);
|
CacheEntry entry = find(key);
|
||||||
if (entry == null) {
|
if (entry == null) {
|
||||||
containerLock.lock();
|
containerLock.lock();
|
||||||
@@ -355,7 +360,7 @@ public final class CacheMemorySource extends AbstractCacheSource {
|
|||||||
entry = new CacheEntry(CacheEntryType.OBJECT, key);
|
entry = new CacheEntry(CacheEntryType.OBJECT, key);
|
||||||
container.put(key, entry);
|
container.put(key, entry);
|
||||||
entry.setObjectValue(convert == null ? this.convert : convert, type, value);
|
entry.setObjectValue(convert == null ? this.convert : convert, type, value);
|
||||||
entry.expireSeconds(expireSeconds);
|
entry.milliSeconds(milliSeconds);
|
||||||
entry.lastAccessed = System.currentTimeMillis();
|
entry.lastAccessed = System.currentTimeMillis();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -372,6 +377,11 @@ public final class CacheMemorySource extends AbstractCacheSource {
|
|||||||
return supplyFuture(() -> setnxex(key, expireSeconds, convert, type, value));
|
return supplyFuture(() -> setnxex(key, expireSeconds, convert, type, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> CompletableFuture<Boolean> setnxpxAsync(String key, int milliSeconds, Convert convert, Type type, T value) {
|
||||||
|
return supplyFuture(() -> setnxpx(key, milliSeconds, convert, type, value));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> T getSet(String key, Convert convert, Type type, T value) {
|
public <T> T getSet(String key, Convert convert, Type type, T value) {
|
||||||
CacheEntry entry = find(key, CacheEntryType.OBJECT);
|
CacheEntry entry = find(key, CacheEntryType.OBJECT);
|
||||||
@@ -405,7 +415,7 @@ public final class CacheMemorySource extends AbstractCacheSource {
|
|||||||
return supplyFuture(() -> getDel(key, type));
|
return supplyFuture(() -> getDel(key, type));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void set0(String key, int expireSeconds, Convert convert, Type type, Object value) {
|
private void set0(String key, int milliSeconds, Convert convert, Type type, Object value) {
|
||||||
CacheEntry entry = find(key, CacheEntryType.OBJECT);
|
CacheEntry entry = find(key, CacheEntryType.OBJECT);
|
||||||
if (entry == null) {
|
if (entry == null) {
|
||||||
containerLock.lock();
|
containerLock.lock();
|
||||||
@@ -422,7 +432,7 @@ public final class CacheMemorySource extends AbstractCacheSource {
|
|||||||
entry.lock();
|
entry.lock();
|
||||||
try {
|
try {
|
||||||
entry.setObjectValue(convert == null ? this.convert : convert, type, value);
|
entry.setObjectValue(convert == null ? this.convert : convert, type, value);
|
||||||
entry.expireSeconds(expireSeconds);
|
entry.milliSeconds(milliSeconds);
|
||||||
entry.lastAccessed = System.currentTimeMillis();
|
entry.lastAccessed = System.currentTimeMillis();
|
||||||
} finally {
|
} finally {
|
||||||
entry.unlock();
|
entry.unlock();
|
||||||
@@ -460,7 +470,7 @@ public final class CacheMemorySource extends AbstractCacheSource {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> void setex(String key, int expireSeconds, Convert convert, Type type, T value) {
|
public <T> void setex(String key, int expireSeconds, Convert convert, Type type, T value) {
|
||||||
set0(key, expireSeconds, convert, type, value);
|
set0(key, expireSeconds * 1000, convert, type, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -468,15 +478,30 @@ public final class CacheMemorySource extends AbstractCacheSource {
|
|||||||
return runFuture(() -> setex(key, expireSeconds, convert, type, value));
|
return runFuture(() -> setex(key, expireSeconds, convert, type, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> void setpx(String key, int milliSeconds, Convert convert, Type type, T value) {
|
||||||
|
set0(key, milliSeconds, convert, type, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> CompletableFuture<Void> setpxAsync(String key, int milliSeconds, Convert convert, Type type, T value) {
|
||||||
|
return runFuture(() -> setpx(key, milliSeconds, convert, type, value));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void expire(String key, int expireSeconds) {
|
public void expire(String key, int expireSeconds) {
|
||||||
|
pexpire(key, expireSeconds * 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void pexpire(String key, int milliSeconds) {
|
||||||
CacheEntry entry = find(key);
|
CacheEntry entry = find(key);
|
||||||
if (entry == null) {
|
if (entry == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
entry.lock();
|
entry.lock();
|
||||||
try {
|
try {
|
||||||
entry.expireSeconds(expireSeconds);
|
entry.milliSeconds(milliSeconds);
|
||||||
} finally {
|
} finally {
|
||||||
entry.unlock();
|
entry.unlock();
|
||||||
}
|
}
|
||||||
@@ -487,6 +512,11 @@ public final class CacheMemorySource extends AbstractCacheSource {
|
|||||||
return runFuture(() -> expire(key, expireSeconds));
|
return runFuture(() -> expire(key, expireSeconds));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CompletableFuture<Void> pexpireAsync(String key, int milliSeconds) {
|
||||||
|
return runFuture(() -> pexpire(key, milliSeconds));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean persist(final String key) {
|
public boolean persist(final String key) {
|
||||||
CacheEntry entry = find(key);
|
CacheEntry entry = find(key);
|
||||||
@@ -735,7 +765,7 @@ public final class CacheMemorySource extends AbstractCacheSource {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (expireSeconds > 0) {
|
if (expireSeconds > 0) {
|
||||||
entry.expireSeconds(expireSeconds);
|
entry.milliSeconds(expireSeconds * 1000);
|
||||||
}
|
}
|
||||||
final Convert c = convert == null ? this.convert : convert;
|
final Convert c = convert == null ? this.convert : convert;
|
||||||
// OBJECT, ATOMIC, DOUBLE, SSET, ZSET, LIST, MAP;
|
// OBJECT, ATOMIC, DOUBLE, SSET, ZSET, LIST, MAP;
|
||||||
@@ -2327,8 +2357,8 @@ public final class CacheMemorySource extends AbstractCacheSource {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public CacheEntry expireSeconds(int expireSeconds) {
|
public CacheEntry milliSeconds(int milliSeconds) {
|
||||||
this.expireMills = expireSeconds > 0 ? expireSeconds * 1000 : 0;
|
this.expireMills = milliSeconds > 0 ? milliSeconds : 0;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -214,6 +214,22 @@ public interface CacheSource extends Resourcable {
|
|||||||
setex(key, expireSeconds, Long.class, value);
|
setex(key, expireSeconds, Long.class, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
default <T> void setpx(String key, int milliSeconds, Convert convert, Type type, T value) {
|
||||||
|
setpxAsync(key, milliSeconds, convert, type, value).join();
|
||||||
|
}
|
||||||
|
|
||||||
|
default <T> void setpx(String key, int milliSeconds, Type type, T value) {
|
||||||
|
setpx(key, milliSeconds, (Convert) null, type, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
default void setpxString(String key, int milliSeconds, String value) {
|
||||||
|
setpx(key, milliSeconds, String.class, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
default void setpxLong(String key, int milliSeconds, long value) {
|
||||||
|
setpx(key, milliSeconds, Long.class, value);
|
||||||
|
}
|
||||||
|
|
||||||
//------------------------ setnxex ------------------------
|
//------------------------ setnxex ------------------------
|
||||||
default <T> boolean setnxex(String key, int expireSeconds, Convert convert, Type type, T value) {
|
default <T> boolean setnxex(String key, int expireSeconds, Convert convert, Type type, T value) {
|
||||||
return setnxexAsync(key, expireSeconds, convert, type, value).join();
|
return setnxexAsync(key, expireSeconds, convert, type, value).join();
|
||||||
@@ -231,6 +247,22 @@ public interface CacheSource extends Resourcable {
|
|||||||
return setnxex(key, expireSeconds, Long.class, value);
|
return setnxex(key, expireSeconds, Long.class, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
default <T> boolean setnxpx(String key, int milliSeconds, Convert convert, Type type, T value) {
|
||||||
|
return setnxpxAsync(key, milliSeconds, convert, type, value).join();
|
||||||
|
}
|
||||||
|
|
||||||
|
default <T> boolean setnxpx(String key, int milliSeconds, Type type, T value) {
|
||||||
|
return setnxpx(key, milliSeconds, (Convert) null, type, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
default boolean setnxpxString(String key, int milliSeconds, String value) {
|
||||||
|
return setnxpx(key, milliSeconds, String.class, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
default boolean setnxpxLong(String key, int milliSeconds, long value) {
|
||||||
|
return setnxpx(key, milliSeconds, Long.class, value);
|
||||||
|
}
|
||||||
|
|
||||||
//------------------------ get ------------------------
|
//------------------------ get ------------------------
|
||||||
default <T> T get(String key, Type type) {
|
default <T> T get(String key, Type type) {
|
||||||
return (T) getAsync(key, type).join();
|
return (T) getAsync(key, type).join();
|
||||||
@@ -345,6 +377,10 @@ public interface CacheSource extends Resourcable {
|
|||||||
expireAsync(key, expireSeconds).join();
|
expireAsync(key, expireSeconds).join();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
default void pexpire(String key, int milliSeconds) {
|
||||||
|
pexpireAsync(key, milliSeconds).join();
|
||||||
|
}
|
||||||
|
|
||||||
default List<String> keys(String pattern) {
|
default List<String> keys(String pattern) {
|
||||||
return keysAsync(pattern).join();
|
return keysAsync(pattern).join();
|
||||||
}
|
}
|
||||||
@@ -1101,6 +1137,20 @@ public interface CacheSource extends Resourcable {
|
|||||||
return setexAsync(key, expireSeconds, Long.class, value);
|
return setexAsync(key, expireSeconds, Long.class, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public <T> CompletableFuture<Void> setpxAsync(String key, int milliSeconds, Convert convert, Type type, T value);
|
||||||
|
|
||||||
|
default <T> CompletableFuture<Void> setpxAsync(String key, int milliSeconds, Type type, T value) {
|
||||||
|
return setpxAsync(key, milliSeconds, (Convert) null, type, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
default CompletableFuture<Void> setpxStringAsync(String key, int milliSeconds, String value) {
|
||||||
|
return setpxAsync(key, milliSeconds, String.class, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
default CompletableFuture<Void> setpxLongAsync(String key, int milliSeconds, long value) {
|
||||||
|
return setpxAsync(key, milliSeconds, Long.class, value);
|
||||||
|
}
|
||||||
|
|
||||||
//------------------------ setnxex ------------------------
|
//------------------------ setnxex ------------------------
|
||||||
public <T> CompletableFuture<Boolean> setnxexAsync(String key, int expireSeconds, Convert convert, Type type, T value);
|
public <T> CompletableFuture<Boolean> setnxexAsync(String key, int expireSeconds, Convert convert, Type type, T value);
|
||||||
|
|
||||||
@@ -1116,6 +1166,20 @@ public interface CacheSource extends Resourcable {
|
|||||||
return setnxexAsync(key, expireSeconds, Long.class, value);
|
return setnxexAsync(key, expireSeconds, Long.class, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public <T> CompletableFuture<Boolean> setnxpxAsync(String key, int milliSeconds, Convert convert, Type type, T value);
|
||||||
|
|
||||||
|
default <T> CompletableFuture<Boolean> setnxpxAsync(String key, int milliSeconds, Type type, T value) {
|
||||||
|
return setnxpxAsync(key, milliSeconds, (Convert) null, type, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
default CompletableFuture<Boolean> setnxpxStringAsync(String key, int milliSeconds, String value) {
|
||||||
|
return setnxpxAsync(key, milliSeconds, String.class, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
default CompletableFuture<Boolean> setnxpxLongAsync(String key, int milliSeconds, long value) {
|
||||||
|
return setnxpxAsync(key, milliSeconds, Long.class, value);
|
||||||
|
}
|
||||||
|
|
||||||
//------------------------ get ------------------------
|
//------------------------ get ------------------------
|
||||||
public <T> CompletableFuture<T> getAsync(String key, Type type);
|
public <T> CompletableFuture<T> getAsync(String key, Type type);
|
||||||
|
|
||||||
@@ -1219,6 +1283,8 @@ public interface CacheSource extends Resourcable {
|
|||||||
|
|
||||||
public CompletableFuture<Void> expireAsync(String key, int seconds);
|
public CompletableFuture<Void> expireAsync(String key, int seconds);
|
||||||
|
|
||||||
|
public CompletableFuture<Void> pexpireAsync(String key, int mills);
|
||||||
|
|
||||||
public CompletableFuture<List<String>> keysAsync(String pattern);
|
public CompletableFuture<List<String>> keysAsync(String pattern);
|
||||||
|
|
||||||
default CompletableFuture<List<String>> keysAsync() {
|
default CompletableFuture<List<String>> keysAsync() {
|
||||||
|
|||||||
Reference in New Issue
Block a user