From 7e0351cfc653f305ea83f35822a84daff8dd3a5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=81=E6=98=BE=E4=BC=98?= <237809796@qq.com> Date: Sat, 6 May 2023 19:03:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=EF=BC=9A=E5=A4=9A=E4=BD=99?= =?UTF-8?q?=E7=9A=84=20redis=20=E6=8F=92=E4=BB=B6=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lettuce/RedisLettuceCacheSource.java | 2670 ----------------- .../RedisLettuceCacheSourceProvider.java | 35 - .../redis/redission/RedissionCacheSource.java | 2226 -------------- .../RedissionCacheSourceProvider.java | 35 - .../redis/vertx/RedisVertxCacheSource.java | 1781 ----------- .../vertx/RedisVertxCacheSourceProvider.java | 32 - 6 files changed, 6779 deletions(-) delete mode 100644 src/org/redkalex/cache/redis/lettuce/RedisLettuceCacheSource.java delete mode 100644 src/org/redkalex/cache/redis/lettuce/RedisLettuceCacheSourceProvider.java delete mode 100644 src/org/redkalex/cache/redis/redission/RedissionCacheSource.java delete mode 100644 src/org/redkalex/cache/redis/redission/RedissionCacheSourceProvider.java delete mode 100644 src/org/redkalex/cache/redis/vertx/RedisVertxCacheSource.java delete mode 100644 src/org/redkalex/cache/redis/vertx/RedisVertxCacheSourceProvider.java diff --git a/src/org/redkalex/cache/redis/lettuce/RedisLettuceCacheSource.java b/src/org/redkalex/cache/redis/lettuce/RedisLettuceCacheSource.java deleted file mode 100644 index 43f2831..0000000 --- a/src/org/redkalex/cache/redis/lettuce/RedisLettuceCacheSource.java +++ /dev/null @@ -1,2670 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package org.redkalex.cache.redis.lettuce; - -import com.zdemo.cachex.AbstractRedisSource; -import com.zdemo.cachex.RedisCryptor; -import io.lettuce.core.*; -import io.lettuce.core.api.StatefulRedisConnection; -import io.lettuce.core.api.async.RedisAsyncCommands; -import io.lettuce.core.api.sync.RedisCommands; -import io.lettuce.core.cluster.RedisClusterClient; -import io.lettuce.core.cluster.api.StatefulRedisClusterConnection; -import io.lettuce.core.cluster.api.async.*; -import io.lettuce.core.cluster.api.sync.*; -import io.lettuce.core.codec.*; -import io.lettuce.core.support.*; -import org.redkale.annotation.AutoLoad; -import org.redkale.annotation.ResourceListener; -import org.redkale.annotation.ResourceType; -import org.redkale.convert.Convert; -import org.redkale.service.Local; -import org.redkale.source.CacheSource; -import org.redkale.util.AnyValue; -import org.redkale.util.RedkaleException; -import org.redkale.util.ResourceEvent; -import org.redkale.util.Utility; -import org.redkalex.cache.redis.AbstractRedisSource; - -import java.io.Serializable; -import java.lang.reflect.Type; -import java.net.URI; -import java.nio.charset.StandardCharsets; -import java.time.Duration; -import java.util.*; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CompletionStage; -import java.util.concurrent.locks.ReentrantLock; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * 注意: 目前Lettuce连接数过小时会出现连接池频频报连接耗尽的错误,推荐使用Redission实现方式。 - * - * @author zhangjx - */ -@Local -@AutoLoad(false) -@ResourceType(CacheSource.class) -public class RedisLettuceCacheSource extends AbstractRedisSource { - - protected final Logger logger = Logger.getLogger(this.getClass().getSimpleName()); - - protected Type objValueType = String.class; - - protected List nodeAddrs; - - protected io.lettuce.core.AbstractRedisClient client; - - protected BoundedAsyncPool> singleBytesConnPool; - - protected BoundedAsyncPool> singleStringConnPool; - - protected BoundedAsyncPool> clusterBytesConnPool; - - protected BoundedAsyncPool> clusterStringConnPool; - - protected RedisCodec stringByteArrayCodec; - - protected RedisCodec stringStringCodec; - - @Override - public void init(AnyValue conf) { - super.init(conf); - if (conf == null) { - conf = AnyValue.create(); - } - initClient(conf); - } - - private void initClient(AnyValue conf) { - this.stringByteArrayCodec = (RedisCodec) RedisCodec.of(StringCodec.UTF8, ByteArrayCodec.INSTANCE); - this.stringStringCodec = StringCodec.UTF8; - - final List addresses = new ArrayList<>(); - AnyValue[] nodes = getNodes(conf); - List uris = new ArrayList(nodes.length); - int urlmaxconns = Utility.cpus(); - String gdb = conf.getValue(CACHE_SOURCE_DB, "").trim(); - String gusername = conf.getValue(CACHE_SOURCE_USER, "").trim(); - String gpassword = conf.getValue(CACHE_SOURCE_PASSWORD, "").trim(); - for (AnyValue node : nodes) { - String addr = node.getValue(CACHE_SOURCE_URL, node.getValue("addr")); //兼容addr - addresses.add(addr); - String dbstr = node.getValue(CACHE_SOURCE_DB, "").trim(); - String username = node.getValue(CACHE_SOURCE_USER, "").trim(); - String password = node.getValue(CACHE_SOURCE_PASSWORD, "").trim(); - URI uri = URI.create(addr); - if (uri.getQuery() != null && !uri.getQuery().isEmpty()) { - String[] qrys = uri.getQuery().split("&|="); - for (int i = 0; i < qrys.length; i += 2) { - if (CACHE_SOURCE_MAXCONNS.equals(qrys[i])) { - urlmaxconns = i == qrys.length - 1 ? Utility.cpus() : Integer.parseInt(qrys[i + 1]); - } - } - } - RedisURI ruri = RedisURI.create(addr); - if (!dbstr.isEmpty()) { - db = Integer.parseInt(dbstr); - ruri.setDatabase(db); - } else if (!gdb.isEmpty()) { - ruri.setDatabase(Integer.parseInt(gdb)); - } -// if (!username.isEmpty()) { -// ruri.setUsername(username); -// } else if (!gusername.isEmpty()) { -// ruri.setUsername(gusername); -// } -// if (!password.isEmpty()) { -// ruri.setPassword(password.toCharArray()); -// } else if (!gpassword.isEmpty()) { -// ruri.setPassword(gpassword.toCharArray()); -// } - if (!username.isEmpty() || !gusername.isEmpty()) { - RedisCredentials authCredentials = RedisCredentials.just(!username.isEmpty() ? username : gusername, !password.isEmpty() ? password : (!gpassword.isEmpty() ? gpassword : "")); - ruri.setCredentialsProvider(RedisCredentialsProvider.from(() -> authCredentials)); - } - - uris.add(ruri); - } - final int maxconns = conf.getIntValue(CACHE_SOURCE_MAXCONNS, urlmaxconns); - final RedisURI singleRedisURI = uris.get(0); - io.lettuce.core.AbstractRedisClient old = this.client; - - RedisClient singleClient = null; - RedisClusterClient clusterClient = null; - if (uris.size() < 2) { - singleClient = io.lettuce.core.RedisClient.create(singleRedisURI); - this.client = singleClient; - } else { - clusterClient = RedisClusterClient.create(uris); - this.client = clusterClient; - } - this.nodeAddrs = addresses; - BoundedPoolConfig bpc = BoundedPoolConfig.builder().maxTotal(maxconns).maxIdle(maxconns).minIdle(0).build(); - if (clusterClient == null) { - RedisClient sClient = singleClient; - this.singleBytesConnPool = AsyncConnectionPoolSupport.createBoundedObjectPool(() -> sClient.connectAsync(stringByteArrayCodec, singleRedisURI), bpc); - this.singleStringConnPool = AsyncConnectionPoolSupport.createBoundedObjectPool(() -> sClient.connectAsync(stringStringCodec, singleRedisURI), bpc); - } else { - RedisClusterClient sClient = clusterClient; - this.clusterBytesConnPool = AsyncConnectionPoolSupport.createBoundedObjectPool(() -> sClient.connectAsync(stringByteArrayCodec), bpc); - this.clusterStringConnPool = AsyncConnectionPoolSupport.createBoundedObjectPool(() -> sClient.connectAsync(stringStringCodec), bpc); - } - if (old != null) { - old.close(); - } - //if (logger.isLoggable(Level.FINE)) logger.log(Level.FINE, RedisLettuceCacheSource.class.getSimpleName() + ": addrs=" + addresses); - } - - @Override - @ResourceListener - public void onResourceChange(ResourceEvent[] events) { - if (events == null || events.length < 1) { - return; - } - StringBuilder sb = new StringBuilder(); - for (ResourceEvent event : events) { - sb.append("CacheSource(name=").append(resourceName()).append(") change '").append(event.name()).append("' to '").append(event.coverNewValue()).append("'\r\n"); - } - initClient(this.config); - if (sb.length() > 0) { - logger.log(Level.INFO, sb.toString()); - } - } - - public boolean acceptsConf(AnyValue config) { - if (config == null) { - return false; - } - AnyValue[] nodes = getNodes(config); - if (nodes == null || nodes.length == 0) { - return false; - } - for (AnyValue node : nodes) { - String val = node.getValue(CACHE_SOURCE_URL, node.getValue("addr")); //兼容addr - if (val != null && val.startsWith("redis://")) { - return true; - } - if (val != null && val.startsWith("rediss://")) { - return true; - } - if (val != null && val.startsWith("redis-socket://")) { - return true; - } - if (val != null && val.startsWith("redis-sentinel://")) { - return true; - } - } - return false; - } - - protected AnyValue[] getNodes(AnyValue config) { - AnyValue[] nodes = config.getAnyValues(CACHE_SOURCE_NODE); - if (nodes == null || nodes.length == 0) { - AnyValue one = config.getAnyValue(CACHE_SOURCE_NODE); - if (one == null) { - String val = config.getValue(CACHE_SOURCE_URL); - if (val == null) { - return nodes; - } - nodes = new AnyValue[]{config}; - } else { - nodes = new AnyValue[]{one}; - } - } - return nodes; - } - - @Override - public final String getType() { - return "redis"; - } - - @Override - public String toString() { - return getClass().getSimpleName() + "{addrs=" + this.nodeAddrs + ", db=" + this.db + "}"; - } - - @Local - public io.lettuce.core.AbstractRedisClient getRedisClient() { - return this.client; - } - - protected List formatCollection(String key, RedisCryptor cryptor, List collection, Convert convert0, final Type componentType) { - List rs = new ArrayList<>(); - if (collection == null) { - return rs; - } - for (byte[] bs : collection) { - if (bs == null) { - continue; - } - rs.add((T) decryptValue(key, cryptor, convert0, componentType, bs)); - } - return rs; - } - - protected Set formatCollection(String key, RedisCryptor cryptor, Set collection, Convert convert0, final Type componentType) { - Set rs = new LinkedHashSet<>(); - if (collection == null) { - return rs; - } - for (byte[] bs : collection) { - if (bs == null) { - continue; - } - rs.add((T) decryptValue(key, cryptor, convert0, componentType, bs)); - } - return rs; - } - - protected Collection formatLongCollection(boolean set, Collection list) { - if (set) { - Set rs = new LinkedHashSet<>(); - for (String str : list) { - rs.add(Long.parseLong(str)); - } - return rs; - } else { - List rs = new ArrayList<>(); - for (String str : list) { - rs.add(Long.parseLong(str)); - } - return rs; - } - } - - protected Collection formatStringCollection(String key, RedisCryptor cryptor, boolean set, Collection list) { - if (cryptor == null) { - return list; - } - if (set) { - Set rs = new LinkedHashSet<>(); - for (String str : list) { - rs.add(cryptor.decrypt(key, str)); - } - return rs; - } else { - List rs = new ArrayList<>(); - for (String str : list) { - rs.add(cryptor.decrypt(key, str)); - } - return rs; - } - } - - @Override - public void destroy(AnyValue conf) { - super.destroy(conf); - if (client != null) { - client.shutdown(); - } - } - - protected CompletableFuture completableBoolFuture(RedisClusterAsyncCommands command, CompletionStage rf) { - return (CompletableFuture) rf.toCompletableFuture().whenComplete((v, e) -> releaseBytesCommand(command)); - } - - protected CompletableFuture completableBoolFuture(String key, RedisCryptor cryptor, RedisClusterAsyncCommands command, CompletionStage rf) { - return (CompletableFuture) rf.toCompletableFuture().whenComplete((v, e) -> releaseStringCommand(command)); - } - - protected CompletableFuture completableBytesFuture(RedisClusterAsyncCommands command, CompletionStage rf) { - return (CompletableFuture) rf.toCompletableFuture().whenComplete((v, e) -> releaseBytesCommand(command)); - } - - protected CompletableFuture completableStringFuture(String key, RedisCryptor cryptor, RedisClusterAsyncCommands command, CompletionStage rf) { - if (cryptor != null) { - return (CompletableFuture) rf.toCompletableFuture() - .thenApply(v -> v == null ? v : cryptor.decrypt(key, v.toString())) - .whenComplete((v, e) -> releaseStringCommand(command)); - } - return (CompletableFuture) rf.toCompletableFuture() - .whenComplete((v, e) -> releaseStringCommand(command)); - } - - protected CompletableFuture completableLongFuture(RedisClusterAsyncCommands command, CompletionStage rf) { - return (CompletableFuture) rf.toCompletableFuture() - .whenComplete((v, e) -> releaseStringCommand(command)); - } - - protected CompletableFuture> connectBytesAsync() { - if (clusterBytesConnPool == null) { - return singleBytesConnPool.acquire().thenApply(c -> c.async()); - } else { - return clusterBytesConnPool.acquire().thenApply(c -> c.async()); - } - } - - protected CompletableFuture> connectStringAsync() { - if (clusterBytesConnPool == null) { - return singleStringConnPool.acquire().thenApply(c -> c.async()); - } else { - return clusterStringConnPool.acquire().thenApply(c -> c.async()); - } - } - - protected RedisClusterCommands connectBytes() { - if (clusterBytesConnPool == null) { - return singleBytesConnPool.acquire().join().sync(); - } else { - return clusterBytesConnPool.acquire().join().sync(); - } - } - - protected RedisClusterCommands connectString() { - if (clusterStringConnPool == null) { - return singleStringConnPool.acquire().join().sync(); - } else { - return clusterStringConnPool.acquire().join().sync(); - } - } - - protected void releaseBytesCommand(RedisClusterCommands command) { - if (command instanceof RedisCommands) { - singleBytesConnPool.release(((RedisCommands) command).getStatefulConnection()).join(); - } else { - clusterBytesConnPool.release(((RedisAdvancedClusterCommands) command).getStatefulConnection()).join(); - } - } - - protected void releaseStringCommand(RedisClusterCommands command) { - if (command instanceof RedisCommands) { - singleStringConnPool.release(((RedisCommands) command).getStatefulConnection()).join(); - } else { - clusterStringConnPool.release(((RedisAdvancedClusterCommands) command).getStatefulConnection()).join(); - } - } - - protected void releaseBytesCommand(RedisClusterAsyncCommands command) { - if (command instanceof RedisAsyncCommands) { - singleBytesConnPool.release(((RedisAsyncCommands) command).getStatefulConnection()); - } else { - clusterBytesConnPool.release(((RedisAdvancedClusterAsyncCommands) command).getStatefulConnection()); - } - } - - protected void releaseStringCommand(RedisClusterAsyncCommands command) { - if (command instanceof RedisAsyncCommands) { - singleStringConnPool.release(((RedisAsyncCommands) command).getStatefulConnection()); - } else { - clusterStringConnPool.release(((RedisAdvancedClusterAsyncCommands) command).getStatefulConnection()); - } - } - - //--------------------- exists ------------------------------ - @Override - public CompletableFuture existsAsync(String key) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.exists(key).thenApply(v -> v > 0)); - }); - } - - @Override - public boolean exists(String key) { - RedisClusterCommands command = connectBytes(); - boolean rs = command.exists(key) > 0; - releaseBytesCommand(command); - return rs; - } - - //--------------------- get ------------------------------ - @Override - public CompletableFuture getAsync(String key, Type type) { - return connectBytesAsync().thenCompose(command -> { - CompletableFuture rf = completableBytesFuture(command, command.get(key)); - return rf.thenApply(bs -> decryptValue(key, cryptor, type, bs)); - }); - } - - @Override - public CompletableFuture getStringAsync(String key) { - return connectStringAsync().thenCompose(command -> { - return completableStringFuture(key, cryptor, command, command.get(key)); - }); - } - - @Override - public CompletableFuture getSetStringAsync(String key, String value) { - return connectStringAsync().thenCompose(command -> { - return completableStringFuture(key, cryptor, command, command.setGet(key, encryptValue(key, cryptor, value))); - }); - } - - @Override - public CompletableFuture getLongAsync(String key, long defValue) { - return connectStringAsync().thenCompose(command -> { - return completableLongFuture(command, command.get(key).thenApply(v -> v == null ? defValue : Long.parseLong(v))); - }); - } - - @Override - public CompletableFuture getSetLongAsync(String key, long value, long defValue) { - return connectStringAsync().thenCompose(command -> { - return completableLongFuture(command, command.setGet(key, String.valueOf(value)).thenApply(v -> v == null ? defValue : Long.parseLong(v))); - }); - } - - @Override - public T get(String key, final Type type) { - final RedisClusterCommands command = connectBytes(); - byte[] bs = command.get(key); - releaseBytesCommand(command); - return decryptValue(key, cryptor, type, bs); - } - - @Override - public T getSet(String key, final Type type, T value) { - final RedisClusterCommands command = connectBytes(); - byte[] bs = command.setGet(key, encryptValue(key, cryptor, type, convert, value)); - releaseBytesCommand(command); - return decryptValue(key, cryptor, type, bs); - } - - @Override - public T getSet(String key, Convert convert0, final Type type, T value) { - Convert c = convert0 == null ? this.convert : convert0; - final RedisClusterCommands command = connectBytes(); - byte[] bs = command.setGet(key, encryptValue(key, cryptor, type, c, value)); - releaseBytesCommand(command); - return decryptValue(key, cryptor, c, type, bs); - } - - @Override - public String getString(String key) { - final RedisClusterCommands command = connectString(); - String rs = command.get(key); - releaseStringCommand(command); - return cryptor != null ? cryptor.decrypt(key, rs) : rs; - } - - @Override - public String getSetString(String key, String value) { - final RedisClusterCommands command = connectString(); - String rs = command.setGet(key, encryptValue(key, cryptor, value)); - releaseStringCommand(command); - return decryptValue(key, cryptor, rs); - } - - @Override - public long getLong(String key, long defValue) { - final RedisClusterCommands command = connectString(); - final String v = command.get(key); - releaseStringCommand(command); - return v == null ? defValue : Long.parseLong(v); - } - - @Override - public long getSetLong(String key, long value, long defValue) { - final RedisClusterCommands command = connectString(); - final String v = command.setGet(key, String.valueOf(value)); - releaseStringCommand(command); - return v == null ? defValue : Long.parseLong(v); - } - - //--------------------- getex ------------------------------ - @Override - public CompletableFuture getexAsync(String key, int expireSeconds, final Type type) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.getex(key, GetExArgs.Builder.ex(expireSeconds)).thenApply(v -> decryptValue(key, cryptor, type, v))); - }); - } - - @Override - public T getex(String key, final int expireSeconds, final Type type) { - final RedisClusterCommands command = connectBytes(); - byte[] bs = command.getex(key, GetExArgs.Builder.ex(expireSeconds)); - releaseBytesCommand(command); - if (bs == null) { - return null; - } - return decryptValue(key, cryptor, type, bs); - } - - @Override - public CompletableFuture getexStringAsync(String key, int expireSeconds) { - return connectStringAsync().thenCompose(command -> { - return completableStringFuture(key, null, command, command.getex(key, GetExArgs.Builder.ex(expireSeconds)).thenApply(v -> cryptor != null ? cryptor.decrypt(key, v) : v)); - }); - } - - @Override - public String getexString(String key, final int expireSeconds) { - final RedisClusterCommands command = connectString(); - String v = command.getex(key, GetExArgs.Builder.ex(expireSeconds)); - releaseStringCommand(command); - if (v == null) { - return null; - } - return cryptor != null ? cryptor.decrypt(key, v) : v; - } - - @Override - public CompletableFuture getexLongAsync(String key, int expireSeconds, long defValue) { - return connectStringAsync().thenCompose(command -> { - //此处获取的long值,无需传cryptor进行解密 - return completableLongFuture(command, command.getex(key, GetExArgs.Builder.ex(expireSeconds)).thenApply(v -> v == null ? defValue : Long.parseLong(v))); - }); - } - - @Override - public long getexLong(String key, final int expireSeconds, long defValue) { - final RedisClusterCommands command = connectString(); - String v = command.getex(key, GetExArgs.Builder.ex(expireSeconds)); - releaseStringCommand(command); - if (v == null) { - return defValue; - } - return Long.parseLong(v); - } - -// //--------------------- setex ------------------------------ - @Override - public CompletableFuture msetAsync(final Object... keyVals) { - if (keyVals.length % 2 != 0) { - throw new RedkaleException("key value must be paired"); - } - Map map = new LinkedHashMap<>(); - for (int i = 0; i < keyVals.length; i += 2) { - String key = keyVals[i].toString(); - Object val = keyVals[i + 1]; - map.put(key, encryptValue(key, cryptor, convert, val)); - } - return connectBytesAsync().thenCompose(command -> completableBytesFuture(command, command.mset(map))); - } - - @Override - public CompletableFuture msetAsync(final Map map) { - if (map == null || map.isEmpty()) { - return CompletableFuture.completedFuture(null); - } - Map rs = new LinkedHashMap<>(); - map.forEach((key, val) -> { - rs.put(key.toString(), encryptValue(key.toString(), cryptor, convert, val)); - }); - return connectBytesAsync().thenCompose(command -> completableBytesFuture(command, command.mset(rs))); - } - - @Override - public CompletableFuture setAsync(String key, final Type type, T value) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.set(key, encryptValue(key, cryptor, type, this.convert, value))); - }); - } - - @Override - public CompletableFuture setAsync(String key, Convert convert0, final Type type, T value) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.set(key, encryptValue(key, cryptor, type, convert0, value))); - }); - } - - @Override - public CompletableFuture setnxAsync(String key, final Type type, T value) { - return connectBytesAsync().thenCompose(command -> { - return completableBoolFuture(command, command.setnx(key, encryptValue(key, cryptor, type, this.convert, value))); - }); - } - - @Override - public CompletableFuture setnxAsync(String key, Convert convert0, final Type type, T value) { - return connectBytesAsync().thenCompose(command -> { - return completableBoolFuture(command, command.setnx(key, encryptValue(key, cryptor, type, convert0, value))); - }); - } - - @Override - public CompletableFuture getSetAsync(String key, final Type type, T value) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.setGet(key, encryptValue(key, cryptor, type, this.convert, value)) - .thenApply(old -> decryptValue(key, cryptor, type, old))); - }); - } - - @Override - public CompletableFuture getSetAsync(String key, Convert convert0, final Type type, T value) { - return connectBytesAsync().thenCompose(command -> { - Convert c = convert0 == null ? this.convert : convert0; - return completableBytesFuture(command, command.setGet(key, encryptValue(key, cryptor, type, c, value)) - .thenApply(old -> decryptValue(key, cryptor, c, type, old))); - }); - } - - @Override - public void mset(final Object... keyVals) { - if (keyVals.length % 2 != 0) { - throw new RedkaleException("key value must be paired"); - } - Map map = new LinkedHashMap<>(); - for (int i = 0; i < keyVals.length; i += 2) { - String key = keyVals[i].toString(); - Object val = keyVals[i + 1]; - map.put(key, encryptValue(key, cryptor, convert, val)); - } - final RedisClusterCommands command = connectBytes(); - command.mset(map); - releaseBytesCommand(command); - } - - @Override - public void mset(final Map map) { - if (map == null || map.isEmpty()) { - return; - } - Map rs = new LinkedHashMap<>(); - map.forEach((key, val) -> { - rs.put(key.toString(), encryptValue(key.toString(), cryptor, convert, val)); - }); - final RedisClusterCommands command = connectBytes(); - command.mset(rs); - releaseBytesCommand(command); - } - - @Override - public void set(final String key, final Type type, T value) { - final RedisClusterCommands command = connectBytes(); - command.set(key, encryptValue(key, cryptor, type, this.convert, value)); - releaseBytesCommand(command); - } - - @Override - public void set(String key, final Convert convert0, final Type type, T value) { - final RedisClusterCommands command = connectBytes(); - command.set(key, encryptValue(key, cryptor, type, convert0, value)); - releaseBytesCommand(command); - } - - @Override - public boolean setnx(final String key, final Type type, T value) { - final RedisClusterCommands command = connectBytes(); - Boolean rs = command.setnx(key, encryptValue(key, cryptor, type, this.convert, value)); - releaseBytesCommand(command); - return rs; - } - - @Override - public boolean setnx(String key, final Convert convert0, final Type type, T value) { - final RedisClusterCommands command = connectBytes(); - Boolean rs = command.setnx(key, encryptValue(key, cryptor, type, convert0, value)); - releaseBytesCommand(command); - return rs; - } - - @Override - public CompletableFuture setStringAsync(String key, String value) { - return connectStringAsync().thenCompose(command -> { - //不处理返回值,无需传cryptor进行解密 - return completableStringFuture(key, null, command, command.set(key, encryptValue(key, cryptor, value))); - }); - } - - @Override - public CompletableFuture setnxStringAsync(String key, String value) { - return connectStringAsync().thenCompose(command -> { - //不处理返回值,无需传cryptor进行解密 - return completableBoolFuture(key, null, command, command.setnx(key, encryptValue(key, cryptor, value))); - }); - } - - @Override - public void setString(String key, String value) { - final RedisClusterCommands command = connectString(); - command.set(key, encryptValue(key, cryptor, value)); - releaseStringCommand(command); - } - - @Override - public boolean setnxString(String key, String value) { - final RedisClusterCommands command = connectString(); - Boolean rs = command.setnx(key, encryptValue(key, cryptor, value)); - releaseStringCommand(command); - return rs; - } - - @Override - public CompletableFuture setLongAsync(String key, long value) { - return connectStringAsync().thenCompose(command -> { - //不处理返回值,无需传cryptor进行解密 - return completableStringFuture(key, null, command, command.set(key, String.valueOf(value))); - }); - } - - @Override - public CompletableFuture setnxLongAsync(String key, long value) { - return connectStringAsync().thenCompose(command -> { - //不处理返回值,无需传cryptor进行解密 - return completableStringFuture(key, null, command, command.setnx(key, String.valueOf(value))); - }); - } - - @Override - public void setLong(String key, long value) { - final RedisClusterCommands command = connectString(); - command.set(key, String.valueOf(value)); - releaseStringCommand(command); - } - - @Override - public boolean setnxLong(String key, long value) { - final RedisClusterCommands command = connectString(); - Boolean rs = command.setnx(key, String.valueOf(value)); - releaseStringCommand(command); - return rs; - } - - @Override - public boolean setnxBytes(final String key, final byte[] value) { - final RedisClusterCommands command = connectBytes(); - Boolean rs = command.setnx(key, value); - releaseBytesCommand(command); - return rs; - } - - @Override - public CompletableFuture setnxBytesAsync(String key, byte[] value) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.setnx(key, value)); - }); - } - -// //--------------------- setex ------------------------------ - @Override - public CompletableFuture setexAsync(String key, int expireSeconds, final Type type, T value) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.setex(key, expireSeconds, encryptValue(key, cryptor, type, convert, value)).thenApply(r -> null)); - - }); - } - - @Override - public CompletableFuture setnxexAsync(String key, int expireSeconds, final Type type, T value) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.set(key, encryptValue(key, cryptor, type, convert, value), SetArgs.Builder.nx().ex(expireSeconds)).thenApply(r -> r != null && ("OK".equals(r) || Integer.parseInt(r) > 0))); - }); - } - - @Override - public CompletableFuture setexAsync(String key, int expireSeconds, Convert convert0, final Type type, T value) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.setex(key, expireSeconds, encryptValue(key, cryptor, type, convert0, value)).thenApply(r -> null)); - }); - } - - @Override - public CompletableFuture setnxexAsync(String key, int expireSeconds, Convert convert0, final Type type, T value) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.set(key, encryptValue(key, cryptor, type, convert0, value), SetArgs.Builder.nx().ex(expireSeconds)).thenApply(r -> r != null && ("OK".equals(r) || Integer.parseInt(r) > 0))); - }); - } - - @Override - public boolean setnxex(String key, int expireSeconds, final Type type, T value) { - final RedisClusterCommands command = connectBytes(); - String r = command.set(key, encryptValue(key, cryptor, type, convert, value), SetArgs.Builder.nx().ex(expireSeconds)); - releaseBytesCommand(command); - return r != null && ("OK".equals(r) || Integer.parseInt(r) > 0); - } - - @Override - public boolean setnxex(String key, int expireSeconds, Convert convert0, final Type type, T value) { - final RedisClusterCommands command = connectBytes(); - String r = command.set(key, encryptValue(key, cryptor, type, convert0, value), SetArgs.Builder.nx().ex(expireSeconds)); - releaseBytesCommand(command); - return r != null && ("OK".equals(r) || Integer.parseInt(r) > 0); - } - - @Override - public void setex(String key, int expireSeconds, final Type type, T value) { - final RedisClusterCommands command = connectBytes(); - command.setex(key, expireSeconds, encryptValue(key, cryptor, type, convert, value)); - releaseBytesCommand(command); - } - - @Override - public void setex(String key, int expireSeconds, Convert convert0, final Type type, T value) { - final RedisClusterCommands command = connectBytes(); - command.setex(key, expireSeconds, encryptValue(key, cryptor, type, convert0, value)); - releaseBytesCommand(command); - } - - @Override - public CompletableFuture setexStringAsync(String key, int expireSeconds, String value) { - return connectStringAsync().thenCompose(command -> { - //不处理返回值,无需传cryptor进行解密 - return completableStringFuture(key, null, command, command.setex(key, expireSeconds, encryptValue(key, cryptor, value)).thenApply(r -> null)); - }); - } - - @Override - public CompletableFuture setnxexStringAsync(String key, int expireSeconds, String value) { - return connectStringAsync().thenCompose(command -> { - return completableStringFuture(key, null, command, command.set(key, encryptValue(key, cryptor, value), SetArgs.Builder.nx().ex(expireSeconds)).thenApply(r -> r != null && ("OK".equals(r) || Integer.parseInt(r) > 0))); - }); - } - - @Override - public void setexString(String key, int expireSeconds, String value) { - final RedisClusterCommands command = connectString(); - command.setex(key, expireSeconds, encryptValue(key, cryptor, value)); - releaseStringCommand(command); - } - - @Override - public boolean setnxexString(String key, int expireSeconds, String value) { - final RedisClusterCommands command = connectString(); - String r = command.set(key, encryptValue(key, cryptor, value), SetArgs.Builder.nx().ex(expireSeconds)); - releaseStringCommand(command); - return r != null && ("OK".equals(r) || Integer.parseInt(r) > 0); - } - - @Override - public CompletableFuture setexLongAsync(String key, int expireSeconds, long value) { - return connectStringAsync().thenCompose(command -> { - //不处理返回值,无需传cryptor进行解密 - return completableStringFuture(key, null, command, command.setex(key, expireSeconds, String.valueOf(value)).thenApply(r -> null)); - }); - } - - @Override - public CompletableFuture setnxexLongAsync(String key, int expireSeconds, long value) { - return connectStringAsync().thenCompose(command -> { - return completableStringFuture(key, null, command, command.set(key, String.valueOf(value), SetArgs.Builder.nx().ex(expireSeconds)).thenApply(r -> r != null && ("OK".equals(r) || Integer.parseInt(r) > 0))); - }); - } - - @Override - public void setexLong(String key, int expireSeconds, long value) { - final RedisClusterCommands command = connectString(); - command.setex(key, expireSeconds, String.valueOf(value)); - releaseStringCommand(command); - } - - @Override - public boolean setnxexLong(String key, int expireSeconds, long value) { - final RedisClusterCommands command = connectString(); - String r = command.set(key, String.valueOf(value), SetArgs.Builder.nx().ex(expireSeconds)); - releaseStringCommand(command); - return r != null && ("OK".equals(r) || Integer.parseInt(r) > 0); - } - -// //--------------------- expire ------------------------------ - @Override - public CompletableFuture expireAsync(String key, int expireSeconds) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.expire(key, Duration.ofSeconds(expireSeconds)).thenApply(r -> null)); - }); - } - - @Override - public void expire(String key, int expireSeconds) { - final RedisClusterCommands command = connectBytes(); - command.expire(key, Duration.ofSeconds(expireSeconds)); - releaseBytesCommand(command); - } - -// //--------------------- del ------------------------------ - @Override - public CompletableFuture delAsync(String... keys) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.del(keys).thenApply(rs -> rs > 0 ? 1 : 0)); - }); - } - - @Override - public int del(String... keys) { - final RedisClusterCommands command = connectBytes(); - int rs = command.del(keys).intValue(); - releaseBytesCommand(command); - return rs; - } - -// //--------------------- incrby ------------------------------ - @Override - public long incr(final String key) { - final RedisClusterCommands command = connectBytes(); - long rs = command.incr(key); - releaseBytesCommand(command); - return rs; - } - - @Override - public CompletableFuture incrAsync(final String key) { - return connectStringAsync().thenCompose(command -> { - //此处获取的long值,无需传cryptor进行解密 - return completableStringFuture(key, null, command, command.incr(key)); - }); - } - - @Override - public long incrby(final String key, long num) { - final RedisClusterCommands command = connectBytes(); - long rs = command.incrby(key, num); - releaseBytesCommand(command); - return rs; - } - - @Override - public double incrbyFloat(final String key, double num) { - final RedisClusterCommands command = connectBytes(); - Double rs = command.incrbyfloat(key, num); - releaseBytesCommand(command); - return rs; - } - - @Override - public CompletableFuture incrbyAsync(final String key, long num) { - return connectStringAsync().thenCompose(command -> { - //此处获取的long值,无需传cryptor进行解密 - return completableStringFuture(key, null, command, command.incrby(key, num)); - }); - } - - @Override - public CompletableFuture incrbyFloatAsync(final String key, double num) { - return connectStringAsync().thenCompose(command -> { - //此处获取的long值,无需传cryptor进行解密 - return completableStringFuture(key, null, command, command.incrbyfloat(key, num)); - }); - } -// //--------------------- decrby ------------------------------ - - @Override - public long decr(final String key) { - final RedisClusterCommands command = connectBytes(); - long rs = command.decr(key); - releaseBytesCommand(command); - return rs; - } - - @Override - public CompletableFuture decrAsync(final String key) { - return connectStringAsync().thenCompose(command -> { - //此处获取的long值,无需传cryptor进行解密 - return completableStringFuture(key, null, command, command.decr(key)); - }); - } - - @Override - public long decrby(final String key, long num) { - final RedisClusterCommands command = connectBytes(); - long rs = command.decrby(key, num); - releaseBytesCommand(command); - return rs; - } - - @Override - public CompletableFuture decrbyAsync(final String key, long num) { - return connectStringAsync().thenCompose(command -> { - //此处获取的long值,无需传cryptor进行解密 - return completableStringFuture(key, null, command, command.decrby(key, num)); - }); - } - - @Override - public int hdel(final String key, String... fields) { - final RedisClusterCommands command = connectBytes(); - int rs = command.hdel(key, fields).intValue(); - releaseBytesCommand(command); - return rs; - } - - @Override - public int hlen(final String key) { - final RedisClusterCommands command = connectBytes(); - int rs = command.hlen(key).intValue(); - releaseBytesCommand(command); - return rs; - } - - @Override - public List hkeys(final String key) { - final RedisClusterCommands command = connectString(); - List rs = command.hkeys(key); - releaseStringCommand(command); - return rs; - } - - @Override - public long hincr(final String key, String field) { - final RedisClusterCommands command = connectString(); - long rs = command.hincrby(key, field, 1L); - releaseStringCommand(command); - return rs; - } - - @Override - public long hincrby(final String key, String field, long num) { - final RedisClusterCommands command = connectString(); - long rs = command.hincrby(key, field, num); - releaseStringCommand(command); - return rs; - } - - @Override - public double hincrbyFloat(final String key, String field, double num) { - final RedisClusterCommands command = connectString(); - double rs = command.hincrbyfloat(key, field, num); - releaseStringCommand(command); - return rs; - } - - @Override - public long hdecr(final String key, String field) { - final RedisClusterCommands command = connectString(); - long rs = command.hincrby(key, field, -1L); - releaseStringCommand(command); - return rs; - } - - @Override - public long hdecrby(final String key, String field, long num) { - final RedisClusterCommands command = connectString(); - long rs = command.hincrby(key, field, -num); - releaseStringCommand(command); - return rs; - } - - @Override - public boolean hexists(final String key, String field) { - final RedisClusterCommands command = connectBytes(); - boolean rs = command.hget(key, field) != null; - releaseBytesCommand(command); - return rs; - } - -// //--------------------- dbsize ------------------------------ - @Override - public long dbsize() { - final RedisClusterCommands command = connectBytes(); - Long rs = command.dbsize(); - releaseBytesCommand(command); - return rs; - } - - @Override - public CompletableFuture dbsizeAsync() { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.dbsize()); - }); - } - - @Override - public void hset(final String key, final String field, final Type type, final T value) { - if (value == null) { - return; - } - final RedisClusterCommands command = connectBytes(); - command.hset(key, field, encryptValue(key, cryptor, type, this.convert, value)); - releaseBytesCommand(command); - } - - @Override - public void hset(final String key, final String field, final Convert convert0, final Type type, final T value) { - if (value == null) { - return; - } - final RedisClusterCommands command = connectBytes(); - command.hset(key, field, encryptValue(key, cryptor, type, convert0, value)); - releaseBytesCommand(command); - } - - @Override - public void hsetString(final String key, final String field, final String value) { - if (value == null) { - return; - } - final RedisClusterCommands command = connectString(); - command.hset(key, field, encryptValue(key, cryptor, value)); - releaseStringCommand(command); - } - - @Override - public void hsetLong(final String key, final String field, final long value) { - final RedisClusterCommands command = connectString(); - command.hset(key, field, String.valueOf(value)); - releaseStringCommand(command); - } - - @Override - public boolean hsetnx(final String key, final String field, final Type type, final T value) { - if (value == null) { - return false; - } - final RedisClusterCommands command = connectBytes(); - Boolean rs = command.hsetnx(key, field, encryptValue(key, cryptor, type, this.convert, value)); - releaseBytesCommand(command); - return rs; - } - - @Override - public boolean hsetnx(final String key, final String field, final Convert convert0, final Type type, final T value) { - if (value == null) { - return false; - } - final RedisClusterCommands command = connectBytes(); - Boolean rs = command.hsetnx(key, field, encryptValue(key, cryptor, type, convert0, value)); - releaseBytesCommand(command); - return rs; - } - - @Override - public boolean hsetnxString(final String key, final String field, final String value) { - if (value == null) { - return false; - } - final RedisClusterCommands command = connectString(); - Boolean rs = command.hsetnx(key, field, encryptValue(key, cryptor, value)); - releaseStringCommand(command); - return rs; - } - - @Override - public boolean hsetnxLong(final String key, final String field, final long value) { - final RedisClusterCommands command = connectString(); - Boolean rs = command.hsetnx(key, field, String.valueOf(value)); - releaseStringCommand(command); - return rs; - } - - @Override - public void hmset(final String key, final Serializable... values) { - Map vals = new LinkedHashMap<>(); - for (int i = 0; i < values.length; i += 2) { - byte[] bs; - if (cryptor != null && values[i + 1] != null) { - bs = values[i + 1] instanceof String ? cryptor.encrypt(key, values[i + 1].toString()).getBytes(StandardCharsets.UTF_8) : encryptValue(key, cryptor, values[i + 1].getClass(), this.convert, values[i + 1]); - } else { - bs = values[i + 1] instanceof String ? values[i + 1].toString().getBytes(StandardCharsets.UTF_8) : this.convert.convertToBytes(values[i + 1]); - } - vals.put(String.valueOf(values[i]), bs); - } - final RedisClusterCommands command = connectBytes(); - command.hmset(key, vals); - releaseBytesCommand(command); - } - - @Override - public void hmset(final String key, final Map map) { - Map vals = new LinkedHashMap<>(); - map.forEach((k, v) -> { - byte[] bs; - if (cryptor != null && v != null) { - bs = v instanceof String ? cryptor.encrypt(key, v.toString()).getBytes(StandardCharsets.UTF_8) : encryptValue(key, cryptor, v.getClass(), this.convert, v); - } else { - bs = v instanceof String ? v.toString().getBytes(StandardCharsets.UTF_8) : this.convert.convertToBytes(v); - } - vals.put(k.toString(), bs); - }); - final RedisClusterCommands command = connectBytes(); - command.hmset(key, vals); - releaseBytesCommand(command); - } - - @Override - public List hmget(final String key, final Type type, final String... fields) { - final RedisClusterCommands command = connectBytes(); - List> rs = command.hmget(key, fields); - releaseBytesCommand(command); - List list = new ArrayList<>(fields.length); - for (String field : fields) { - byte[] bs = null; - for (KeyValue kv : rs) { - if (kv.getKey().equals(field)) { - bs = kv.hasValue() ? kv.getValue() : null; - break; - } - } - if (bs == null) { - list.add(null); - } else { - list.add(decryptValue(key, cryptor, type, bs)); - } - } - return list; - } - - @Override - public Map hmap(final String key, final Type type, int offset, int limit, String pattern) { - final RedisClusterCommands command = connectBytes(); - ScanArgs args = ScanArgs.Builder.limit(limit); - if (pattern != null) { - args = args.match(pattern); - } - MapScanCursor rs = command.hscan(key, new ScanCursor(String.valueOf(offset), false), args); - releaseBytesCommand(command); - final Map map = new LinkedHashMap<>(); - rs.getMap().forEach((k, v) -> map.put(k, decryptValue(key, cryptor, type, v))); - return map; - } - - @Override - public Map hmap(final String key, final Type type, int offset, int limit) { - return hmap(key, type, offset, limit, null); - } - - @Override - public T hget(final String key, final String field, final Type type) { - final RedisClusterCommands command = connectBytes(); - byte[] bs = command.hget(key, field); - releaseBytesCommand(command); - return decryptValue(key, cryptor, type, bs); - } - - @Override - public String hgetString(final String key, final String field) { - final RedisClusterCommands command = connectString(); - String rs = command.hget(key, field); - releaseStringCommand(command); - return cryptor != null ? cryptor.decrypt(key, rs) : rs; - } - - @Override - public long hgetLong(final String key, final String field, long defValue) { - final RedisClusterCommands command = connectString(); - String rs = command.hget(key, field); - releaseStringCommand(command); - if (rs == null) { - return defValue; - } - try { - return Long.parseLong(rs); - } catch (NumberFormatException e) { - return defValue; - } - } - - @Override - public Map mget(final Type componentType, final String... keys) { - final RedisClusterCommands command = connectBytes(); - List> rs = command.mget(keys); - releaseBytesCommand(command); - Map map = new LinkedHashMap(rs.size()); - rs.forEach(kv -> { - if (kv.hasValue()) { - map.put(kv.getKey(), decryptValue(kv.getKey(), cryptor, componentType, kv.getValue())); - } - }); - return map; - } - - @Override - public Map mgetBytes(final String... keys) { - final RedisClusterCommands command = connectBytes(); - List> rs = command.mget(keys); - releaseBytesCommand(command); - Map map = new LinkedHashMap(rs.size()); - rs.forEach(kv -> { - if (kv.hasValue()) { - map.put(kv.getKey(), decryptValue(kv.getKey(), cryptor, byte[].class, kv.getValue())); - } - }); - return map; - } - - @Override - public Set smembers(String key, final Type componentType) { - final RedisClusterCommands command = connectBytes(); - Set rs = formatCollection(key, cryptor, command.smembers(key), convert, componentType); - releaseBytesCommand(command); - return rs; - } - - @Override - public List lrange(String key, final Type componentType) { - final RedisClusterCommands command = connectBytes(); - List rs = formatCollection(key, cryptor, command.lrange(key, 0, -1), convert, componentType); - releaseBytesCommand(command); - return rs; - } - - protected Collection getCollection(final RedisClusterCommands command, String key, final Type componentType) { - final String type = command.type(key); - if (type.contains("list")) { - return formatCollection(key, cryptor, command.lrange(key, 0, -1), convert, componentType); - } else { //set - return formatCollection(key, cryptor, command.smembers(key), convert, componentType); - } - } - - @Override - public Map> smembers(final Type componentType, String... keys) { - final RedisClusterCommands command = connectBytes(); - final Map> map = new LinkedHashMap<>(); - for (String key : keys) { - map.put(key, formatCollection(key, cryptor, command.smembers(key), convert, componentType)); - } - releaseBytesCommand(command); - return map; - } - - @Override - public Map> lrange(final Type componentType, String... keys) { - final RedisClusterCommands command = connectBytes(); - final Map> map = new LinkedHashMap<>(); - for (String key : keys) { - map.put(key, formatCollection(key, cryptor, command.lrange(key, 0, -1), convert, componentType)); - } - releaseBytesCommand(command); - return map; - } - - @Override - public int llen(String key) { - final RedisClusterCommands command = connectBytes(); - int rs = command.llen(key).intValue(); - releaseBytesCommand(command); - return rs; - } - - @Override - public int scard(String key) { - final RedisClusterCommands command = connectBytes(); - int rs = command.scard(key).intValue(); - releaseBytesCommand(command); - return rs; - } - - @Override - @SuppressWarnings({"ConfusingArrayVararg", "PrimitiveArrayArgumentToVariableArgMethod"}) - public void rpush(String key, final Type componentType, T value) { - final RedisClusterCommands command = connectBytes(); - byte[] bs; - if (cryptor != null && componentType == String.class) { - bs = cryptor.encrypt(key, String.valueOf(value)).getBytes(StandardCharsets.UTF_8); - } else { - bs = encryptValue(key, cryptor, componentType, convert, value); - } - command.rpush(key, bs); - releaseBytesCommand(command); - } - - @Override - public int lrem(String key, final Type componentType, T value) { - final RedisClusterCommands command = connectBytes(); - byte[] bs; - if (cryptor != null && componentType == String.class) { - bs = cryptor.encrypt(key, String.valueOf(value)).getBytes(StandardCharsets.UTF_8); - } else { - bs = encryptValue(key, cryptor, componentType, convert, value); - } - int rs = command.lrem(key, 1L, bs).intValue(); - releaseBytesCommand(command); - return rs; - } - - @Override - public boolean sismember(String key, final Type componentType, T value) { - final RedisClusterCommands command = connectBytes(); - byte[] bs; - if (cryptor != null && componentType == String.class) { - bs = cryptor.encrypt(key, String.valueOf(value)).getBytes(StandardCharsets.UTF_8); - } else { - bs = encryptValue(key, cryptor, componentType, convert, value); - } - boolean rs = command.sismember(key, bs); - releaseBytesCommand(command); - return rs; - } - - @Override - @SuppressWarnings({"ConfusingArrayVararg", "PrimitiveArrayArgumentToVariableArgMethod"}) - public void sadd(String key, final Type componentType, T value) { - final RedisClusterCommands command = connectBytes(); - byte[] bs; - if (cryptor != null && componentType == String.class) { - bs = cryptor.encrypt(key, String.valueOf(value)).getBytes(StandardCharsets.UTF_8); - } else { - bs = encryptValue(key, cryptor, componentType, convert, value); - } - command.sadd(key, bs); - releaseBytesCommand(command); - } - - @Override - @SuppressWarnings({"ConfusingArrayVararg", "PrimitiveArrayArgumentToVariableArgMethod"}) - public int srem(String key, final Type componentType, T value) { - final RedisClusterCommands command = connectBytes(); - byte[] bs; - if (cryptor != null && componentType == String.class) { - bs = cryptor.encrypt(key, String.valueOf(value)).getBytes(StandardCharsets.UTF_8); - } else { - bs = encryptValue(key, cryptor, componentType, convert, value); - } - int rs = command.srem(key, bs).intValue(); - releaseBytesCommand(command); - return rs; - } - - @Override - public T spop(String key, final Type componentType) { - final RedisClusterCommands command = connectBytes(); - T rs = decryptValue(key, cryptor, componentType, command.spop(key)); - releaseBytesCommand(command); - return rs; - } - - @Override - public Set spop(String key, int count, final Type componentType) { - final RedisClusterCommands command = connectBytes(); - Set rs = formatCollection(key, cryptor, command.spop(key, count), convert, componentType); - releaseBytesCommand(command); - return rs; - } - - @Override - public byte[] getBytes(final String key) { - final RedisClusterCommands command = connectBytes(); - byte[] bs = command.get(key); - releaseBytesCommand(command); - return bs; - } - - @Override - public byte[] getSetBytes(final String key, byte[] value) { - final RedisClusterCommands command = connectBytes(); - byte[] bs = command.setGet(key, value); - releaseBytesCommand(command); - return bs; - } - - @Override - public byte[] getexBytes(final String key, final int expireSeconds) { - final RedisClusterCommands command = connectBytes(); - byte[] bs = command.getex(key, GetExArgs.Builder.ex(expireSeconds)); - releaseBytesCommand(command); - return bs; - } - - @Override - public void setBytes(final String key, final byte[] value) { - final RedisClusterCommands command = connectBytes(); - command.set(key, value); - releaseBytesCommand(command); - } - - @Override - public void setexBytes(final String key, final int expireSeconds, final byte[] value) { - final RedisClusterCommands command = connectBytes(); - command.setex(key, expireSeconds, value); - releaseBytesCommand(command); - } - - @Override - public boolean setnxexBytes(final String key, final int expireSeconds, final byte[] value) { - final RedisClusterCommands command = connectBytes(); - String r = command.set(key, value, SetArgs.Builder.nx().ex(expireSeconds)); - releaseBytesCommand(command); - return r != null && ("OK".equals(r) || Integer.parseInt(r) > 0); - } - - @Override - public List keys(String pattern) { - final RedisClusterCommands command = connectBytes(); - List rs = command.keys(pattern == null || pattern.isEmpty() ? "*" : pattern); - releaseBytesCommand(command); - return rs; - } - - @Override - public Map mgetString(final String... keys) { - final RedisClusterCommands command = connectString(); - List> rs = command.mget(keys); - releaseStringCommand(command); - Map map = new LinkedHashMap<>(); - rs.forEach(kv -> { - if (kv.hasValue()) { - map.put(kv.getKey(), cryptor != null ? cryptor.decrypt(kv.getKey(), kv.getValue()) : kv.getValue()); - } - }); - return map; - } - - protected Collection decryptStringCollection(String key, final boolean set, Collection collection) { - if (collection == null || collection.isEmpty() || cryptor == null) { - return collection; - } - if (set) { - Set newset = new LinkedHashSet<>(); - for (String value : collection) { - newset.add(cryptor.decrypt(key, value)); - } - return newset; - } else { - List newlist = new ArrayList<>(); - for (String value : collection) { - newlist.add(cryptor.decrypt(key, value)); - } - return newlist; - } - } - - protected Collection getStringCollection(final RedisClusterCommands command, String key) { - final String type = command.type(key); - if (type.contains("list")) { //list - return decryptStringCollection(key, false, command.lrange(key, 0, -1)); - } else { //set - return decryptStringCollection(key, true, command.smembers(key)); - } - } - - @Override - public void rpushString(String key, String value) { - final RedisClusterCommands command = connectString(); - command.rpush(key, encryptValue(key, cryptor, value)); - releaseStringCommand(command); - } - - @Override - public String spopString(String key) { - final RedisClusterCommands command = connectString(); - String rs = command.spop(key); - releaseStringCommand(command); - return cryptor != null ? cryptor.encrypt(key, rs) : rs; - } - - @Override - public Set spopString(String key, int count) { - final RedisClusterCommands command = connectString(); - Set rs = command.spop(key, count); - releaseStringCommand(command); - return (Set) decryptStringCollection(key, true, rs); - } - - @Override - public int lremString(String key, String value) { - final RedisClusterCommands command = connectString(); - int rs = command.lrem(key, 1, encryptValue(key, cryptor, value)).intValue(); - releaseStringCommand(command); - return rs; - } - - @Override - public boolean sismemberString(String key, String value) { - final RedisClusterCommands command = connectString(); - boolean rs = command.sismember(key, encryptValue(key, cryptor, value)); - releaseStringCommand(command); - return rs; - } - - @Override - public void saddString(String key, String value) { - final RedisClusterCommands command = connectString(); - command.sadd(key, encryptValue(key, cryptor, value)); - releaseStringCommand(command); - } - - @Override - public int sremString(String key, String value) { - final RedisClusterCommands command = connectString(); - int rs = command.srem(key, encryptValue(key, cryptor, value)).intValue(); - releaseStringCommand(command); - return rs; - } - - @Override - public Map mgetLong(String... keys) { - final RedisClusterCommands command = connectString(); - List> rs = command.mget(keys); - releaseStringCommand(command); - Map map = new LinkedHashMap<>(); - rs.forEach(kv -> { - if (kv.hasValue()) { - map.put(kv.getKey(), Long.parseLong(kv.getValue())); - } - }); - return map; - } - - @Override - public void rpushLong(String key, long value) { - final RedisClusterCommands command = connectString(); - command.rpush(key, String.valueOf(value)); - releaseStringCommand(command); - } - - @Override - public Long spopLong(String key) { - final RedisClusterCommands command = connectString(); - String value = command.spop(key); - releaseStringCommand(command); - return value == null ? null : Long.parseLong(value); - } - - @Override - public Set spopLong(String key, int count) { - final RedisClusterCommands command = connectString(); - Set rs = (Set) formatLongCollection(true, command.spop(key, count)); - releaseStringCommand(command); - return rs; - } - - @Override - public int lremLong(String key, long value) { - final RedisClusterCommands command = connectString(); - int rs = command.lrem(key, 1, String.valueOf(value)).intValue(); - releaseStringCommand(command); - return rs; - } - - @Override - public boolean sismemberLong(String key, long value) { - final RedisClusterCommands command = connectString(); - boolean rs = command.sismember(key, String.valueOf(value)); - releaseStringCommand(command); - return rs; - } - - @Override - public void saddLong(String key, long value) { - final RedisClusterCommands command = connectString(); - command.sadd(key, String.valueOf(value)); - releaseStringCommand(command); - } - - @Override - public int sremLong(String key, long value) { - final RedisClusterCommands command = connectString(); - int rs = command.srem(key, String.valueOf(value)).intValue(); - releaseStringCommand(command); - return rs; - } - - @Override - public CompletableFuture hdelAsync(String key, String... fields) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.hdel(key, fields)); - }); - } - - @Override - public CompletableFuture> hkeysAsync(String key) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.hkeys(key)); - }); - } - - @Override - public CompletableFuture hlenAsync(String key) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.hlen(key)); - }); - } - - @Override - public CompletableFuture hincrAsync(String key, String field) { - return hincrbyAsync(key, field, 1); - } - - @Override - public CompletableFuture hincrbyAsync(String key, String field, long num) { - return connectStringAsync().thenCompose(command -> { - //此处获取的long值,无需传cryptor进行解密 - return completableStringFuture(key, null, command, command.hincrby(key, field, num)); - }); - } - - @Override - public CompletableFuture hincrbyFloatAsync(String key, String field, double num) { - return connectStringAsync().thenCompose(command -> { - //此处获取的long值,无需传cryptor进行解密 - return completableStringFuture(key, null, command, command.hincrbyfloat(key, field, num)); - }); - } - - @Override - public CompletableFuture hdecrAsync(String key, String field) { - return hincrbyAsync(key, field, -1); - } - - @Override - public CompletableFuture hdecrbyAsync(String key, String field, long num) { - return hincrbyAsync(key, field, -num); - } - - @Override - public CompletableFuture hexistsAsync(String key, String field) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.hexists(key, field)); - }); - } - - @Override - public CompletableFuture hsetAsync(String key, String field, Type type, T value) { - if (value == null) { - return CompletableFuture.completedFuture(null); - } - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.hset(key, field, convert.convertToBytes(type, value))); - }); - } - - @Override - public CompletableFuture hsetAsync(String key, String field, Convert convert0, Type type, T value) { - if (value == null) { - return CompletableFuture.completedFuture(null); - } - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.hset(key, field, (convert0 == null ? convert : convert0).convertToBytes(type, value))); - }); - } - - @Override - public CompletableFuture hsetStringAsync(String key, String field, String value) { - return connectStringAsync().thenCompose(command -> { - //不处理返回值,无需传cryptor进行解密 - return completableStringFuture(key, null, command, command.hset(key, field, value)); - }); - } - - @Override - public CompletableFuture hsetLongAsync(String key, String field, long value) { - return connectStringAsync().thenCompose(command -> { - //不处理返回值,无需传cryptor进行解密 - return completableStringFuture(key, null, command, command.hset(key, field, String.valueOf(value))); - }); - } - - @Override - public CompletableFuture hsetnxAsync(String key, String field, Type type, T value) { - if (value == null) { - return CompletableFuture.completedFuture(null); - } - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.hsetnx(key, field, convert.convertToBytes(type, value))); - }); - } - - @Override - public CompletableFuture hsetnxAsync(String key, String field, Convert convert0, Type type, T value) { - if (value == null) { - return CompletableFuture.completedFuture(null); - } - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.hsetnx(key, field, (convert0 == null ? convert : convert0).convertToBytes(type, value))); - }); - } - - @Override - public CompletableFuture hsetnxStringAsync(String key, String field, String value) { - return connectStringAsync().thenCompose(command -> { - //不处理返回值,无需传cryptor进行解密 - return completableStringFuture(key, null, command, command.hsetnx(key, field, value)); - }); - } - - @Override - public CompletableFuture hsetnxLongAsync(String key, String field, long value) { - return connectStringAsync().thenCompose(command -> { - //不处理返回值,无需传cryptor进行解密 - return completableStringFuture(key, null, command, command.hsetnx(key, field, String.valueOf(value))); - }); - } - - @Override - public CompletableFuture hmsetAsync(String key, Serializable... values) { - Map vals = new LinkedHashMap<>(); - for (int i = 0; i < values.length; i += 2) { - byte[] bs; - if (cryptor != null && values[i + 1] != null) { - bs = values[i + 1] instanceof String ? cryptor.encrypt(key, values[i + 1].toString()).getBytes(StandardCharsets.UTF_8) : encryptValue(key, cryptor, values[i + 1].getClass(), this.convert, values[i + 1]); - } else { - bs = values[i + 1] instanceof String ? values[i + 1].toString().getBytes(StandardCharsets.UTF_8) : this.convert.convertToBytes(values[i + 1]); - } - vals.put(String.valueOf(values[i]), bs); - } - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.hmset(key, vals)); - }); - } - - @Override - public CompletableFuture hmsetAsync(String key, Map map) { - Map vals = new LinkedHashMap<>(); - map.forEach((k, v) -> { - byte[] bs; - if (cryptor != null && v != null) { - bs = v instanceof String ? cryptor.encrypt(key, v.toString()).getBytes(StandardCharsets.UTF_8) : encryptValue(key, cryptor, v.getClass(), this.convert, v); - } else { - bs = v instanceof String ? v.toString().getBytes(StandardCharsets.UTF_8) : this.convert.convertToBytes(v); - } - vals.put(k.toString(), bs); - }); - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.hmset(key, vals)); - }); - } - - @Override - public CompletableFuture> hmgetAsync(String key, Type type, String... fields) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.hmget(key, fields).thenApply((List> rs) -> { - List list = new ArrayList<>(fields.length); - for (String field : fields) { - byte[] bs = null; - for (KeyValue kv : rs) { - if (kv.getKey().equals(field)) { - bs = kv.hasValue() ? kv.getValue() : null; - break; - } - } - if (bs == null) { - list.add(null); - } else { - list.add(decryptValue(key, cryptor, type, bs)); - } - } - return list; - })); - }); - } - - @Override - public CompletableFuture> hmapAsync(String key, Type type, int offset, int limit) { - return hmapAsync(key, type, offset, limit, null); - } - - @Override - public CompletableFuture> hmapAsync(String key, Type type, int offset, int limit, String pattern) { - return connectBytesAsync().thenCompose(command -> { - ScanArgs args = ScanArgs.Builder.limit(limit); - if (pattern != null) { - args = args.match(pattern); - } - return completableBytesFuture(command, command.hscan(key, new ScanCursor(String.valueOf(offset), false), args).thenApply((MapScanCursor rs) -> { - final Map map = new LinkedHashMap<>(); - rs.getMap().forEach((k, v) -> map.put(k, v == null ? null : decryptValue(key, cryptor, type, v))); - return map; - })); - }); - } - - @Override - public CompletableFuture hgetAsync(String key, String field, Type type) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.hget(key, field).thenApply(bs -> bs == null ? null : decryptValue(key, cryptor, type, bs))); - }); - } - - @Override - public CompletableFuture hgetStringAsync(String key, String field) { - return connectStringAsync().thenCompose(command -> { - return completableStringFuture(key, cryptor, command, command.hget(key, field)); - }); - } - - @Override - public CompletableFuture hgetLongAsync(String key, String field, long defValue) { - return connectStringAsync().thenCompose(command -> { - //此处获取的long值,无需传cryptor进行解密 - return completableStringFuture(key, null, command, command.hget(key, field).thenApply(bs -> bs == null ? defValue : Long.parseLong(bs))); - }); - } - - @Override - public CompletableFuture> mgetAsync(Type componentType, String... keys) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.mget(keys).thenApply((List> rs) -> { - Map map = new LinkedHashMap(rs.size()); - rs.forEach(kv -> { - if (kv.hasValue()) { - map.put(kv.getKey(), decryptValue(kv.getKey(), cryptor, componentType, kv.getValue())); - } - }); - return map; - })); - }); - } - - @Override - public CompletableFuture> mgetBytesAsync(String... keys) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.mget(keys).thenApply((List> rs) -> { - Map map = new LinkedHashMap(rs.size()); - rs.forEach(kv -> { - if (kv.hasValue()) { - map.put(kv.getKey(), decryptValue(kv.getKey(), cryptor, byte[].class, kv.getValue())); - } - }); - return map; - })); - }); - } - - @Override - public CompletableFuture> smembersAsync(String key, final Type componentType) { - return connectBytesAsync().thenCompose(command -> { - return command.smembers(key).thenApply(set -> formatCollection(key, cryptor, set, convert, componentType)); - }); - } - - @Override - public CompletableFuture> lrangeAsync(String key, final Type componentType) { - return connectBytesAsync().thenCompose(command -> { - return command.lrange(key, 0, -1).thenApply(list -> formatCollection(key, cryptor, list, convert, componentType)); - }); - } - - protected CompletableFuture> getCollectionAsync( - final RedisClusterAsyncCommands command, String key, final Type componentType) { - return completableBytesFuture(command, command.type(key).thenCompose(type -> { - if (type.contains("list")) { - return command.lrange(key, 0, -1).thenApply(list -> formatCollection(key, cryptor, list, convert, componentType)); - } else { //set - return command.smembers(key).thenApply(set -> formatCollection(key, cryptor, set, convert, componentType)); - } - })); - } - - @Override - public CompletableFuture>> lrangeAsync(Type componentType, String... keys) { - return connectBytesAsync().thenCompose(command -> { - final Map> map = new LinkedHashMap<>(); - final ReentrantLock mapLock = new ReentrantLock(); - final CompletableFuture[] futures = new CompletableFuture[keys.length]; - for (int i = 0; i < keys.length; i++) { - final String key = keys[i]; - futures[i] = command.lrange(key, 0, -1).thenAccept(rs -> { - if (rs != null) { - mapLock.lock(); - try { - map.put(key, formatCollection(key, cryptor, rs, convert, componentType)); - } finally { - mapLock.unlock(); - } - } - }).toCompletableFuture(); - } - return CompletableFuture.allOf(futures).thenApply(v -> map); - }); - } - - @Override - public CompletableFuture>> smembersAsync(Type componentType, String... keys) { - return connectBytesAsync().thenCompose(command -> { - final Map> map = new LinkedHashMap<>(); - final ReentrantLock mapLock = new ReentrantLock(); - final CompletableFuture[] futures = new CompletableFuture[keys.length]; - for (int i = 0; i < keys.length; i++) { - final String key = keys[i]; - futures[i] = command.smembers(key).thenAccept(rs -> { - if (rs != null) { - mapLock.lock(); - try { - map.put(key, formatCollection(key, cryptor, rs, convert, componentType)); - } finally { - mapLock.unlock(); - } - } - }).toCompletableFuture(); - } - return CompletableFuture.allOf(futures).thenApply(v -> map); - }); - } - - @Override - public CompletableFuture llenAsync(String key) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.type(key).thenCompose(type -> { - return command.llen(key).thenApply(v -> v.intValue()); - })); - }); - } - - @Override - public CompletableFuture scardAsync(String key) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.type(key).thenCompose(type -> { - return command.scard(key).thenApply(v -> v.intValue()); - })); - }); - } - - @Override - public CompletableFuture spopAsync(String key, Type componentType) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.spop(key).thenApply(bs -> bs == null ? null : decryptValue(key, cryptor, componentType, bs))); - }); - } - - @Override - public CompletableFuture> spopAsync(String key, int count, Type componentType) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.spop(key, count).thenApply(v -> formatCollection(key, cryptor, v, convert, componentType))); - }); - } - - @Override - public CompletableFuture rpushAsync(String key, Type componentType, T value) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.rpush(key, encryptValue(key, cryptor, componentType, convert.convertToBytes(componentType, value)))); - }); - } - - @Override - public CompletableFuture lremAsync(String key, Type componentType, T value) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.lrem(key, 1, encryptValue(key, cryptor, componentType, convert.convertToBytes(componentType, value))).thenApply(v -> v.intValue())); - }); - } - - @Override - public CompletableFuture sismemberAsync(String key, Type componentType, T value) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.sismember(key, encryptValue(key, cryptor, componentType, convert.convertToBytes(componentType, value)))); - }); - } - - @Override - @SuppressWarnings({"ConfusingArrayVararg", "PrimitiveArrayArgumentToVariableArgMethod"}) - public CompletableFuture saddAsync(String key, Type componentType, T value) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.sadd(key, encryptValue(key, cryptor, componentType, convert.convertToBytes(componentType, value)))); - }); - } - - @Override - public CompletableFuture sremAsync(String key, Type componentType, T value) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.srem(key, encryptValue(key, cryptor, componentType, convert.convertToBytes(componentType, value))).thenApply(v -> v.intValue())); - }); - } - - @Override - public CompletableFuture getBytesAsync(String key) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.get(key)); - }); - } - - @Override - public CompletableFuture getSetBytesAsync(String key, byte[] value) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.setGet(key, value)); - }); - } - - @Override - public CompletableFuture getexBytesAsync(String key, int expireSeconds) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.expire(key, expireSeconds).thenCompose(v -> command.get(key))); - }); - } - - @Override - public CompletableFuture setBytesAsync(String key, byte[] value) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.set(key, value)); - }); - } - - @Override - public CompletableFuture setexBytesAsync(String key, int expireSeconds, byte[] value) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.setex(key, expireSeconds, value)); - }); - } - - @Override - public CompletableFuture setnxexBytesAsync(String key, int expireSeconds, byte[] value) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.set(key, value, SetArgs.Builder.nx().ex(expireSeconds)).thenApply(r -> r != null && ("OK".equals(r) || Integer.parseInt(r) > 0))); - }); - } - - @Override - public CompletableFuture> keysAsync(String pattern) { - return connectStringAsync().thenCompose(command -> { - //此处获取key值,无需传cryptor进行解密 - return completableStringFuture(null, null, command, command.keys(pattern == null || pattern.isEmpty() ? "*" : pattern)); - }); - } - - @Override - public CompletableFuture> mgetStringAsync(String... keys) { - return connectStringAsync().thenCompose(command -> { - return completableStringFuture(null, null, command, command.mget(keys).thenApply((List> rs) -> { - Map map = new LinkedHashMap<>(); - rs.forEach(kv -> { - if (kv.hasValue()) { - map.put(kv.getKey(), cryptor != null ? cryptor.decrypt(kv.getKey(), kv.getValue()) : kv.getValue()); - } - }); - return map; - })); - }); - } - - protected CompletableFuture> getStringCollectionAsync(CompletableFuture> commandFuture, String key) { - return commandFuture.thenCompose(command -> getStringCollectionAsync(command, key)); - } - - protected CompletableFuture> getStringCollectionAsync(RedisClusterAsyncCommands command, String key) { - return completableStringFuture(key, null, command, command.type(key).thenApply(type -> { - if (type.contains("list")) { - return command.lrange(key, 0, -1).thenApply(list -> formatStringCollection(key, cryptor, false, list)); - } else { //set - return command.smembers(key).thenApply(list -> formatStringCollection(key, cryptor, true, list)); - } - })); - } - - @Override - public CompletableFuture rpushStringAsync(String key, String value) { - return connectStringAsync().thenCompose(command -> { - return completableStringFuture(key, null, command, command.rpush(key, encryptValue(key, cryptor, value))); - }); - } - - @Override - public CompletableFuture spopStringAsync(String key) { - return connectStringAsync().thenCompose(command -> { - return completableStringFuture(key, cryptor, command, command.spop(key)); - }); - } - - @Override - public CompletableFuture> spopStringAsync(String key, int count) { - return connectStringAsync().thenCompose(command -> { - return completableStringFuture(key, null, command, command.spop(key, count).thenApply(list -> formatStringCollection(key, cryptor, true, list))); - }); - } - - @Override - public CompletableFuture lremStringAsync(String key, String value) { - return connectStringAsync().thenCompose(command -> { - //此处获取的int值,无需传cryptor进行解密 - return completableStringFuture(key, null, command, command.lrem(key, 1, value).thenApply(v -> v.intValue())); - }); - } - - @Override - public CompletableFuture sismemberStringAsync(String key, String value) { - return connectStringAsync().thenCompose(command -> { - //此处获取的boolean值,无需传cryptor进行解密 - return completableStringFuture(key, null, command, command.sismember(key, value)); - }); - } - - @Override - public CompletableFuture saddStringAsync(String key, String value) { - return connectStringAsync().thenCompose(command -> { - //不处理返回值,无需传cryptor进行解密 - return completableStringFuture(key, null, command, command.sadd(key, value)); - }); - } - - @Override - public CompletableFuture sremStringAsync(String key, String value) { - return connectStringAsync().thenCompose(command -> { - //此处获取的int值,无需传cryptor进行解密 - return completableStringFuture(key, null, command, command.srem(key, value).thenApply(v -> v.intValue())); - }); - } - - @Override - public CompletableFuture> mgetLongAsync(String... keys) { - return connectStringAsync().thenCompose(command -> { - //此处获取的long值,无需传cryptor进行解密 - return completableStringFuture(keys[0], null, command, command.mget(keys).thenApply((List> rs) -> { - Map map = new LinkedHashMap<>(); - rs.forEach(kv -> { - if (kv.hasValue()) { - map.put(kv.getKey(), Long.parseLong(kv.getValue())); - } - }); - return map; - })); - }); - } - - protected CompletableFuture> getLongCollectionAsync(final CompletableFuture> commandFuture, String key) { - return commandFuture.thenCompose(command -> getLongCollectionAsync(command, key)); - } - - protected CompletableFuture> getLongCollectionAsync(final RedisClusterAsyncCommands command, String key) { - //此处获取的long值,无需传cryptor进行解密 - return completableStringFuture(key, null, command, command.type(key).thenApply(type -> { - if (type.contains("list")) { - return command.lrange(key, 0, -1).thenApply(rs -> formatLongCollection(false, rs)); - } else { //set - return command.smembers(key).thenApply(rs -> formatLongCollection(true, rs)); - } - })); - } - - @Override - public CompletableFuture rpushLongAsync(String key, long value) { - return connectStringAsync().thenCompose(command -> { - //不处理返回值,无需传cryptor进行解密 - return completableStringFuture(key, null, command, command.rpush(key, String.valueOf(value))); - }); - } - - @Override - public CompletableFuture spopLongAsync(String key) { - return connectStringAsync().thenCompose(command -> { - //此处获取的long值,无需传cryptor进行解密 - return completableStringFuture(key, null, command, command.spop(key).thenApply(v -> v == null ? null : Long.parseLong(v))); - }); - } - - @Override - public CompletableFuture> spopLongAsync(String key, int count) { - return connectStringAsync().thenCompose(command -> { - //此处获取的long值,无需传cryptor进行解密 - return completableStringFuture(key, null, command, command.spop(key, count).thenApply(v -> v == null ? null : formatLongCollection(true, v))); - }); - } - - @Override - public CompletableFuture lremLongAsync(String key, long value) { - return connectStringAsync().thenCompose(command -> { - //此处获取的int值,无需传cryptor进行解密 - return completableStringFuture(key, null, command, command.lrem(key, 1, String.valueOf(value)).thenApply(v -> v.intValue())); - }); - } - - @Override - public CompletableFuture sismemberLongAsync(String key, long value) { - return connectStringAsync().thenCompose(command -> { - //此处获取的boolean值,无需传cryptor进行解密 - return completableStringFuture(key, null, command, command.sismember(key, String.valueOf(value))); - }); - } - - @Override - public CompletableFuture saddLongAsync(String key, long value) { - return connectStringAsync().thenCompose(command -> { - //不处理返回值,无需传cryptor进行解密 - return completableStringFuture(key, null, command, command.sadd(key, String.valueOf(value))); - }); - } - - @Override - public CompletableFuture sremLongAsync(String key, long value) { - return connectStringAsync().thenCompose(command -> { - //此处获取的int值,无需传cryptor进行解密 - return completableStringFuture(key, null, command, command.srem(key, String.valueOf(value)).thenApply(v -> v.intValue())); - }); - } - - @Override - public CompletableFuture getLongArrayAsync(String... keys) { - return connectStringAsync().thenCompose(command -> { - //此处获取的long值,无需传cryptor进行解密 - return completableStringFuture(keys[0], null, command, command.mget(keys).thenApply((List> rs) -> { - Long[] array = new Long[keys.length]; - for (int i = 0; i < array.length; i++) { - Long bs = null; - for (KeyValue kv : rs) { - if (kv.getKey().equals(keys[i])) { - bs = kv.hasValue() ? Long.parseLong(kv.getValue()) : null; - break; - } - } - array[i] = bs; - } - return array; - })); - }); - } - - @Override - public CompletableFuture> getLongCollectionAsync(String key) { - return getLongCollectionAsync(connectStringAsync(), key); - } - - @Override - public CompletableFuture>> getLongCollectionMapAsync(boolean set, String... keys) { - return connectStringAsync().thenCompose(command -> { - final Map> map = new LinkedHashMap<>(); - final ReentrantLock mapLock = new ReentrantLock(); - final CompletableFuture[] futures = new CompletableFuture[keys.length]; - if (set) { - for (int i = 0; i < keys.length; i++) { - final String key = keys[i]; - futures[i] = command.smembers(key).thenAccept(rs -> { - if (rs != null) { - mapLock.lock(); - try { - map.put(key, formatLongCollection(set, rs)); - } finally { - mapLock.unlock(); - } - } - }).toCompletableFuture(); - } - } else { - for (int i = 0; i < keys.length; i++) { - final String key = keys[i]; - futures[i] = command.lrange(key, 0, -1).thenAccept(rs -> { - if (rs != null) { - mapLock.lock(); - try { - map.put(key, formatLongCollection(set, rs)); - } finally { - mapLock.unlock(); - } - } - }).toCompletableFuture(); - } - } - return CompletableFuture.allOf(futures).thenApply(v -> map); - }); - } - - @Override - public CompletableFuture> getexLongCollectionAsync(String key, int expireSeconds) { - return connectStringAsync().thenCompose(command -> { - //此处获取的long值,无需传cryptor进行解密 - return completableStringFuture(key, null, command, command.expire(key, expireSeconds).thenCompose(v -> getLongCollectionAsync(command, key))); - }); - } - - @Override - public CompletableFuture>> getStringCollectionMapAsync(boolean set, String... keys) { - return connectStringAsync().thenCompose(command -> { - final Map> map = new LinkedHashMap<>(); - final ReentrantLock mapLock = new ReentrantLock(); - final CompletableFuture[] futures = new CompletableFuture[keys.length]; - if (set) { - for (int i = 0; i < keys.length; i++) { - final String key = keys[i]; - futures[i] = command.smembers(key).thenAccept(rs -> { - if (rs != null) { - mapLock.lock(); - try { - map.put(key, formatStringCollection(key, cryptor, set, rs)); - } finally { - mapLock.unlock(); - } - } - }).toCompletableFuture(); - } - } else { - for (int i = 0; i < keys.length; i++) { - final String key = keys[i]; - futures[i] = command.lrange(key, 0, -1).thenAccept(rs -> { - if (rs != null) { - mapLock.lock(); - try { - map.put(key, formatStringCollection(key, cryptor, set, rs)); - } finally { - mapLock.unlock(); - } - } - }).toCompletableFuture(); - } - } - return CompletableFuture.allOf(futures).thenApply(v -> map); - }); - } - - @Override - public CompletableFuture> getexStringCollectionAsync(String key, int expireSeconds) { - return connectStringAsync().thenCompose(command -> { - return completableStringFuture(key, null, command, command.expire(key, expireSeconds).thenCompose(v -> getStringCollectionAsync(command, key))); - }); - } - - @Override - public Collection getCollection(String key, final Type componentType) { - final RedisClusterCommands command = connectBytes(); - Collection rs = getCollection(command, key, componentType); - releaseBytesCommand(command); - return rs; - } - - @Override - public Map> getCollectionMap(final boolean set, final Type componentType, String... keys) { - final RedisClusterCommands command = connectBytes(); - final Map> map = new LinkedHashMap<>(); - if (set) { //set - for (String key : keys) { - map.put(key, formatCollection(key, cryptor, command.smembers(key), convert, componentType)); - } - } else { //list - for (String key : keys) { - map.put(key, formatCollection(key, cryptor, command.lrange(key, 0, -1), convert, componentType)); - } - } - releaseBytesCommand(command); - return map; - } - - @Override - public int getCollectionSize(String key) { - final RedisClusterCommands command = connectBytes(); - final String type = command.type(key); - int rs; - if (type.contains("list")) { - rs = command.llen(key).intValue(); - } else { //set - rs = command.scard(key).intValue(); - } - releaseBytesCommand(command); - return rs; - } - - @Override - public Collection getexCollection(String key, final int expireSeconds, final Type componentType) { - final RedisClusterCommands command = connectBytes(); - command.expire(key, Duration.ofSeconds(expireSeconds)); - Collection rs = getCollection(command, key, componentType); - releaseBytesCommand(command); - return rs; - } - - @Override - public String[] getStringArray(final String... keys) { - final RedisClusterCommands command = connectString(); - List> rs = command.mget(keys); - releaseStringCommand(command); - String[] array = new String[keys.length]; - for (int i = 0; i < array.length; i++) { - String bs = null; - for (KeyValue kv : rs) { - if (kv.getKey().equals(keys[i])) { - bs = kv.hasValue() ? (cryptor != null ? cryptor.decrypt(kv.getKey(), kv.getValue()) : kv.getValue()) : null; - break; - } - } - array[i] = bs; - } - return array; - } - - @Override - public Collection getStringCollection(String key) { - final RedisClusterCommands command = connectString(); - Collection rs = getStringCollection(command, key); - releaseStringCommand(command); - return rs; - } - - @Override - public Map> getStringCollectionMap(final boolean set, String... keys) { - final RedisClusterCommands command = connectString(); - final Map> map = new LinkedHashMap<>(); - if (set) {//set - for (String key : keys) { - map.put(key, decryptStringCollection(key, true, command.smembers(key))); - } - } else { //list - for (String key : keys) { - map.put(key, decryptStringCollection(key, false, command.lrange(key, 0, -1))); - } - } - releaseStringCommand(command); - return map; - } - - @Override - public Collection getexStringCollection(String key, final int expireSeconds) { - final RedisClusterCommands command = connectString(); - command.expire(key, expireSeconds); - Collection rs = getStringCollection(command, key); - releaseStringCommand(command); - return rs; - } - - @Override - public Long[] getLongArray(String... keys) { - final RedisClusterCommands command = connectString(); - List> rs = command.mget(keys); - releaseStringCommand(command); - Long[] array = new Long[keys.length]; - for (int i = 0; i < array.length; i++) { - Long bs = null; - for (KeyValue kv : rs) { - if (kv.getKey().equals(keys[i])) { - bs = kv.hasValue() ? Long.parseLong(kv.getValue()) : null; - break; - } - } - array[i] = bs; - } - return array; - } - - @Override - public Collection getLongCollection(String key) { - final RedisClusterCommands command = connectString(); - Collection rs = getLongCollection(command, key); - releaseStringCommand(command); - return rs; - } - - protected Collection getLongCollection(final RedisClusterCommands command, String key) { - final String type = command.type(key); - Collection rs; - if (type.contains("list")) { //list - rs = formatLongCollection(false, command.lrange(key, 0, -1)); - } else { //set - rs = formatLongCollection(true, command.smembers(key)); - } - releaseStringCommand(command); - return rs; - } - - @Override - public Map> getLongCollectionMap(boolean set, String... keys) { - final RedisClusterCommands command = connectString(); - final Map> map = new LinkedHashMap<>(); - if (set) { //set - for (String key : keys) { - map.put(key, formatLongCollection(true, command.smembers(key))); - } - } else { //list - for (String key : keys) { - map.put(key, formatLongCollection(false, command.lrange(key, 0, -1))); - } - } - releaseStringCommand(command); - return map; - } - - @Override - public Collection getexLongCollection(String key, int expireSeconds) { - final RedisClusterCommands command = connectString(); - command.expire(key, expireSeconds); - releaseStringCommand(command); - return getLongCollection(key); - } - - @Override - public CompletableFuture getStringArrayAsync(String... keys) { - return connectStringAsync().thenCompose(command -> { - return completableStringFuture(null, null, command, command.mget(keys).thenApply((List> rs) -> { - String[] array = new String[keys.length]; - for (int i = 0; i < array.length; i++) { - String bs = null; - for (KeyValue kv : rs) { - if (kv.getKey().equals(keys[i])) { - bs = kv.hasValue() ? (cryptor != null ? cryptor.decrypt(kv.getKey(), kv.getValue()) : kv.getValue()) : null; - break; - } - } - array[i] = bs; - } - return array; - })); - }); - } - - @Override - public CompletableFuture> getexCollectionAsync(String key, int expireSeconds, final Type componentType) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.expire(key, expireSeconds).thenCompose(v -> getCollectionAsync(command, key, componentType))); - }); - } - - @Override - public CompletableFuture> getCollectionAsync(String key, final Type componentType) { - return connectBytesAsync().thenCompose(command -> { - return getCollectionAsync(command, key, componentType); - }); - } - - @Override - public CompletableFuture>> getCollectionMapAsync(boolean set, Type componentType, String... keys) { - return connectBytesAsync().thenCompose(command -> { - final Map> map = new LinkedHashMap<>(); - final ReentrantLock mapLock = new ReentrantLock(); - final CompletableFuture[] futures = new CompletableFuture[keys.length]; - if (set) { - for (int i = 0; i < keys.length; i++) { - final String key = keys[i]; - futures[i] = command.smembers(key).thenAccept(rs -> { - if (rs != null) { - mapLock.lock(); - try { - map.put(key, formatCollection(key, cryptor, rs, convert, componentType)); - } finally { - mapLock.unlock(); - } - } - }).toCompletableFuture(); - } - } else { - for (int i = 0; i < keys.length; i++) { - final String key = keys[i]; - futures[i] = command.lrange(key, 0, -1).thenAccept(rs -> { - if (rs != null) { - mapLock.lock(); - try { - map.put(key, formatCollection(key, cryptor, rs, convert, componentType)); - } finally { - mapLock.unlock(); - } - } - }).toCompletableFuture(); - } - } - return CompletableFuture.allOf(futures).thenApply(v -> map); - }); - } - - @Override - public CompletableFuture getCollectionSizeAsync(String key) { - return connectBytesAsync().thenCompose(command -> { - return completableBytesFuture(command, command.type(key).thenCompose(type -> { - if (type.contains("list")) { - return command.llen(key).thenApply(v -> v.intValue()); - } else { //set - return command.scard(key).thenApply(v -> v.intValue()); - } - })); - }); - } - - @Override - public CompletableFuture> getStringCollectionAsync(String key) { - return getStringCollectionAsync(connectStringAsync(), key); - } - -} diff --git a/src/org/redkalex/cache/redis/lettuce/RedisLettuceCacheSourceProvider.java b/src/org/redkalex/cache/redis/lettuce/RedisLettuceCacheSourceProvider.java deleted file mode 100644 index 2c35c3d..0000000 --- a/src/org/redkalex/cache/redis/lettuce/RedisLettuceCacheSourceProvider.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package org.redkalex.cache.redis.lettuce; - -import org.redkale.annotation.Priority; -import org.redkale.source.CacheSource; -import org.redkale.source.CacheSourceProvider; -import org.redkale.util.AnyValue; - -/** - * - * @author zhangjx - */ -@Priority(-100) -public class RedisLettuceCacheSourceProvider implements CacheSourceProvider { - - @Override - public boolean acceptsConf(AnyValue config) { - try { - Object.class.isAssignableFrom(io.lettuce.core.support.BoundedPoolConfig.class); //试图加载Lettuce相关类 - return new RedisLettuceCacheSource().acceptsConf(config); - } catch (Throwable e) { - return false; - } - } - - @Override - public CacheSource createInstance() { - return new RedisLettuceCacheSource(); - } - -} diff --git a/src/org/redkalex/cache/redis/redission/RedissionCacheSource.java b/src/org/redkalex/cache/redis/redission/RedissionCacheSource.java deleted file mode 100644 index a6039f3..0000000 --- a/src/org/redkalex/cache/redis/redission/RedissionCacheSource.java +++ /dev/null @@ -1,2226 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package org.redkalex.cache.redis.redission; - -import com.zdemo.cachex.AbstractRedisSource; -import com.zdemo.cachex.RedisCryptor; -import org.redisson.Redisson; -import org.redisson.api.*; -import org.redisson.client.codec.*; -import org.redisson.config.*; -import org.redkale.annotation.AutoLoad; -import org.redkale.annotation.ResourceListener; -import org.redkale.annotation.ResourceType; -import org.redkale.convert.Convert; -import org.redkale.service.Local; -import org.redkale.source.CacheSource; -import org.redkale.util.AnyValue; -import org.redkale.util.ResourceEvent; -import org.redkale.util.Utility; - -import java.io.Serializable; -import java.lang.reflect.Type; -import java.net.URI; -import java.nio.charset.StandardCharsets; -import java.time.Duration; -import java.util.*; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CompletionStage; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.locks.ReentrantLock; -import java.util.logging.Level; -import java.util.logging.Logger; -import java.util.stream.Collectors; - -/** - * //https://www.cnblogs.com/xiami2046/p/13934146.html - * - * @author zhangjx - */ -@Local -@AutoLoad(false) -@ResourceType(CacheSource.class) -public class RedissionCacheSource extends AbstractRedisSource { - - protected final Logger logger = Logger.getLogger(this.getClass().getSimpleName()); - - protected List nodeAddrs; - - protected RedissonClient client; - - @Override - public void init(AnyValue conf) { - super.init(conf); - if (conf == null) { - conf = AnyValue.create(); - } - initClient(conf); - } - - private void initClient(AnyValue conf) { - final List addresses = new ArrayList<>(); - Config redisConfig = new Config(); - AnyValue[] nodes = getNodes(conf); - String cluster = conf.getOrDefault("cluster", ""); - int maxconns = conf.getIntValue(CACHE_SOURCE_MAXCONNS, Utility.cpus()); - BaseConfig baseConfig = null; - for (AnyValue node : nodes) { - String addr = node.getValue(CACHE_SOURCE_URL, node.getValue("addr")); //兼容addr - String db0 = node.getValue(CACHE_SOURCE_DB, "").trim(); - if (!db0.isEmpty()) { - this.db = Integer.valueOf(db0); - } - String username = node.getValue(CACHE_SOURCE_USER, "").trim(); - String password = node.getValue(CACHE_SOURCE_PASSWORD, "").trim(); - if (addr.startsWith("redis")) { - URI uri = URI.create(addr); - if (uri.getQuery() != null && !uri.getQuery().isEmpty()) { - String[] qrys = uri.getQuery().split("&|="); - for (int i = 0; i < qrys.length; i += 2) { - if (CACHE_SOURCE_USER.equals(qrys[i])) { - username = i == qrys.length - 1 ? "" : qrys[i + 1]; - } else if (CACHE_SOURCE_PASSWORD.equals(qrys[i])) { - password = i == qrys.length - 1 ? "" : qrys[i + 1]; - } else if (CACHE_SOURCE_DB.equals(qrys[i])) { - String urldb = i == qrys.length - 1 ? "-1" : qrys[i + 1]; - this.db = Integer.valueOf(urldb); - } - if (CACHE_SOURCE_MAXCONNS.equals(qrys[i])) { - maxconns = i == qrys.length - 1 ? Utility.cpus() : Integer.parseInt(qrys[i + 1]); - } - } - } - } - addresses.add(addr); - if (nodes.length == 1) { - baseConfig = redisConfig.useSingleServer(); - if (maxconns > 0) { - redisConfig.useSingleServer().setConnectionMinimumIdleSize(maxconns / 2 + 1); - redisConfig.useSingleServer().setConnectionPoolSize(maxconns); - } - redisConfig.useSingleServer().setAddress(addr); - redisConfig.useSingleServer().setDatabase(this.db); - } else if ("masterslave".equalsIgnoreCase(cluster)) { //主从 - baseConfig = redisConfig.useMasterSlaveServers(); - if (maxconns > 0) { - redisConfig.useMasterSlaveServers().setMasterConnectionMinimumIdleSize(maxconns / 2 + 1); - redisConfig.useMasterSlaveServers().setMasterConnectionPoolSize(maxconns); - redisConfig.useMasterSlaveServers().setSlaveConnectionMinimumIdleSize(maxconns / 2 + 1); - redisConfig.useMasterSlaveServers().setSlaveConnectionPoolSize(maxconns); - } - if (node.get("master") != null) { - redisConfig.useMasterSlaveServers().setMasterAddress(addr); - } else { - redisConfig.useMasterSlaveServers().addSlaveAddress(addr); - } - redisConfig.useMasterSlaveServers().setDatabase(this.db); - } else if ("cluster".equalsIgnoreCase(cluster)) { //集群 - baseConfig = redisConfig.useClusterServers(); - if (maxconns > 0) { - redisConfig.useClusterServers().setMasterConnectionMinimumIdleSize(maxconns / 2 + 1); - redisConfig.useClusterServers().setMasterConnectionPoolSize(maxconns); - redisConfig.useClusterServers().setSlaveConnectionMinimumIdleSize(maxconns / 2 + 1); - redisConfig.useClusterServers().setSlaveConnectionPoolSize(maxconns); - } - redisConfig.useClusterServers().addNodeAddress(addr); - } else if ("replicated".equalsIgnoreCase(cluster)) { // - baseConfig = redisConfig.useReplicatedServers(); - if (maxconns > 0) { - redisConfig.useReplicatedServers().setMasterConnectionMinimumIdleSize(maxconns / 2 + 1); - redisConfig.useReplicatedServers().setMasterConnectionPoolSize(maxconns); - redisConfig.useReplicatedServers().setSlaveConnectionMinimumIdleSize(maxconns / 2 + 1); - redisConfig.useReplicatedServers().setSlaveConnectionPoolSize(maxconns); - } - redisConfig.useReplicatedServers().addNodeAddress(addr); - redisConfig.useReplicatedServers().setDatabase(this.db); - } else if ("sentinel".equalsIgnoreCase(cluster)) { // - baseConfig = redisConfig.useSentinelServers(); - if (maxconns > 0) { - redisConfig.useSentinelServers().setMasterConnectionMinimumIdleSize(maxconns / 2 + 1); - redisConfig.useSentinelServers().setMasterConnectionPoolSize(maxconns); - redisConfig.useSentinelServers().setSlaveConnectionMinimumIdleSize(maxconns / 2 + 1); - redisConfig.useSentinelServers().setSlaveConnectionPoolSize(maxconns); - } - redisConfig.useSentinelServers().addSentinelAddress(addr); - redisConfig.useSentinelServers().setDatabase(this.db); - } - if (baseConfig != null) { //单个进程的不同自定义密码 - if (!username.isEmpty()) { - baseConfig.setUsername(username); - } - if (!password.isEmpty()) { - baseConfig.setPassword(password); - } - } - } - if (baseConfig != null) { //配置全局密码 - String username = conf.getValue(CACHE_SOURCE_USER, "").trim(); - String password = conf.getValue(CACHE_SOURCE_PASSWORD, "").trim(); - String retryAttempts = conf.getValue("retryAttempts", "").trim(); - String retryInterval = conf.getValue("retryInterval", "").trim(); - if (!username.isEmpty()) { - baseConfig.setUsername(username); - } - if (!password.isEmpty()) { - baseConfig.setPassword(password); - } - if (!retryAttempts.isEmpty()) { - baseConfig.setRetryAttempts(Integer.parseInt(retryAttempts)); - } - if (!retryInterval.isEmpty()) { - baseConfig.setRetryInterval(Integer.parseInt(retryInterval)); - } - } - RedissonClient old = this.client; - this.client = Redisson.create(redisConfig); - this.nodeAddrs = addresses; - if (old != null) { - old.shutdown(); - } -// RTopic topic = client.getTopic("__keyevent@" + db + "__:expired", new StringCodec()); -// topic.addListener(String.class, (CharSequence cs, String key) -> { -// if (logger.isLoggable(Level.FINE)) logger.log(Level.FINE, RedissionCacheSource.class.getSimpleName() + "." + db + ": expired key=" + key + ", cs=" + cs); -// }); - //if (logger.isLoggable(Level.FINE)) logger.log(Level.FINE, RedissionCacheSource.class.getSimpleName() + ": addrs=" + addresses + ", db=" + db); - - } - - @Override - @ResourceListener - public void onResourceChange(ResourceEvent[] events) { - if (events == null || events.length < 1) { - return; - } - StringBuilder sb = new StringBuilder(); - for (ResourceEvent event : events) { - sb.append("CacheSource(name=").append(resourceName()).append(") change '").append(event.name()).append("' to '").append(event.coverNewValue()).append("'\r\n"); - } - initClient(this.config); - if (sb.length() > 0) { - logger.log(Level.INFO, sb.toString()); - } - } - - public boolean acceptsConf(AnyValue config) { - if (config == null) { - return false; - } - AnyValue[] nodes = getNodes(config); - if (nodes == null || nodes.length == 0) { - return false; - } - for (AnyValue node : nodes) { - String val = node.getValue(CACHE_SOURCE_URL, node.getValue("addr")); //兼容addr - if (val != null && val.startsWith("redis://")) { - return true; - } - if (val != null && val.startsWith("rediss://")) { - return true; - } - } - return false; - } - - protected AnyValue[] getNodes(AnyValue config) { - AnyValue[] nodes = config.getAnyValues(CACHE_SOURCE_NODE); - if (nodes == null || nodes.length == 0) { - AnyValue one = config.getAnyValue(CACHE_SOURCE_NODE); - if (one == null) { - String val = config.getValue(CACHE_SOURCE_URL); - if (val == null) { - return nodes; - } - nodes = new AnyValue[]{config}; - } else { - nodes = new AnyValue[]{one}; - } - } - return nodes; - } - - @Override - public final String getType() { - return "redis"; - } - - @Override - public String toString() { - return getClass().getSimpleName() + "{addrs=" + this.nodeAddrs + ", db=" + this.db + "}"; - } - - @Local - public RedissonClient getRedissonClient() { - return client; - } - - protected CompletableFuture completableFuture(CompletionStage rf) { - return rf.toCompletableFuture(); - } - - protected CompletableFuture completableFuture(String key, RedisCryptor cryptor, CompletionStage rf) { - return cryptor != null ? rf.toCompletableFuture().thenApply(v -> cryptor.decrypt(key, v)) : rf.toCompletableFuture(); - } - - protected CompletableFuture completableFuture(String key, RedisCryptor cryptor, Type type, CompletionStage rf) { - return rf.toCompletableFuture().thenApply(bs -> decryptValue(key, cryptor, type, bs)); - } - - protected CompletableFuture completableFuture(String key, RedisCryptor cryptor, Convert c, Type type, CompletionStage rf) { - return rf.toCompletableFuture().thenApply(bs -> decryptValue(key, cryptor, c, type, bs)); - } - - @Override - public void destroy(AnyValue conf) { - super.destroy(conf); - if (client != null) { - client.shutdown(); - } - } - - //--------------------- exists ------------------------------ - @Override - public CompletableFuture existsAsync(String key) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - return completableFuture(bucket.isExistsAsync()); - } - - @Override - public boolean exists(String key) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - return bucket.isExists(); - } - - //--------------------- get ------------------------------ - @Override - public CompletableFuture getAsync(String key, Type type) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - return completableFuture(key, cryptor, type, bucket.getAsync()); - } - - @Override - public CompletableFuture getStringAsync(String key) { - final RBucket bucket = client.getBucket(key, StringCodec.INSTANCE); - return completableFuture(key, cryptor, bucket.getAsync()); - } - - @Override - public CompletableFuture getSetStringAsync(String key, String value) { - final RBucket bucket = client.getBucket(key, StringCodec.INSTANCE); - return completableFuture(key, cryptor, bucket.getAndSetAsync(encryptValue(key, cryptor, value))); - } - - @Override - public CompletableFuture getLongAsync(String key, long defValue) { - final RBucket bucket = client.getBucket(key, StringCodec.INSTANCE); - return completableFuture(bucket.getAsync().thenApply(v -> v == null ? defValue : Long.parseLong(v))); - } - - @Override - public CompletableFuture getSetLongAsync(String key, long value, long defValue) { - final RBucket bucket = client.getBucket(key, StringCodec.INSTANCE); - return completableFuture(bucket.getAndSetAsync(String.valueOf(value)).thenApply(v -> v == null ? defValue : Long.parseLong(v))); - } - - @Override - public T get(String key, final Type type) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - return decryptValue(key, cryptor, type, bucket.get()); - } - - @Override - public String getString(String key) { - final RBucket bucket = client.getBucket(key, StringCodec.INSTANCE); - return decryptValue(key, cryptor, bucket.get()); - } - - @Override - public String getSetString(String key, String value) { - final RBucket bucket = client.getBucket(key, StringCodec.INSTANCE); - return decryptValue(key, cryptor, bucket.getAndSet(encryptValue(key, cryptor, value))); - } - - @Override - public long getLong(String key, long defValue) { - final RBucket bucket = client.getBucket(key, StringCodec.INSTANCE); - String v = bucket.get(); - return v == null ? defValue : Long.parseLong(v); - } - - @Override - public long getSetLong(String key, long value, long defValue) { - final RBucket bucket = client.getBucket(key, StringCodec.INSTANCE); - String v = bucket.getAndSet(String.valueOf(value)); - return v == null ? defValue : Long.parseLong(v); - } - - //--------------------- getex ------------------------------ - @Override - public CompletableFuture getexAsync(String key, int expireSeconds, final Type type) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - return completableFuture(bucket.getAndExpireAsync(Duration.ofSeconds(expireSeconds)).thenApply(bs -> decryptValue(key, cryptor, type, bs))); - } - - @Override - public T getex(String key, final int expireSeconds, final Type type) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - return decryptValue(key, cryptor, type, bucket.getAndExpire(Duration.ofSeconds(expireSeconds))); - } - - @Override - public CompletableFuture getexStringAsync(String key, int expireSeconds) { - final RBucket bucket = client.getBucket(key, StringCodec.INSTANCE); - return completableFuture(bucket.getAndExpireAsync(Duration.ofSeconds(expireSeconds)).thenApply(v -> decryptValue(key, cryptor, v))); - } - - @Override - public String getexString(String key, final int expireSeconds) { - final RBucket bucket = client.getBucket(key, StringCodec.INSTANCE); - String rs = bucket.getAndExpire(Duration.ofSeconds(expireSeconds)); - return rs == null ? rs : decryptValue(key, cryptor, rs); - } - - @Override - public CompletableFuture getexLongAsync(String key, int expireSeconds, long defValue) { - final RBucket bucket = client.getBucket(key, StringCodec.INSTANCE); - return completableFuture(bucket.getAndExpireAsync(Duration.ofSeconds(expireSeconds)).thenApply(v -> v == null ? defValue : Long.parseLong(v))); - } - - @Override - public long getexLong(String key, final int expireSeconds, long defValue) { - final RBucket bucket = client.getBucket(key, StringCodec.INSTANCE); - String v = bucket.getAndExpire(Duration.ofSeconds(expireSeconds)); - return v == null ? defValue : Long.parseLong(v); - } - - //--------------------- setex ------------------------------ - @Override - public CompletableFuture setAsync(String key, final Type type, T value) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - return completableFuture(bucket.setAsync(type == String.class ? encryptValue(key, cryptor, String.valueOf(value)).getBytes(StandardCharsets.UTF_8) : encryptValue(key, cryptor, type, convert, value))); - } - - @Override - public CompletableFuture setAsync(String key, Convert convert0, final Type type, T value) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - return completableFuture(bucket.setAsync(type == String.class ? encryptValue(key, cryptor, String.valueOf(value)).getBytes(StandardCharsets.UTF_8) : encryptValue(key, cryptor, type, convert0, value))); - } - - @Override - public CompletableFuture setnxAsync(String key, final Type type, T value) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - return completableFuture(bucket.setIfAbsentAsync(type == String.class ? encryptValue(key, cryptor, String.valueOf(value)).getBytes(StandardCharsets.UTF_8) : encryptValue(key, cryptor, type, convert, value))); - } - - @Override - public CompletableFuture setnxAsync(String key, Convert convert0, final Type type, T value) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - return completableFuture(bucket.setIfAbsentAsync(type == String.class ? encryptValue(key, cryptor, String.valueOf(value)).getBytes(StandardCharsets.UTF_8) : encryptValue(key, cryptor, type, convert0, value))); - } - - @Override - public CompletableFuture getSetAsync(String key, final Type type, T value) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - return completableFuture(bucket.getAndSetAsync(type == String.class ? encryptValue(key, cryptor, String.valueOf(value)).getBytes(StandardCharsets.UTF_8) : encryptValue(key, cryptor, type, convert, value)) - .thenApply(old -> old == null ? null : convert.convertFrom(type, old))); - } - - @Override - public CompletableFuture getSetAsync(String key, Convert convert0, final Type type, T value) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - Convert c = convert0 == null ? this.convert : convert0; - return completableFuture(bucket.getAndSetAsync(type == String.class ? encryptValue(key, cryptor, String.valueOf(value)).getBytes(StandardCharsets.UTF_8) : encryptValue(key, cryptor, type, c, value)) - .thenApply(old -> old == null ? null : (T) c.convertFrom(type, old))); - } - - @Override - public boolean setnxBytes(final String key, final byte[] value) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - return bucket.setIfAbsent(value); - } - - @Override - public CompletableFuture setnxBytesAsync(final String key, byte[] value) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - return completableFuture(bucket.setIfAbsentAsync(value)); - } - - @Override - public CompletableFuture msetAsync(Object... keyVals) { - Map map = new LinkedHashMap<>(); - for (int i = 0; i < keyVals.length; i += 2) { - String key = keyVals[i].toString(); - Object val = keyVals[i + 1]; - map.put(key, val instanceof String ? encryptValue(key, cryptor, val.toString()).getBytes(StandardCharsets.UTF_8) : encryptValue(key, cryptor, this.convert, val)); - } - final RBuckets bucket = client.getBuckets(ByteArrayCodec.INSTANCE); - return completableFuture(bucket.setAsync(map).thenApply(v -> null)); - } - - @Override - public CompletableFuture msetAsync(Map map) { - Map bs = new LinkedHashMap<>(); - map.forEach((key, val) -> { - bs.put(key.toString(), val instanceof String ? encryptValue(key.toString(), cryptor, val.toString()).getBytes(StandardCharsets.UTF_8) : encryptValue(key.toString(), cryptor, this.convert, val)); - }); - final RBuckets bucket = client.getBuckets(ByteArrayCodec.INSTANCE); - return completableFuture(bucket.setAsync(bs).thenApply(v -> null)); - } - - @Override - public void mset(Object... keyVals) { - Map map = new LinkedHashMap<>(); - for (int i = 0; i < keyVals.length; i += 2) { - String key = keyVals[i].toString(); - Object val = keyVals[i + 1]; - map.put(key, val instanceof String ? encryptValue(key, cryptor, val.toString()).getBytes(StandardCharsets.UTF_8) : encryptValue(key, cryptor, this.convert, val)); - } - client.getBuckets(ByteArrayCodec.INSTANCE).set(map); - } - - @Override - public void mset(Map map) { - Map bs = new LinkedHashMap<>(); - map.forEach((key, val) -> { - bs.put(key.toString(), val instanceof String ? encryptValue(key.toString(), cryptor, val.toString()).getBytes(StandardCharsets.UTF_8) : encryptValue(key.toString(), cryptor, this.convert, val)); - }); - client.getBuckets(ByteArrayCodec.INSTANCE).set(bs); - } - - @Override - public void set(final String key, final Type type, T value) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - bucket.set(type == String.class ? encryptValue(key, cryptor, String.valueOf(value)).getBytes(StandardCharsets.UTF_8) : encryptValue(key, cryptor, type, convert, value)); - } - - @Override - public void set(String key, final Convert convert0, final Type type, T value) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - bucket.set(encryptValue(key, cryptor, type, convert0, value)); - } - - @Override - public boolean setnx(final String key, final Type type, T value) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - return bucket.setIfAbsent(type == String.class ? encryptValue(key, cryptor, String.valueOf(value)).getBytes(StandardCharsets.UTF_8) : encryptValue(key, cryptor, type, convert, value)); - } - - @Override - public boolean setnx(String key, final Convert convert0, final Type type, T value) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - return bucket.setIfAbsent(encryptValue(key, cryptor, type, convert0, value)); - } - - @Override - public T getSet(final String key, final Type type, T value) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - byte[] old = bucket.getAndSet(type == String.class ? encryptValue(key, cryptor, String.valueOf(value)).getBytes(StandardCharsets.UTF_8) : encryptValue(key, cryptor, type, convert, value)); - return old == null ? null : convert.convertFrom(type, old); - } - - @Override - public T getSet(String key, final Convert convert0, final Type type, T value) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - Convert c = convert0 == null ? this.convert : convert0; - byte[] old = bucket.getAndSet(encryptValue(key, cryptor, type, c, value)); - return decryptValue(key, cryptor, c, type, old); - } - - @Override - public CompletableFuture setStringAsync(String key, String value) { - return completableFuture(client.getBucket(key, StringCodec.INSTANCE).setAsync(value)); - } - - @Override - public CompletableFuture setnxStringAsync(String key, String value) { - return completableFuture(client.getBucket(key, StringCodec.INSTANCE).setIfAbsentAsync(value)); - } - - @Override - public void setString(String key, String value) { - client.getBucket(key, StringCodec.INSTANCE).set(value); - } - - @Override - public boolean setnxString(String key, String value) { - return client.getBucket(key, StringCodec.INSTANCE).setIfAbsent(value); - } - - @Override - public CompletableFuture setLongAsync(String key, long value) { - return completableFuture(client.getAtomicLong(key).setAsync(value)); - } - - @Override - public CompletableFuture setnxLongAsync(String key, long value) { - final RBucket bucket = client.getBucket(key, StringCodec.INSTANCE); - return completableFuture(bucket.setIfAbsentAsync(String.valueOf(value))); - } - - @Override - public void setLong(String key, long value) { - client.getAtomicLong(key).set(value); - } - - @Override - public boolean setnxLong(String key, long value) { - return client.getBucket(key, StringCodec.INSTANCE).setIfAbsent(String.valueOf(value)); - } - - //--------------------- setex ------------------------------ - @Override - public CompletableFuture setexAsync(String key, int expireSeconds, final Type type, T value) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - return completableFuture(bucket.setAsync(encryptValue(key, cryptor, type, convert, value), expireSeconds, TimeUnit.SECONDS).thenApply(r -> null)); - } - - @Override - public CompletableFuture setexAsync(String key, int expireSeconds, Convert convert0, final Type type, T value) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - return completableFuture(bucket.setAsync(encryptValue(key, cryptor, type, convert0, value), expireSeconds, TimeUnit.SECONDS).thenApply(r -> null)); - } - - @Override - public void setex(String key, int expireSeconds, final Type type, T value) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - bucket.set(encryptValue(key, cryptor, type, convert, value), expireSeconds, TimeUnit.SECONDS); - } - - @Override - public void setex(String key, int expireSeconds, Convert convert0, final Type type, T value) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - bucket.set(encryptValue(key, cryptor, type, convert0, value), expireSeconds, TimeUnit.SECONDS); - } - - @Override - public CompletableFuture setexStringAsync(String key, int expireSeconds, String value) { - final RBucket bucket = client.getBucket(key, StringCodec.INSTANCE); - return completableFuture(bucket.setAsync(encryptValue(key, cryptor, value), expireSeconds, TimeUnit.SECONDS).thenApply(r -> null)); - } - - @Override - public void setexString(String key, int expireSeconds, String value) { - final RBucket bucket = client.getBucket(key, StringCodec.INSTANCE); - bucket.set(encryptValue(key, cryptor, value), expireSeconds, TimeUnit.SECONDS); - } - - @Override - public CompletableFuture setexLongAsync(String key, int expireSeconds, long value) { - final RBucket bucket = client.getBucket(key, StringCodec.INSTANCE); - return completableFuture(bucket.setAsync(String.valueOf(value), expireSeconds, TimeUnit.SECONDS).thenApply(r -> null)); - } - - @Override - public void setexLong(String key, int expireSeconds, long value) { - final RBucket bucket = client.getBucket(key, StringCodec.INSTANCE); - bucket.set(String.valueOf(value), expireSeconds, TimeUnit.SECONDS); - } - - //--------------------- setex ------------------------------ - @Override - public CompletableFuture setnxexAsync(String key, int expireSeconds, final Type type, T value) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - return completableFuture(bucket.setIfAbsentAsync(encryptValue(key, cryptor, type, convert, value), Duration.ofSeconds(expireSeconds))); - } - - @Override - public CompletableFuture setnxexAsync(String key, int expireSeconds, Convert convert0, final Type type, T value) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - return completableFuture(bucket.setIfAbsentAsync(encryptValue(key, cryptor, type, convert0, value), Duration.ofSeconds(expireSeconds))); - } - - @Override - public boolean setnxex(String key, int expireSeconds, final Type type, T value) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - return bucket.setIfAbsent(encryptValue(key, cryptor, type, convert, value), Duration.ofSeconds(expireSeconds)); - } - - @Override - public boolean setnxex(String key, int expireSeconds, Convert convert0, final Type type, T value) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - return bucket.setIfAbsent(encryptValue(key, cryptor, type, convert0, value), Duration.ofSeconds(expireSeconds)); - } - - @Override - public CompletableFuture setnxexStringAsync(String key, int expireSeconds, String value) { - final RBucket bucket = client.getBucket(key, StringCodec.INSTANCE); - return completableFuture(bucket.setIfAbsentAsync(encryptValue(key, cryptor, value), Duration.ofSeconds(expireSeconds))); - } - - @Override - public boolean setnxexString(String key, int expireSeconds, String value) { - final RBucket bucket = client.getBucket(key, StringCodec.INSTANCE); - return bucket.setIfAbsent(encryptValue(key, cryptor, value), Duration.ofSeconds(expireSeconds)); - } - - @Override - public CompletableFuture setnxexLongAsync(String key, int expireSeconds, long value) { - final RBucket bucket = client.getBucket(key, StringCodec.INSTANCE); - return completableFuture(bucket.setIfAbsentAsync(String.valueOf(value), Duration.ofSeconds(expireSeconds))); - } - - @Override - public boolean setnxexLong(String key, int expireSeconds, long value) { - final RBucket bucket = client.getBucket(key, StringCodec.INSTANCE); - return bucket.setIfAbsent(String.valueOf(value), Duration.ofSeconds(expireSeconds)); - } - - //--------------------- expire ------------------------------ - @Override - public CompletableFuture expireAsync(String key, int expireSeconds) { - return completableFuture(client.getBucket(key).expireAsync(Duration.ofSeconds(expireSeconds)).thenApply(r -> null)); - } - - @Override - public void expire(String key, int expireSeconds) { - client.getBucket(key).expire(Duration.ofSeconds(expireSeconds)); - } - - //--------------------- del ------------------------------ - @Override - public CompletableFuture delAsync(String... keys) { - return completableFuture(client.getKeys().deleteAsync(keys).thenApply(rs -> rs.intValue())); - } - - @Override - public int del(String... keys) { - return (int) client.getKeys().delete(keys); - } - - //--------------------- incrby ------------------------------ - @Override - public long incr(final String key) { - return client.getAtomicLong(key).incrementAndGet(); - } - - @Override - public CompletableFuture incrAsync(final String key) { - return completableFuture(client.getAtomicLong(key).incrementAndGetAsync()); - } - - @Override - public long incrby(final String key, long num) { - return client.getAtomicLong(key).addAndGet(num); - } - - @Override - public double incrbyFloat(final String key, double num) { - return client.getAtomicDouble(key).addAndGet(num); - } - - @Override - public CompletableFuture incrbyAsync(final String key, long num) { - return completableFuture(client.getAtomicLong(key).addAndGetAsync(num)); - } - - @Override - public CompletableFuture incrbyFloatAsync(final String key, double num) { - return completableFuture(client.getAtomicDouble(key).addAndGetAsync(num)); - } - - //--------------------- decrby ------------------------------ - @Override - public long decr(final String key) { - return client.getAtomicLong(key).decrementAndGet(); - } - - @Override - public CompletableFuture decrAsync(final String key) { - return completableFuture(client.getAtomicLong(key).decrementAndGetAsync()); - } - - @Override - public long decrby(final String key, long num) { - return client.getAtomicLong(key).addAndGet(-num); - } - - @Override - public CompletableFuture decrbyAsync(final String key, long num) { - return completableFuture(client.getAtomicLong(key).addAndGetAsync(-num)); - } - - @Override - public int hdel(final String key, String... fields) { - RMap map = client.getMap(key, MapByteArrayCodec.instance); - return (int) map.fastRemove(fields); - } - - @Override - public int hlen(final String key) { - return client.getMap(key, MapByteArrayCodec.instance).size(); - } - - @Override - public List hkeys(final String key) { - return (List) new ArrayList<>(client.getMap(key, MapStringCodec.instance).keySet()); - } - - @Override - public long hincr(final String key, String field) { - RMap map = client.getMap(key, MapLongCodec.instance); - return map.addAndGet(field, 1L); - } - - @Override - public long hincrby(final String key, String field, long num) { - RMap map = client.getMap(key, MapLongCodec.instance); - return map.addAndGet(field, num); - } - - @Override - public double hincrbyFloat(final String key, String field, double num) { - RMap map = client.getMap(key, MapDoubleCodec.instance); - return map.addAndGet(field, num); - } - - @Override - public long hdecr(final String key, String field) { - RMap map = client.getMap(key, MapLongCodec.instance); - return map.addAndGet(field, -1L); - } - - @Override - public long hdecrby(final String key, String field, long num) { - RMap map = client.getMap(key, MapLongCodec.instance); - return map.addAndGet(field, -num); - } - - @Override - public boolean hexists(final String key, String field) { - return client.getMap(key, MapByteArrayCodec.instance).containsKey(field); - } - - @Override - public void hset(final String key, final String field, final Type type, final T value) { - if (value == null) { - return; - } - RMap map = client.getMap(key, MapByteArrayCodec.instance); - map.fastPut(field, encryptValue(key, cryptor, type, convert, value)); - } - - @Override - public void hset(final String key, final String field, final Convert convert0, final Type type, final T value) { - if (value == null) { - return; - } - RMap map = client.getMap(key, MapByteArrayCodec.instance); - map.fastPut(field, encryptValue(key, cryptor, type, convert0, value)); - } - - @Override - public boolean hsetnx(final String key, final String field, final Type type, final T value) { - if (value == null) { - return false; - } - RMap map = client.getMap(key, MapByteArrayCodec.instance); - return map.fastPutIfAbsent(field, encryptValue(key, cryptor, type, convert, value)); - } - - @Override - public boolean hsetnx(final String key, final String field, final Convert convert0, final Type type, final T value) { - if (value == null) { - return false; - } - RMap map = client.getMap(key, MapByteArrayCodec.instance); - return map.fastPutIfAbsent(field, encryptValue(key, cryptor, type, convert0, value)); - } - - @Override - public void hsetString(final String key, final String field, final String value) { - if (value == null) { - return; - } - RMap map = client.getMap(key, MapStringCodec.instance); - map.fastPut(field, encryptValue(key, cryptor, value)); - } - - @Override - public boolean hsetnxString(final String key, final String field, final String value) { - if (value == null) { - return false; - } - RMap map = client.getMap(key, MapStringCodec.instance); - return map.fastPutIfAbsent(field, encryptValue(key, cryptor, value)); - } - - @Override - public void hsetLong(final String key, final String field, final long value) { - RMap map = client.getMap(key, MapLongCodec.instance); - map.fastPut(field, value); - } - - @Override - public boolean hsetnxLong(final String key, final String field, final long value) { - RMap map = client.getMap(key, MapLongCodec.instance); - return map.fastPutIfAbsent(field, value); - } - - @Override - public void hmset(final String key, final Serializable... values) { - Map vals = new LinkedHashMap<>(); - for (int i = 0; i < values.length; i += 2) { - vals.put(String.valueOf(values[i]), values[i + 1] instanceof String ? encryptValue(key, cryptor, values[i + 1].toString()).getBytes(StandardCharsets.UTF_8) : encryptValue(key, cryptor, this.convert, values[i + 1])); - } - RMap rm = client.getMap(key, MapByteArrayCodec.instance); - rm.putAll(vals); - } - - @Override - public void hmset(final String key, final Map map) { - Map vals = new LinkedHashMap<>(); - map.forEach((k, v) -> { - vals.put(k.toString(), v instanceof String ? encryptValue(key, cryptor, v.toString()).getBytes(StandardCharsets.UTF_8) : encryptValue(key, cryptor, this.convert, v)); - }); - RMap rm = client.getMap(key, MapByteArrayCodec.instance); - rm.putAll(vals); - } - - @Override - public List hmget(final String key, final Type type, final String... fields) { - RMap map = client.getMap(key, MapByteArrayCodec.instance); - Map rs = map.getAll(Utility.ofSet(fields)); - List list = new ArrayList<>(fields.length); - for (String field : fields) { - byte[] bs = rs.get(field); - if (bs == null) { - list.add(null); - } else { - list.add(decryptValue(key, cryptor, type, bs)); - } - } - return list; - } - - @Override - public Map hmap(final String key, final Type type, int offset, int limit, String pattern) { - RMap map = client.getMap(key, MapByteArrayCodec.instance); - Iterator it = map.keySet(pattern, offset + limit).iterator(); - final Map rs = new LinkedHashMap<>(); - int index = -1; - while (it.hasNext()) { - if (++index < offset) { - continue; - } - if (index >= offset + limit) { - break; - } - String field = it.next(); - byte[] bs = map.get(field); - if (bs != null) { - rs.put(field, decryptValue(key, cryptor, type, bs)); - } - } - return rs; - } - - @Override - public Map hmap(final String key, final Type type, int offset, int limit) { - RMap map = client.getMap(key, MapByteArrayCodec.instance); - Iterator it = map.keySet(offset + limit).iterator(); - final Map rs = new LinkedHashMap<>(); - int index = -1; - while (it.hasNext()) { - if (++index < offset) { - continue; - } - if (index >= offset + limit) { - break; - } - String field = it.next(); - byte[] bs = map.get(field); - if (bs != null) { - rs.put(field, decryptValue(key, cryptor, type, bs)); - } - } - return rs; - } - - @Override - public T hget(final String key, final String field, final Type type) { - RMap map = client.getMap(key, MapByteArrayCodec.instance); - byte[] bs = map.get(field); - return decryptValue(key, cryptor, type, bs); - } - - @Override - public String hgetString(final String key, final String field) { - RMap map = client.getMap(key, MapStringCodec.instance); - return decryptValue(key, cryptor, map.get(field)); - } - - @Override - public long hgetLong(final String key, final String field, long defValue) { - RMap map = client.getMap(key, MapLongCodec.instance); - Long rs = map.get(field); - return rs == null ? defValue : rs; - } - - @Override - public CompletableFuture hdelAsync(final String key, String... fields) { - RMap map = client.getMap(key, MapByteArrayCodec.instance); - return completableFuture(map.fastRemoveAsync(fields).thenApply(r -> r.intValue())); - } - - @Override - public CompletableFuture hlenAsync(final String key) { - RMap map = client.getMap(key, MapByteArrayCodec.instance); - return completableFuture(map.sizeAsync()); - } - - @Override - public CompletableFuture> hkeysAsync(final String key) { - RMap map = client.getMap(key, MapByteArrayCodec.instance); - return completableFuture(map.readAllKeySetAsync().thenApply(set -> set == null ? null : new ArrayList(set))); - } - - @Override - public CompletableFuture hincrAsync(final String key, String field) { - RMap map = client.getMap(key, MapLongCodec.instance); - return completableFuture(map.addAndGetAsync(field, 1L)); - } - - @Override - public CompletableFuture hincrbyAsync(final String key, String field, long num) { - RMap map = client.getMap(key, MapLongCodec.instance); - return completableFuture(map.addAndGetAsync(field, num)); - } - - @Override - public CompletableFuture hincrbyFloatAsync(final String key, String field, double num) { - RMap map = client.getMap(key, MapDoubleCodec.instance); - return completableFuture(map.addAndGetAsync(field, num)); - } - - @Override - public CompletableFuture hdecrAsync(final String key, String field) { - RMap map = client.getMap(key, MapLongCodec.instance); - return completableFuture(map.addAndGetAsync(field, -1L)); - } - - @Override - public CompletableFuture hdecrbyAsync(final String key, String field, long num) { - RMap map = client.getMap(key, MapLongCodec.instance); - return completableFuture(map.addAndGetAsync(field, -num)); - } - - @Override - public CompletableFuture hexistsAsync(final String key, String field) { - RMap map = client.getMap(key, MapLongCodec.instance); - return completableFuture(map.containsKeyAsync(field)); - } - - @Override - public CompletableFuture hsetAsync(final String key, final String field, final Type type, final T value) { - RMap map = client.getMap(key, MapByteArrayCodec.instance); - return completableFuture(map.fastPutAsync(field, encryptValue(key, cryptor, type, convert, value)).thenApply(r -> null)); - } - - @Override - public CompletableFuture hsetAsync(final String key, final String field, final Convert convert0, final Type type, final T value) { - RMap map = client.getMap(key, MapByteArrayCodec.instance); - return completableFuture(map.fastPutAsync(field, encryptValue(key, cryptor, type, convert0, value)).thenApply(r -> null)); - } - - @Override - public CompletableFuture hsetnxAsync(final String key, final String field, final Type type, final T value) { - RMap map = client.getMap(key, MapByteArrayCodec.instance); - return completableFuture(map.fastPutIfAbsentAsync(field, encryptValue(key, cryptor, type, convert, value))); - } - - @Override - public CompletableFuture hsetnxAsync(final String key, final String field, final Convert convert0, final Type type, final T value) { - RMap map = client.getMap(key, MapByteArrayCodec.instance); - return completableFuture(map.fastPutIfAbsentAsync(field, encryptValue(key, cryptor, type, convert0, value))); - } - - @Override - public CompletableFuture hsetStringAsync(final String key, final String field, final String value) { - if (value == null) { - return CompletableFuture.completedFuture(null); - } - RMap map = client.getMap(key, MapStringCodec.instance); - return completableFuture(map.fastPutAsync(field, encryptValue(key, cryptor, value)).thenApply(r -> null)); - } - - @Override - public CompletableFuture hsetnxStringAsync(final String key, final String field, final String value) { - if (value == null) { - return CompletableFuture.completedFuture(false); - } - RMap map = client.getMap(key, MapStringCodec.instance); - return completableFuture(map.fastPutIfAbsentAsync(field, encryptValue(key, cryptor, value))); - } - - @Override - public CompletableFuture hsetLongAsync(final String key, final String field, final long value) { - RMap map = client.getMap(key, MapLongCodec.instance); - return completableFuture(map.fastPutAsync(field, value).thenApply(r -> null)); - } - - @Override - public CompletableFuture hsetnxLongAsync(final String key, final String field, final long value) { - RMap map = client.getMap(key, MapLongCodec.instance); - return completableFuture(map.fastPutIfAbsentAsync(field, value)); - } - - @Override - public CompletableFuture hmsetAsync(final String key, final Serializable... values) { - Map vals = new LinkedHashMap<>(); - for (int i = 0; i < values.length; i += 2) { - vals.put(String.valueOf(values[i]), encryptValue(key, cryptor, convert, values[i + 1])); - } - RMap rm = client.getMap(key, MapByteArrayCodec.instance); - return completableFuture(rm.putAllAsync(vals)); - } - - @Override - public CompletableFuture hmsetAsync(final String key, final Map map) { - Map vals = new LinkedHashMap<>(); - map.forEach((k, v) -> { - vals.put(k.toString(), encryptValue(key, cryptor, convert, v)); - }); - RMap rm = client.getMap(key, MapByteArrayCodec.instance); - return completableFuture(rm.putAllAsync(vals)); - } - - @Override - public CompletableFuture> hmgetAsync(final String key, final Type type, final String... fields) { - RMap map = client.getMap(key, MapByteArrayCodec.instance); - return completableFuture(map.getAllAsync(Utility.ofSet(fields)).thenApply(rs -> { - List list = new ArrayList<>(fields.length); - for (String field : fields) { - byte[] bs = rs.get(field); - if (bs == null) { - list.add(null); - } else { - list.add(decryptValue(key, cryptor, type, bs)); - } - } - return list; - })); - } - - @Override - public CompletableFuture> hmapAsync(final String key, final Type type, int offset, int limit) { - return CompletableFuture.supplyAsync(() -> { - RMap map = client.getMap(key, MapByteArrayCodec.instance); - - Iterator it = map.keySet(offset + limit).iterator(); - final Map rs = new LinkedHashMap<>(); - int index = -1; - while (it.hasNext()) { - if (++index < offset) { - continue; - } - if (index >= offset + limit) { - break; - } - String field = it.next(); - byte[] bs = map.get(field); - if (bs != null) { - rs.put(field, decryptValue(key, cryptor, type, bs)); - } - } - return rs; - }); - } - - @Override - public CompletableFuture> hmapAsync(final String key, final Type type, int offset, int limit, String pattern) { - return CompletableFuture.supplyAsync(() -> { - RMap map = client.getMap(key, MapByteArrayCodec.instance); - - Iterator it = map.keySet(pattern, offset + limit).iterator(); - final Map rs = new LinkedHashMap<>(); - int index = -1; - while (it.hasNext()) { - if (++index < offset) { - continue; - } - if (index >= offset + limit) { - break; - } - String field = it.next(); - byte[] bs = map.get(field); - if (bs != null) { - rs.put(field, decryptValue(key, cryptor, type, bs)); - } - } - return rs; - }); - } - - @Override - public CompletableFuture hgetAsync(final String key, final String field, final Type type) { - RMap map = client.getMap(key, MapByteArrayCodec.instance); - return completableFuture(map.getAsync(field).thenApply(r -> decryptValue(key, cryptor, type, r))); - } - - @Override - public CompletableFuture hgetStringAsync(final String key, final String field) { - RMap map = client.getMap(key, MapStringCodec.instance); - return completableFuture(key, cryptor, map.getAsync(field)); - } - - @Override - public CompletableFuture hgetLongAsync(final String key, final String field, long defValue) { - RMap map = client.getMap(key, MapLongCodec.instance); - return completableFuture(map.getAsync(field).thenApply(r -> r == null ? defValue : r.longValue())); - } - - //--------------------- collection ------------------------------ - @Override - public CompletableFuture llenAsync(String key) { - return completableFuture(client.getList(key).sizeAsync()); - } - - @Override - public CompletableFuture scardAsync(String key) { - return completableFuture(client.getSet(key).sizeAsync()); - } - - @Override - public int llen(String key) { - return client.getList(key).size(); - } - - @Override - public int scard(String key) { - return client.getSet(key).size(); - } - - @Override - public CompletableFuture> smembersAsync(String key, final Type componentType) { - return completableFuture((CompletionStage) client.getSet(key, ByteArrayCodec.INSTANCE).readAllAsync().thenApply(set -> { - if (set == null || set.isEmpty()) { - return set; - } - Set rs = new LinkedHashSet<>(); - for (Object item : set) { - byte[] bs = (byte[]) item; - if (bs == null) { - rs.add(null); - } else { - rs.add(componentType == String.class ? (T) decryptValue(key, cryptor, new String(bs, StandardCharsets.UTF_8)) : (T) decryptValue(key, cryptor, componentType, bs)); - } - } - return rs; - })); - } - - @Override - public CompletableFuture> lrangeAsync(String key, final Type componentType) { - return completableFuture((CompletionStage) client.getList(key, ByteArrayCodec.INSTANCE).readAllAsync().thenApply(list -> { - if (list == null || list.isEmpty()) { - return list; - } - List rs = new ArrayList<>(); - for (Object item : list) { - byte[] bs = (byte[]) item; - if (bs == null) { - rs.add(null); - } else { - rs.add(componentType == String.class ? (T) decryptValue(key, cryptor, new String(bs, StandardCharsets.UTF_8)) : (T) decryptValue(key, cryptor, componentType, bs)); - } - } - return rs; - })); - } - - @Override - public CompletableFuture> mgetLongAsync(String... keys) { - return completableFuture(client.getBuckets(org.redisson.client.codec.LongCodec.INSTANCE).getAsync(keys)); - } - - @Override - public CompletableFuture> mgetStringAsync(String... keys) { - return completableFuture(client.getBuckets(StringCodec.INSTANCE).getAsync(keys).thenApply(map -> { - if (cryptor == null) { - return (Map) map; - } - Map rs = new LinkedHashMap(); - map.forEach((k, v) -> rs.put(k, decryptValue(k, cryptor, v == null ? null : v.toString()))); - return rs; - })); - } - - @Override - public CompletableFuture> mgetAsync(final Type componentType, String... keys) { - return completableFuture(client.getBuckets(ByteArrayCodec.INSTANCE).getAsync(keys).thenApply(map -> { - Map rs = new LinkedHashMap(); - map.forEach((k, v) -> rs.put(k, decryptValue(k, cryptor, componentType, (byte[]) v))); - return rs; - })); - } - - @Override - public CompletableFuture> mgetBytesAsync(String... keys) { - return completableFuture(client.getBuckets(ByteArrayCodec.INSTANCE).getAsync(keys).thenApply(map -> { - Map rs = new LinkedHashMap(); - map.forEach((k, v) -> rs.put(k, decryptValue(k, cryptor, byte[].class, (byte[]) v))); - return rs; - })); - } - - @Override - public CompletableFuture>> lrangeAsync(final Type componentType, final String... keys) { - final CompletableFuture>> rsFuture = new CompletableFuture<>(); - final Map> map = new LinkedHashMap<>(); - final ReentrantLock mapLock = new ReentrantLock(); - final CompletableFuture[] futures = new CompletableFuture[keys.length]; - for (int i = 0; i < keys.length; i++) { - final String key = keys[i]; - futures[i] = completableFuture(client.getList(key, ByteArrayCodec.INSTANCE).readAllAsync().thenApply(list -> { - if (list == null || list.isEmpty()) { - return list; - } - List rs = new ArrayList<>(); - for (Object item : list) { - byte[] bs = (byte[]) item; - if (bs == null) { - rs.add(null); - } else { - rs.add(decryptValue(key, cryptor, componentType, bs)); - } - } - mapLock.lock(); - try { - map.put(key, rs); - } finally { - mapLock.unlock(); - } - return rs; - })); - } - CompletableFuture.allOf(futures).whenComplete((w, e) -> { - if (e != null) { - rsFuture.completeExceptionally(e); - } else { - rsFuture.complete(map); - } - }); - return rsFuture; - } - - @Override - public CompletableFuture>> smembersAsync(final Type componentType, final String... keys) { - final CompletableFuture>> rsFuture = new CompletableFuture<>(); - final Map> map = new LinkedHashMap<>(); - final ReentrantLock mapLock = new ReentrantLock(); - final CompletableFuture[] futures = new CompletableFuture[keys.length]; - for (int i = 0; i < keys.length; i++) { - final String key = keys[i]; - futures[i] = completableFuture(client.getSet(key, ByteArrayCodec.INSTANCE).readAllAsync().thenApply(set -> { - if (set == null || set.isEmpty()) { - return set; - } - Set rs = new LinkedHashSet<>(); - for (Object item : set) { - byte[] bs = (byte[]) item; - if (bs == null) { - rs.add(null); - } else { - rs.add(decryptValue(key, cryptor, componentType, bs)); - } - } - mapLock.lock(); - try { - map.put(key, rs); - } finally { - mapLock.unlock(); - } - return rs; - })); - } - CompletableFuture.allOf(futures).whenComplete((w, e) -> { - if (e != null) { - rsFuture.completeExceptionally(e); - } else { - rsFuture.complete(map); - } - }); - return rsFuture; - } - - @Override - public Set smembers(String key, final Type componentType) { - return (Set) smembersAsync(key, componentType).join(); - } - - @Override - public List lrange(String key, final Type componentType) { - return (List) lrangeAsync(key, componentType).join(); - } - - @Override - public Map mgetLong(final String... keys) { - return client.getBuckets(org.redisson.client.codec.LongCodec.INSTANCE).get(keys); - } - - @Override - public Map mgetString(final String... keys) { - Map map = client.getBuckets(StringCodec.INSTANCE).get(keys); - if (cryptor != null && !map.isEmpty()) { - Map rs = new LinkedHashMap<>(); - map.forEach((k, v) -> rs.put(k, decryptValue(k, cryptor, v))); - return rs; - } - return map; - } - - @Override - public Map mget(final Type componentType, final String... keys) { - Map map = client.getBuckets(ByteArrayCodec.INSTANCE).get(keys); - Map rs = new LinkedHashMap(map.size()); - map.forEach((k, v) -> rs.put(k, decryptValue(k, cryptor, componentType, v))); - return rs; - } - - @Override - public Map mgetBytes(final String... keys) { - Map map = client.getBuckets(ByteArrayCodec.INSTANCE).get(keys); - Map rs = new LinkedHashMap(map.size()); - map.forEach((k, v) -> rs.put(k, decryptValue(k, cryptor, byte[].class, v))); - return rs; - } - - @Override - public Map> smembers(final Type componentType, String... keys) { - return (Map) smembersAsync(componentType, keys).join(); - } - - @Override - public Map> lrange(final Type componentType, String... keys) { - return (Map) lrangeAsync(componentType, keys).join(); - } - - //--------------------- existsItem ------------------------------ - @Override - public boolean sismember(String key, final Type componentType, T value) { - final RSet bucket = client.getSet(key, ByteArrayCodec.INSTANCE); - return bucket.contains(componentType == String.class ? encryptValue(key, cryptor, String.valueOf(value)).getBytes(StandardCharsets.UTF_8) : encryptValue(key, cryptor, componentType, convert, value)); - } - - @Override - public CompletableFuture sismemberAsync(String key, final Type componentType, T value) { - final RSet bucket = client.getSet(key, ByteArrayCodec.INSTANCE); - return completableFuture(bucket.containsAsync(encryptValue(key, cryptor, componentType, convert, value))); - } - - @Override - public boolean sismemberString(String key, String value) { - final RSet bucket = client.getSet(key, StringCodec.INSTANCE); - return bucket.contains(encryptValue(key, cryptor, value)); - } - - @Override - public CompletableFuture sismemberStringAsync(String key, String value) { - final RSet bucket = client.getSet(key, StringCodec.INSTANCE); - return completableFuture(bucket.containsAsync(encryptValue(key, cryptor, value))); - } - - @Override - public boolean sismemberLong(String key, long value) { - final RSet bucket = client.getSet(key, org.redisson.client.codec.LongCodec.INSTANCE); - return bucket.contains(value); - } - - @Override - public CompletableFuture sismemberLongAsync(String key, long value) { - final RSet bucket = client.getSet(key, org.redisson.client.codec.LongCodec.INSTANCE); - return completableFuture(bucket.containsAsync(value)); - } - - //--------------------- rpush ------------------------------ - @Override - public CompletableFuture rpushAsync(String key, final Type componentType, T value) { - final RList bucket = client.getList(key, ByteArrayCodec.INSTANCE); - return completableFuture(bucket.addAsync(encryptValue(key, cryptor, componentType, convert, value)).thenApply(r -> null)); - } - - @Override - public void rpush(String key, final Type componentType, T value) { - final RList bucket = client.getList(key, ByteArrayCodec.INSTANCE); - bucket.add(componentType == String.class ? encryptValue(key, cryptor, String.valueOf(value)).getBytes(StandardCharsets.UTF_8) : encryptValue(key, cryptor, componentType, convert, value)); - } - - @Override - public CompletableFuture rpushStringAsync(String key, String value) { - final RList bucket = client.getList(key, StringCodec.INSTANCE); - return completableFuture(bucket.addAsync(encryptValue(key, cryptor, value)).thenApply(r -> null)); - } - - @Override - public void rpushString(String key, String value) { - final RList bucket = client.getList(key, StringCodec.INSTANCE); - bucket.add(encryptValue(key, cryptor, value)); - } - - @Override - public CompletableFuture rpushLongAsync(String key, long value) { - final RList bucket = client.getList(key, org.redisson.client.codec.LongCodec.INSTANCE); - return completableFuture(bucket.addAsync(value).thenApply(r -> null)); - } - - @Override - public void rpushLong(String key, long value) { - final RList bucket = client.getList(key, org.redisson.client.codec.LongCodec.INSTANCE); - bucket.add(value); - } - - //--------------------- lrem ------------------------------ - @Override - public CompletableFuture lremAsync(String key, final Type componentType, T value) { - return completableFuture(client.getList(key, ByteArrayCodec.INSTANCE).removeAsync(encryptValue(key, cryptor, componentType, convert, value)).thenApply(r -> r ? 1 : 0)); - } - - @Override - public int lrem(String key, final Type componentType, T value) { - final RList bucket = client.getList(key, ByteArrayCodec.INSTANCE); - return bucket.remove(componentType == String.class ? encryptValue(key, cryptor, String.valueOf(value)).getBytes(StandardCharsets.UTF_8) : encryptValue(key, cryptor, componentType, convert, value)) ? 1 : 0; - } - - @Override - public CompletableFuture lremStringAsync(String key, String value) { - return completableFuture(client.getList(key, StringCodec.INSTANCE).removeAsync(encryptValue(key, cryptor, value)).thenApply(r -> r ? 1 : 0)); - } - - @Override - public int lremString(String key, String value) { - return client.getList(key, StringCodec.INSTANCE).remove(encryptValue(key, cryptor, value)) ? 1 : 0; - } - - @Override - public CompletableFuture lremLongAsync(String key, long value) { - return completableFuture(client.getList(key, org.redisson.client.codec.LongCodec.INSTANCE).removeAsync((Object) value).thenApply(r -> r ? 1 : 0)); - } - - @Override - public int lremLong(String key, long value) { - return client.getList(key, org.redisson.client.codec.LongCodec.INSTANCE).remove((Object) value) ? 1 : 0; - } - - //--------------------- sadd ------------------------------ - @Override - public CompletableFuture saddAsync(String key, Type componentType, T value) { - final RSet bucket = client.getSet(key, ByteArrayCodec.INSTANCE); - return completableFuture(bucket.addAsync(encryptValue(key, cryptor, componentType, convert, value)).thenApply(r -> null)); - } - - @Override - public CompletableFuture spopAsync(String key, Type componentType) { - final RSet bucket = client.getSet(key, ByteArrayCodec.INSTANCE); - return completableFuture(bucket.removeRandomAsync().thenApply(bs -> bs == null ? null : decryptValue(key, cryptor, componentType, bs))); - } - - @Override - public CompletableFuture> spopAsync(String key, int count, Type componentType) { - final RSet bucket = client.getSet(key, ByteArrayCodec.INSTANCE); - return completableFuture(bucket.removeRandomAsync(count).thenApply((Set bslist) -> { - if (bslist == null || bslist.isEmpty()) { - return new LinkedHashSet(); - } - Set rs = new LinkedHashSet<>(); - for (byte[] bs : bslist) { - rs.add(decryptValue(key, cryptor, componentType, bs)); - } - return rs; - })); - } - - @Override - public CompletableFuture spopStringAsync(String key) { - final RSet bucket = client.getSet(key, StringCodec.INSTANCE); - return completableFuture(key, cryptor, bucket.removeRandomAsync()); - } - - @Override - public CompletableFuture> spopStringAsync(String key, int count) { - final RSet bucket = client.getSet(key, StringCodec.INSTANCE); - return completableFuture(bucket.removeRandomAsync(count).thenApply(r -> { - if (r == null) { - return r; - } - if (cryptor == null) { - return new LinkedHashSet<>(r); - } - Set rs = new LinkedHashSet<>(); - for (Object item : r) { - rs.add(item == null ? null : decryptValue(key, cryptor, item.toString())); - } - return rs; - })); - } - - @Override - public CompletableFuture spopLongAsync(String key) { - final RSet bucket = client.getSet(key, org.redisson.client.codec.LongCodec.INSTANCE); - return completableFuture(bucket.removeRandomAsync()); - } - - @Override - public CompletableFuture> spopLongAsync(String key, int count) { - final RSet bucket = client.getSet(key, org.redisson.client.codec.LongCodec.INSTANCE); - return completableFuture(bucket.removeRandomAsync(count)); - } - - @Override - public void sadd(String key, final Type componentType, T value) { - final RSet bucket = client.getSet(key, ByteArrayCodec.INSTANCE); - bucket.add(componentType == String.class ? encryptValue(key, cryptor, String.valueOf(value)).getBytes(StandardCharsets.UTF_8) : encryptValue(key, cryptor, componentType, convert, value)); - } - - @Override - public T spop(String key, final Type componentType) { - final RSet bucket = client.getSet(key, ByteArrayCodec.INSTANCE); - byte[] bs = bucket.removeRandom(); - return decryptValue(key, cryptor, componentType, bs); - } - - @Override - public Set spop(String key, int count, final Type componentType) { - final RSet bucket = client.getSet(key, ByteArrayCodec.INSTANCE); - Set< byte[]> bslist = bucket.removeRandom(count); - Set rs = new LinkedHashSet<>(); - if (bslist == null) { - return rs; - } - for (byte[] bs : bslist) { - rs.add(decryptValue(key, cryptor, componentType, bs)); - } - return rs; - } - - @Override - public String spopString(String key) { - final RSet bucket = client.getSet(key, StringCodec.INSTANCE); - return decryptValue(key, cryptor, bucket.removeRandom()); - } - - @Override - public Set spopString(String key, int count) { - final RSet bucket = client.getSet(key, StringCodec.INSTANCE); - Set r = bucket.removeRandom(count); - if (cryptor == null) { - return r; - } - Set rs = new LinkedHashSet<>(); - for (String item : r) { - rs.add(decryptValue(key, cryptor, item)); - } - return rs; - } - - @Override - public Long spopLong(String key) { - final RSet bucket = client.getSet(key, org.redisson.client.codec.LongCodec.INSTANCE); - return bucket.removeRandom(); - } - - @Override - public Set spopLong(String key, int count) { - final RSet bucket = client.getSet(key, org.redisson.client.codec.LongCodec.INSTANCE); - return bucket.removeRandom(count); - } - - @Override - public CompletableFuture saddStringAsync(String key, String value) { - final RSet bucket = client.getSet(key, StringCodec.INSTANCE); - return completableFuture(bucket.addAsync(encryptValue(key, cryptor, value)).thenApply(r -> null)); - } - - @Override - public void saddString(String key, String value) { - final RSet bucket = client.getSet(key, StringCodec.INSTANCE); - bucket.add(encryptValue(key, cryptor, value)); - } - - @Override - public CompletableFuture saddLongAsync(String key, long value) { - final RSet bucket = client.getSet(key, org.redisson.client.codec.LongCodec.INSTANCE); - return completableFuture(bucket.addAsync(value).thenApply(r -> null)); - } - - @Override - public void saddLong(String key, long value) { - final RSet bucket = client.getSet(key, org.redisson.client.codec.LongCodec.INSTANCE); - bucket.add(value); - } - - //--------------------- srem ------------------------------ - @Override - public CompletableFuture sremAsync(String key, final Type componentType, T value) { - return completableFuture(client.getSet(key, ByteArrayCodec.INSTANCE).removeAsync(encryptValue(key, cryptor, componentType, convert, value)).thenApply(r -> r ? 1 : 0)); - } - - @Override - public int srem(String key, final Type componentType, T value) { - final RSet bucket = client.getSet(key, ByteArrayCodec.INSTANCE); - return bucket.remove(componentType == String.class ? String.valueOf(value).getBytes(StandardCharsets.UTF_8) : encryptValue(key, cryptor, componentType, convert, value)) ? 1 : 0; - } - - @Override - public CompletableFuture sremStringAsync(String key, String value) { - return completableFuture(client.getSet(key, StringCodec.INSTANCE).removeAsync(encryptValue(key, cryptor, value)).thenApply(r -> r ? 1 : 0)); - } - - @Override - public int sremString(String key, String value) { - return client.getSet(key, StringCodec.INSTANCE).remove(encryptValue(key, cryptor, value)) ? 1 : 0; - } - - @Override - public CompletableFuture sremLongAsync(String key, long value) { - return completableFuture(client.getSet(key, org.redisson.client.codec.LongCodec.INSTANCE).removeAsync(value).thenApply(r -> r ? 1 : 0)); - } - - @Override - public int sremLong(String key, long value) { - return client.getSet(key, org.redisson.client.codec.LongCodec.INSTANCE).remove(value) ? 1 : 0; - } - - //--------------------- keys ------------------------------ - @Override - public List keys(String pattern) { - if (pattern == null || pattern.isEmpty()) { - return client.getKeys().getKeysStream().collect(Collectors.toList()); - } else { - return client.getKeys().getKeysStreamByPattern(pattern).collect(Collectors.toList()); - } - } - - @Override - public byte[] getBytes(final String key) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - return bucket.get(); - } - - @Override - public byte[] getSetBytes(final String key, byte[] value) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - return bucket.getAndSet(value); - } - - @Override - public byte[] getexBytes(final String key, final int expireSeconds) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - return bucket.getAndExpire(Duration.ofSeconds(expireSeconds)); - } - - @Override - public void setBytes(final String key, final byte[] value) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - bucket.set(value); - } - - @Override - public void setexBytes(final String key, final int expireSeconds, final byte[] value) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - bucket.set(value, expireSeconds, TimeUnit.SECONDS); - } - - @Override - public boolean setnxexBytes(final String key, final int expireSeconds, final byte[] value) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - return bucket.setIfAbsent(value, Duration.ofSeconds(expireSeconds)); - } - - @Override - public CompletableFuture getBytesAsync(final String key) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - return completableFuture(bucket.getAsync()); - } - - @Override - public CompletableFuture getSetBytesAsync(final String key, byte[] value) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - return completableFuture(bucket.getAndSetAsync(value)); - } - - @Override - public CompletableFuture getexBytesAsync(final String key, final int expireSeconds) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - return completableFuture(bucket.getAndExpireAsync(Duration.ofSeconds(expireSeconds))); - } - - @Override - public CompletableFuture setBytesAsync(final String key, final byte[] value) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - return completableFuture(bucket.setAsync(value)); - } - - @Override - public CompletableFuture setexBytesAsync(final String key, final int expireSeconds, final byte[] value) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - return completableFuture(bucket.setAsync(value, expireSeconds, TimeUnit.SECONDS).thenApply(v -> null)); - } - - @Override - public CompletableFuture setnxexBytesAsync(final String key, final int expireSeconds, final byte[] value) { - final RBucket bucket = client.getBucket(key, ByteArrayCodec.INSTANCE); - return completableFuture(bucket.setIfAbsentAsync(value, Duration.ofSeconds(expireSeconds))); - } - - @Override - public CompletableFuture> keysAsync(String pattern) { - return CompletableFuture.supplyAsync(() -> keys(pattern)); - } - - //--------------------- dbsize ------------------------------ - @Override - public long dbsize() { - return client.getKeys().count(); - } - - @Override - public CompletableFuture dbsizeAsync() { - return completableFuture(client.getKeys().countAsync()); - } - - protected static class MapByteArrayCodec extends ByteArrayCodec { - - public static final MapByteArrayCodec instance = new MapByteArrayCodec(); - - @Override - public org.redisson.client.protocol.Decoder getMapKeyDecoder() { - return StringCodec.INSTANCE.getValueDecoder(); - } - - @Override - public org.redisson.client.protocol.Encoder getMapKeyEncoder() { - return StringCodec.INSTANCE.getValueEncoder(); - } - } - - protected static class MapStringCodec extends StringCodec { - - public static final MapStringCodec instance = new MapStringCodec(); - - @Override - public org.redisson.client.protocol.Decoder getMapKeyDecoder() { - return StringCodec.INSTANCE.getValueDecoder(); - } - - @Override - public org.redisson.client.protocol.Encoder getMapKeyEncoder() { - return StringCodec.INSTANCE.getValueEncoder(); - } - } - - protected static class MapLongCodec extends org.redisson.client.codec.LongCodec { - - public static final MapLongCodec instance = new MapLongCodec(); - - @Override - public org.redisson.client.protocol.Decoder getMapKeyDecoder() { - return StringCodec.INSTANCE.getValueDecoder(); - } - - @Override - public org.redisson.client.protocol.Encoder getMapKeyEncoder() { - return StringCodec.INSTANCE.getValueEncoder(); - } - } - - protected static class MapDoubleCodec extends org.redisson.client.codec.DoubleCodec { - - public static final MapLongCodec instance = new MapLongCodec(); - - @Override - public org.redisson.client.protocol.Decoder getMapKeyDecoder() { - return StringCodec.INSTANCE.getValueDecoder(); - } - - @Override - public org.redisson.client.protocol.Encoder getMapKeyEncoder() { - return StringCodec.INSTANCE.getValueEncoder(); - } - } - - @Override - public CompletableFuture>> getCollectionMapAsync(final boolean set, final Type componentType, final String... keys) { - final CompletableFuture>> rsFuture = new CompletableFuture<>(); - final Map> map = new LinkedHashMap<>(); - final ReentrantLock mapLock = new ReentrantLock(); - final CompletableFuture[] futures = new CompletableFuture[keys.length]; - if (!set) { //list - for (int i = 0; i < keys.length; i++) { - final String key = keys[i]; - futures[i] = completableFuture(client.getList(key, ByteArrayCodec.INSTANCE).readAllAsync().thenApply(list -> { - if (list == null || list.isEmpty()) { - return list; - } - List rs = new ArrayList<>(); - for (Object item : list) { - byte[] bs = (byte[]) item; - if (bs == null) { - rs.add(null); - } else { - rs.add(decryptValue(key, cryptor, componentType, bs)); - } - } - mapLock.lock(); - try { - map.put(key, rs); - } finally { - mapLock.unlock(); - } - return rs; - })); - } - } else { - for (int i = 0; i < keys.length; i++) { - final String key = keys[i]; - futures[i] = completableFuture(client.getSet(key, ByteArrayCodec.INSTANCE).readAllAsync().thenApply(list -> { - if (list == null || list.isEmpty()) { - return list; - } - List rs = new ArrayList<>(); - for (Object item : list) { - byte[] bs = (byte[]) item; - if (bs == null) { - rs.add(null); - } else { - rs.add(decryptValue(key, cryptor, componentType, bs)); - } - } - mapLock.lock(); - try { - map.put(key, rs); - } finally { - mapLock.unlock(); - } - return rs; - })); - } - } - CompletableFuture.allOf(futures).whenComplete((w, e) -> { - if (e != null) { - rsFuture.completeExceptionally(e); - } else { - rsFuture.complete(map); - } - }); - return rsFuture; - } - - @Override - public CompletableFuture getCollectionSizeAsync(String key) { - return completableFuture(client.getScript().evalAsync(RScript.Mode.READ_ONLY, "return redis.call('TYPE', '" + key + "')", RScript.ReturnType.VALUE).thenCompose(type -> { - if (String.valueOf(type).contains("list")) { - return client.getList(key).sizeAsync(); - } else { - return client.getSet(key).sizeAsync(); - } - })); - } - - @Override - public int getCollectionSize(String key) { - String type = client.getScript().eval(RScript.Mode.READ_ONLY, "return redis.call('TYPE', '" + key + "')", RScript.ReturnType.VALUE); - if (String.valueOf(type).contains("list")) { - return client.getList(key).size(); - } else { - return client.getSet(key).size(); - } - } - - @Override - public CompletableFuture> getCollectionAsync(String key, final Type componentType) { - return completableFuture(client.getScript().evalAsync(RScript.Mode.READ_ONLY, "return redis.call('TYPE', '" + key + "')", RScript.ReturnType.VALUE).thenCompose(type -> { - if (String.valueOf(type).contains("list")) { - return (CompletionStage) client.getList(key, ByteArrayCodec.INSTANCE).readAllAsync().thenApply(list -> { - if (list == null || list.isEmpty()) { - return list; - } - List rs = new ArrayList<>(); - for (Object item : list) { - byte[] bs = (byte[]) item; - if (bs == null) { - rs.add(null); - } else { - rs.add(componentType == String.class ? (T) decryptValue(key, cryptor, new String(bs, StandardCharsets.UTF_8)) : (T) decryptValue(key, cryptor, componentType, bs)); - } - } - return rs; - }); - } else { - return (CompletionStage) client.getSet(key, ByteArrayCodec.INSTANCE).readAllAsync().thenApply(set -> { - if (set == null || set.isEmpty()) { - return set; - } - Set rs = new LinkedHashSet<>(); - for (Object item : set) { - byte[] bs = (byte[]) item; - if (bs == null) { - rs.add(null); - } else { - rs.add(componentType == String.class ? (T) decryptValue(key, cryptor, new String(bs, StandardCharsets.UTF_8)) : (T) decryptValue(key, cryptor, componentType, bs)); - } - } - return rs; - }); - } - })); - } - - @Override - public CompletableFuture getLongArrayAsync(String... keys) { - return completableFuture(client.getBuckets(org.redisson.client.codec.LongCodec.INSTANCE).getAsync(keys).thenApply(map -> { - Long[] rs = new Long[keys.length]; - for (int i = 0; i < rs.length; i++) { - rs[i] = (Long) map.get(keys[i]); - } - return rs; - })); - } - - @Override - public CompletableFuture getStringArrayAsync(String... keys) { - return completableFuture(client.getBuckets(StringCodec.INSTANCE).getAsync(keys).thenApply(map -> { - String[] rs = new String[keys.length]; - for (int i = 0; i < rs.length; i++) { - rs[i] = decryptValue(keys[i], cryptor, (String) map.get(keys[i])); - } - return rs; - })); - } - - @Override - public CompletableFuture> getStringCollectionAsync(String key) { - return completableFuture(client.getScript().evalAsync(RScript.Mode.READ_ONLY, "return redis.call('TYPE', '" + key + "')", RScript.ReturnType.VALUE).thenCompose(type -> { - if (String.valueOf(type).contains("list")) { - return (CompletionStage) client.getList(key, StringCodec.INSTANCE).readAllAsync().thenApply(list -> { - if (list == null || list.isEmpty() || cryptor == null) { - return list; - } - List rs = new ArrayList<>(); - for (Object item : list) { - rs.add(item == null ? null : decryptValue(key, cryptor, item.toString())); - } - return rs; - }); - } else { - return (CompletionStage) client.getSet(key, StringCodec.INSTANCE).readAllAsync().thenApply(set -> { - if (set == null) { - return set; - } - if (set.isEmpty() || cryptor == null) { - return new ArrayList<>(set); - } - List rs = new ArrayList<>(); //不用set - for (Object item : set) { - rs.add(item == null ? null : decryptValue(key, cryptor, item.toString())); - } - return rs; - }); - } - })); - } - - @Override - public CompletableFuture>> getStringCollectionMapAsync(final boolean set, String... keys) { - final CompletableFuture>> rsFuture = new CompletableFuture<>(); - final Map> map = new LinkedHashMap<>(); - final ReentrantLock mapLock = new ReentrantLock(); - final CompletableFuture[] futures = new CompletableFuture[keys.length]; - if (!set) { //list - for (int i = 0; i < keys.length; i++) { - final String key = keys[i]; - futures[i] = completableFuture(client.getList(key, StringCodec.INSTANCE).readAllAsync().thenApply((Collection r) -> { - if (r != null) { - if (cryptor != null && !r.isEmpty()) { - List rs = new ArrayList<>(); - for (Object item : r) { - rs.add(item == null ? null : decryptValue(key, cryptor, item.toString())); - } - r = rs; - } - mapLock.lock(); - try { - map.put(key, r); - } finally { - mapLock.unlock(); - } - } - return null; - })); - } - } else { - for (int i = 0; i < keys.length; i++) { - final String key = keys[i]; - futures[i] = completableFuture(client.getSet(key, StringCodec.INSTANCE).readAllAsync().thenApply((Collection r) -> { - if (r != null) { - boolean changed = false; - if (cryptor != null && !r.isEmpty()) { - List rs = new ArrayList<>(); - for (Object item : r) { - rs.add(item == null ? null : decryptValue(key, cryptor, item.toString())); - } - r = rs; - changed = true; - } - mapLock.lock(); - try { - map.put(key, changed ? r : new ArrayList(r)); - } finally { - mapLock.unlock(); - } - } - return null; - })); - } - } - CompletableFuture.allOf(futures).whenComplete((w, e) -> { - if (e != null) { - rsFuture.completeExceptionally(e); - } else { - rsFuture.complete(map); - } - }); - return rsFuture; - } - - @Override - public Collection getStringCollection(String key) { - return getStringCollectionAsync(key).join(); - } - - @Override - public Map> getStringCollectionMap(final boolean set, String... keys) { - return getStringCollectionMapAsync(set, keys).join(); - } - - @Override - public CompletableFuture> getLongCollectionAsync(String key) { - return completableFuture(client.getScript().evalAsync(RScript.Mode.READ_ONLY, "return redis.call('TYPE', '" + key + "')", RScript.ReturnType.VALUE).thenCompose(type -> { - if (String.valueOf(type).contains("list")) { - return (CompletionStage) client.getList(key, org.redisson.client.codec.LongCodec.INSTANCE).readAllAsync(); - } else { - return (CompletionStage) client.getSet(key, org.redisson.client.codec.LongCodec.INSTANCE).readAllAsync().thenApply(s -> s == null ? null : new ArrayList(s)); - } - })); - } - - @Override - public CompletableFuture>> getLongCollectionMapAsync(final boolean set, String... keys) { - final CompletableFuture>> rsFuture = new CompletableFuture<>(); - final Map> map = new LinkedHashMap<>(); - final ReentrantLock mapLock = new ReentrantLock(); - final CompletableFuture[] futures = new CompletableFuture[keys.length]; - if (!set) { //list - for (int i = 0; i < keys.length; i++) { - final String key = keys[i]; - futures[i] = completableFuture(client.getList(key, org.redisson.client.codec.LongCodec.INSTANCE).readAllAsync().thenApply(r -> { - if (r != null) { - mapLock.lock(); - try { - map.put(key, (Collection) r); - } finally { - mapLock.unlock(); - } - } - return null; - })); - } - } else { - for (int i = 0; i < keys.length; i++) { - final String key = keys[i]; - futures[i] = completableFuture(client.getSet(key, org.redisson.client.codec.LongCodec.INSTANCE).readAllAsync().thenApply(r -> { - if (r != null) { - mapLock.lock(); - try { - map.put(key, new ArrayList(r)); - } finally { - mapLock.unlock(); - } - } - return null; - })); - } - } - CompletableFuture.allOf(futures).whenComplete((w, e) -> { - if (e != null) { - rsFuture.completeExceptionally(e); - } else { - rsFuture.complete(map); - } - }); - return rsFuture; - } - - @Override - public Collection getLongCollection(String key) { - return getLongCollectionAsync(key).join(); - } - - @Override - public Map> getLongCollectionMap(final boolean set, String... keys) { - return getLongCollectionMapAsync(set, keys).join(); - } - - //--------------------- getexCollection ------------------------------ - @Override - public CompletableFuture> getexCollectionAsync(String key, int expireSeconds, final Type componentType) { - return (CompletableFuture) expireAsync(key, expireSeconds).thenCompose(v -> getCollectionAsync(key, componentType)); - } - - @Override - public Collection getexCollection(String key, final int expireSeconds, final Type componentType) { - return (Collection) getexCollectionAsync(key, expireSeconds, componentType).join(); - } - - @Override - public CompletableFuture> getexStringCollectionAsync(String key, int expireSeconds) { - return (CompletableFuture) expireAsync(key, expireSeconds).thenCompose(v -> getStringCollectionAsync(key)); - } - - @Override - public Collection getexStringCollection(String key, final int expireSeconds) { - return getexStringCollectionAsync(key, expireSeconds).join(); - } - - @Override - public CompletableFuture> getexLongCollectionAsync(String key, int expireSeconds) { - return (CompletableFuture) expireAsync(key, expireSeconds).thenCompose(v -> getLongCollectionAsync(key)); - } - - @Override - public Collection getexLongCollection(String key, final int expireSeconds) { - return getexLongCollectionAsync(key, expireSeconds).join(); - } - - @Override - public Collection getCollection(String key, final Type componentType) { - return (Collection) getCollectionAsync(key, componentType).join(); - } - - @Override - public Long[] getLongArray(final String... keys) { - Map map = client.getBuckets(org.redisson.client.codec.LongCodec.INSTANCE).get(keys); - Long[] rs = new Long[keys.length]; - for (int i = 0; i < rs.length; i++) { - rs[i] = map.get(keys[i]); - } - return rs; - } - - @Override - public String[] getStringArray(final String... keys) { - Map map = client.getBuckets(StringCodec.INSTANCE).get(keys); - String[] rs = new String[keys.length]; - for (int i = 0; i < rs.length; i++) { - rs[i] = decryptValue(keys[i], cryptor, map.get(keys[i])); - } - return rs; - } - - @Override - public Map> getCollectionMap(final boolean set, final Type componentType, String... keys) { - return (Map) getCollectionMapAsync(set, componentType, keys).join(); - } - -} diff --git a/src/org/redkalex/cache/redis/redission/RedissionCacheSourceProvider.java b/src/org/redkalex/cache/redis/redission/RedissionCacheSourceProvider.java deleted file mode 100644 index 9242b4b..0000000 --- a/src/org/redkalex/cache/redis/redission/RedissionCacheSourceProvider.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package org.redkalex.cache.redis.redission; - -import org.redkale.annotation.Priority; -import org.redkale.source.CacheSource; -import org.redkale.source.CacheSourceProvider; -import org.redkale.util.AnyValue; - -/** - * - * @author zhangjx - */ -@Priority(-300) -public class RedissionCacheSourceProvider implements CacheSourceProvider { - - @Override - public boolean acceptsConf(AnyValue config) { - try { - Object.class.isAssignableFrom(org.redisson.config.Config.class); //试图加载Redission相关类 - return new RedissionCacheSource().acceptsConf(config); - } catch (Throwable e) { - return false; - } - } - - @Override - public CacheSource createInstance() { - return new RedissionCacheSource(); - } - -} diff --git a/src/org/redkalex/cache/redis/vertx/RedisVertxCacheSource.java b/src/org/redkalex/cache/redis/vertx/RedisVertxCacheSource.java deleted file mode 100644 index 7f7ac49..0000000 --- a/src/org/redkalex/cache/redis/vertx/RedisVertxCacheSource.java +++ /dev/null @@ -1,1781 +0,0 @@ -/* - */ -package org.redkalex.cache.redis.vertx; - -import com.zdemo.cachex.AbstractRedisSource; -import com.zdemo.cachex.RedisCryptor; -import io.vertx.core.*; -import io.vertx.redis.client.*; -import org.redkale.annotation.AutoLoad; -import org.redkale.annotation.ResourceListener; -import org.redkale.annotation.ResourceType; -import org.redkale.convert.Convert; -import org.redkale.convert.json.JsonConvert; -import org.redkale.service.Local; -import org.redkale.source.CacheSource; -import org.redkale.util.*; - -import java.io.Serializable; -import java.lang.reflect.Type; -import java.net.URI; -import java.nio.charset.StandardCharsets; -import java.util.*; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.locks.ReentrantLock; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * - * @author zhangjx - */ -@Local -@AutoLoad(false) -@ResourceType(CacheSource.class) -public class RedisVertxCacheSource extends AbstractRedisSource { - - protected final Logger logger = Logger.getLogger(this.getClass().getSimpleName()); - - protected Type objValueType = String.class; - - protected List nodeAddrs; - - protected Vertx vertx; - - protected io.vertx.redis.client.RedisAPI client; - - @Override - public void init(AnyValue conf) { - super.init(conf); - if (conf == null) { - conf = AnyValue.create(); - } - initClient(conf); - } - - private void initClient(AnyValue conf) { - String password = null; - int urlmaxconns = Utility.cpus(); - List addrs = new ArrayList<>(); - for (AnyValue node : getNodes(conf)) { - String addrstr = node.getValue(CACHE_SOURCE_URL); - addrs.add(addrstr); - password = node.getValue(CACHE_SOURCE_PASSWORD, null); - if (db < 0) { - String db0 = node.getValue(CACHE_SOURCE_DB, "").trim(); - if (!db0.isEmpty()) { - db = Integer.valueOf(db0); - } - } - URI uri = URI.create(addrstr); - if (uri.getQuery() != null && !uri.getQuery().isEmpty()) { - String[] qrys = uri.getQuery().split("&|="); - for (int i = 0; i < qrys.length; i += 2) { - if (CACHE_SOURCE_MAXCONNS.equals(qrys[i])) { - urlmaxconns = i == qrys.length - 1 ? Utility.cpus() : Integer.parseInt(qrys[i + 1]); - } - } - } - } - int maxconns = conf.getIntValue(CACHE_SOURCE_MAXCONNS, urlmaxconns); - //Redis链接 - RedisOptions redisConfig = new RedisOptions(); - if (maxconns > 0) { - redisConfig.setMaxPoolSize(maxconns); - } - if (password != null) { - redisConfig.setPassword(password.trim()); - } - if (maxconns > 0) { - redisConfig.setMaxPoolWaiting(maxconns != Utility.cpus() ? maxconns : maxconns * 10); - } - redisConfig.setEndpoints(addrs); - if (this.vertx == null) { - this.vertx = Vertx.vertx(new VertxOptions().setWorkerPoolSize(Utility.cpus()).setPreferNativeTransport(true)); - } - RedisAPI old = this.client; - this.client = RedisAPI.api(Redis.createClient(this.vertx, redisConfig)); - if (old != null) { - old.close(); - } - } - - @Override - @ResourceListener - public void onResourceChange(ResourceEvent[] events) { - if (events == null || events.length < 1) { - return; - } - StringBuilder sb = new StringBuilder(); - for (ResourceEvent event : events) { - sb.append("CacheSource(name=").append(resourceName()).append(") change '").append(event.name()).append("' to '").append(event.coverNewValue()).append("'\r\n"); - } - initClient(this.config); - if (sb.length() > 0) { - logger.log(Level.INFO, sb.toString()); - } - } - - public boolean acceptsConf(AnyValue config) { - if (config == null) { - return false; - } - AnyValue[] nodes = getNodes(config); - if (nodes == null || nodes.length == 0) { - return false; - } - for (AnyValue node : nodes) { - String val = node.getValue(CACHE_SOURCE_URL); - if (val != null && val.startsWith("redis://")) { - return true; - } - if (val != null && val.startsWith("rediss://")) { - return true; - } - } - return false; - } - - protected AnyValue[] getNodes(AnyValue config) { - AnyValue[] nodes = config.getAnyValues(CACHE_SOURCE_NODE); - if (nodes == null || nodes.length == 0) { - AnyValue one = config.getAnyValue(CACHE_SOURCE_NODE); - if (one == null) { - String val = config.getValue(CACHE_SOURCE_URL); - if (val == null) { - return nodes; - } - nodes = new AnyValue[]{config}; - } else { - nodes = new AnyValue[]{one}; - } - } - return nodes; - } - - @Override - public final String getType() { - return "redis"; - } - - @Override - public String toString() { - return getClass().getSimpleName() + "{addrs=" + this.nodeAddrs + ", db=" + this.db + "}"; - } - - @Override - public void destroy(AnyValue conf) { - super.destroy(conf); - if (client != null) { - client.close(); - } - if (this.vertx != null) { - this.vertx.close(); - } - } - - protected CompletableFuture completableFuture(io.vertx.core.Future rf) { - return rf.toCompletionStage().toCompletableFuture(); - } - - protected CompletableFuture sendAsync(Command cmd, String... args) { - return completableFuture(redisAPI().send(cmd, args)); - } - - protected RedisAPI redisAPI() { - return client; - } - - protected Long orElse(Long v, long def) { - return v == null ? def : v; - } - - protected Integer orElse(Integer v, int def) { - return v == null ? def : v; - } - - protected Boolean getBooleanValue(Response resp) { - if (resp == null) { - return false; - } - Boolean v = resp.toBoolean(); - return v == null ? false : v; - } - - protected String getStringValue(String key, RedisCryptor cryptor, Response resp) { - if (resp == null) { - return null; - } - String val = resp.toString(StandardCharsets.UTF_8); - if (cryptor == null) { - return val; - } - return cryptor.decrypt(key, val); - } - - protected Long getLongValue(Response resp, long defvalue) { - if (resp == null) { - return defvalue; - } - Long v = resp.toLong(); - return v == null ? defvalue : v; - } - - protected Double getDoubleValue(Response resp, double defvalue) { - if (resp == null) { - return defvalue; - } - Double v = resp.toDouble(); - return v == null ? defvalue : v; - } - - protected Integer getIntValue(Response resp, int defvalue) { - if (resp == null) { - return defvalue; - } - Integer v = resp.toInteger(); - return v == null ? defvalue : v; - } - - protected Boolean getBoolValue(Response resp) { - if (resp == null) { - return false; - } - Integer v = resp.toInteger(); - return v == null ? false : v > 0; - } - - protected T getObjectValue(String key, RedisCryptor cryptor, String bs, Type type) { - if (bs == null) { - return null; - } - if (type == byte[].class) { - return (T) bs.getBytes(StandardCharsets.UTF_8); - } - if (type == String.class) { - return (T) decryptValue(key, cryptor, bs); - } - if (type == long.class) { - return (T) (Long) Long.parseLong(bs); - } - return (T) JsonConvert.root().convertFrom(type, decryptValue(key, cryptor, bs)); - } - - protected T getObjectValue(String key, RedisCryptor cryptor, Response resp, Type type) { - return getObjectValue(key, cryptor, resp == null ? null : resp.toString(StandardCharsets.UTF_8), type); - } - - protected Collection getCollectionValue(String key, RedisCryptor cryptor, Response resp, boolean set, Type type) { - int size = resp == null ? 0 : resp.size(); - if (size == 0) { - return set ? new LinkedHashSet<>() : new ArrayList<>(); - } - Collection list = set ? new LinkedHashSet<>() : new ArrayList<>(); - for (int i = 0; i < size; i++) { - list.add(getObjectValue(key, cryptor, resp.get(i), type)); - } - return list; - } - - protected Map getMapValue(String key, RedisCryptor cryptor, Response gresp, Type type) { - int gsize = gresp.size(); - if (gsize == 0) { - return new LinkedHashMap<>(); - } - Map map = new LinkedHashMap<>(); - //resp.tostring = [0, [key1, 10, key2, 30]] - for (int j = 0; j < gsize; j++) { - Response resp = gresp.get(j); - if (resp.type() != ResponseType.MULTI) { - continue; - } - int size = resp.size(); - for (int i = 0; i < size; i += 2) { - String bs1 = resp.get(i).toString(StandardCharsets.UTF_8); - String bs2 = resp.get(i + 1).toString(StandardCharsets.UTF_8); - T val = getObjectValue(key, cryptor, bs2, type); - if (val != null) { - map.put(getObjectValue(key, cryptor, bs1, String.class).toString(), val); - } - } - } - return map; - } - - protected String[] keyArgs(boolean set, String key) { - if (set) { - return new String[]{key}; - } - return new String[]{key, "0", "-1"}; - } - - protected String formatValue(long value) { - return String.valueOf(value); - } - - protected String formatValue(String key, RedisCryptor cryptor, String value) { - return encryptValue(key, cryptor, value); - } - - protected String formatValue(String key, RedisCryptor cryptor, Object value) { - return formatValue(key, cryptor, null, null, value); - } - - protected String formatValue(String key, RedisCryptor cryptor, Convert convert0, Type type, Object value) { - if (value == null) { - throw new NullPointerException(); - } - if (value instanceof byte[]) { - return new String((byte[]) value, StandardCharsets.UTF_8); - } - if (convert0 == null) { - if (convert == null) { - convert = JsonConvert.root(); //compile模式下convert可能为null - } - convert0 = convert; - } - if (type == null) { - type = value.getClass(); - } - Class clz = value.getClass(); - if (clz == String.class || clz == Long.class - || Number.class.isAssignableFrom(clz) || CharSequence.class.isAssignableFrom(clz)) { - return String.valueOf(value); - } - String val = (convert0 instanceof JsonConvert) ? ((JsonConvert) convert0).convertTo(type, value) : new String(convert0.convertToBytes(type, value), StandardCharsets.UTF_8); - return encryptValue(key, cryptor, val); - } - - //--------------------- exists ------------------------------ - @Override - public CompletableFuture existsAsync(String key) { - return sendAsync(Command.EXISTS, key).thenApply(v -> getBooleanValue(v)); - } - - @Override - public boolean exists(String key) { - return existsAsync(key).join(); - } - - //--------------------- get ------------------------------ - @Override - public CompletableFuture getAsync(String key, Type type) { - return sendAsync(Command.GET, key).thenApply(v -> getObjectValue(key, cryptor, v, type)); - } - - @Override - public CompletableFuture getStringAsync(String key) { - return sendAsync(Command.GET, key).thenApply(v -> getStringValue(key, cryptor, v)); - } - - @Override - public CompletableFuture getLongAsync(String key, long defValue) { - return sendAsync(Command.GET, key).thenApply(v -> getLongValue(v, defValue)); - } - - @Override - public T get(String key, final Type type) { - return (T) getAsync(key, type).join(); - } - - @Override - public String getString(String key) { - return getStringAsync(key).join(); - } - - @Override - public String getSetString(String key, String value) { - return getSetStringAsync(key, value).join(); - } - - @Override - public long getLong(String key, long defValue) { - return getLongAsync(key, defValue).join(); - } - - @Override - public long getSetLong(String key, long value, long defValue) { - return getSetLongAsync(key, value, defValue).join(); - } - - //--------------------- getex ------------------------------ - @Override - public CompletableFuture getexAsync(String key, int expireSeconds, final Type type) { - return sendAsync(Command.GETEX, key, "EX", String.valueOf(expireSeconds)).thenApply(v -> getObjectValue(key, cryptor, v, type)); - } - - @Override - public T getex(String key, final int expireSeconds, final Type type) { - return (T) getexAsync(key, expireSeconds, type).join(); - } - - @Override - public CompletableFuture getexStringAsync(String key, int expireSeconds) { - return sendAsync(Command.GETEX, key, "EX", String.valueOf(expireSeconds)).thenApply(v -> getStringValue(key, cryptor, v)); - } - - @Override - public String getexString(String key, final int expireSeconds) { - return getexStringAsync(key, expireSeconds).join(); - } - - @Override - public CompletableFuture getexLongAsync(String key, int expireSeconds, long defValue) { - return sendAsync(Command.GETEX, key, "EX", String.valueOf(expireSeconds)).thenApply(v -> getLongValue(v, defValue)); - } - - @Override - public long getexLong(String key, final int expireSeconds, long defValue) { - return getexLongAsync(key, expireSeconds, defValue).join(); - } - - @Override - public void mset(final Object... keyVals) { - msetAsync(keyVals).join(); - } - - @Override - public void mset(final Map map) { - msetAsync(map).join(); - } - - //--------------------- set ------------------------------ - @Override - public CompletableFuture msetAsync(final Object... keyVals) { - if (keyVals.length % 2 != 0) { - throw new RedkaleException("key value must be paired"); - } - String[] args = new String[keyVals.length]; - for (int i = 0; i < keyVals.length; i += 2) { - String key = keyVals[i].toString(); - Object val = keyVals[i + 1]; - args[i] = key; - args[i + 1] = formatValue(key, cryptor, convert, val.getClass(), val); - } - return sendAsync(Command.MSET, args).thenApply(v -> null); - } - - @Override - public CompletableFuture msetAsync(final Map map) { - if (map == null || map.isEmpty()) { - return CompletableFuture.completedFuture(null); - } - List bs = new ArrayList<>(); - map.forEach((key, val) -> { - bs.add(key.toString()); - bs.add(formatValue(key.toString(), cryptor, convert, val.getClass(), val)); - }); - return sendAsync(Command.MSET, bs.toArray(new String[bs.size()])).thenApply(v -> null); - } - - @Override - public CompletableFuture setAsync(String key, final Type type, T value) { - return sendAsync(Command.SET, key, formatValue(key, cryptor, (Convert) null, type, value)).thenApply(v -> null); - } - - @Override - public CompletableFuture setAsync(String key, Convert convert, final Type type, T value) { - return sendAsync(Command.SET, key, formatValue(key, cryptor, convert, type, value)).thenApply(v -> null); - } - - @Override - public CompletableFuture setnxAsync(String key, final Type type, T value) { - return sendAsync(Command.SETNX, key, formatValue(key, cryptor, (Convert) null, type, value)).thenApply(v -> getBoolValue(v)); - } - - @Override - public CompletableFuture setnxAsync(String key, Convert convert, final Type type, T value) { - return sendAsync(Command.SETNX, key, formatValue(key, cryptor, convert, type, value)).thenApply(v -> getBoolValue(v)); - } - - @Override - public CompletableFuture getSetAsync(String key, final Type type, T value) { - return sendAsync(Command.GETSET, key, formatValue(key, cryptor, convert, type, value)).thenApply(v -> getObjectValue(key, cryptor, v, type)); - } - - @Override - public CompletableFuture getSetAsync(String key, Convert convert0, final Type type, T value) { - return sendAsync(Command.GETSET, key, formatValue(key, cryptor, convert0, type, value)).thenApply(v -> getObjectValue(key, cryptor, v, type)); - } - - @Override - public void set(final String key, final Type type, T value) { - setAsync(key, type, value).join(); - } - - @Override - public void set(final String key, final Convert convert, final Type type, T value) { - setAsync(key, convert, type, value).join(); - } - - @Override - public boolean setnx(final String key, final Type type, T value) { - return setnxAsync(key, type, value).join(); - } - - @Override - public boolean setnx(final String key, final Convert convert, final Type type, T value) { - return setnxAsync(key, convert, type, value).join(); - } - - @Override - public T getSet(final String key, final Type type, T value) { - return getSetAsync(key, type, value).join(); - } - - @Override - public T getSet(String key, final Convert convert, final Type type, T value) { - return getSetAsync(key, convert, type, value).join(); - } - - @Override - public CompletableFuture setStringAsync(String key, String value) { - return sendAsync(Command.SET, key, formatValue(key, cryptor, value)).thenApply(v -> null); - } - - @Override - public CompletableFuture setnxStringAsync(String key, String value) { - return sendAsync(Command.SETNX, key, formatValue(key, cryptor, value)).thenApply(v -> getBoolValue(v)); - } - - @Override - public CompletableFuture getSetStringAsync(String key, String value) { - return sendAsync(Command.GETSET, key, formatValue(key, cryptor, value)).thenApply(v -> getStringValue(key, cryptor, v)); - } - - @Override - public void setString(String key, String value) { - setStringAsync(key, value).join(); - } - - @Override - public boolean setnxString(String key, String value) { - return setnxStringAsync(key, value).join(); - } - - @Override - public CompletableFuture setLongAsync(String key, long value) { - return sendAsync(Command.SET, key, formatValue(value)).thenApply(v -> null); - } - - @Override - public CompletableFuture setnxLongAsync(String key, long value) { - return sendAsync(Command.SETNX, key, formatValue(value)).thenApply(v -> getBoolValue(v)); - } - - @Override - public CompletableFuture getSetLongAsync(String key, long value, long defvalue) { - return sendAsync(Command.GETSET, key, formatValue(value)).thenApply(v -> getLongValue(v, defvalue)); - } - - @Override - public void setLong(String key, long value) { - setLongAsync(key, value).join(); - } - - @Override - public boolean setnxLong(String key, long value) { - return setnxLongAsync(key, value).join(); - } - - //--------------------- setex ------------------------------ - @Override - public CompletableFuture setexAsync(String key, int expireSeconds, final Type type, T value) { - return sendAsync(Command.SETEX, key, String.valueOf(expireSeconds), formatValue(key, cryptor, (Convert) null, type, value)).thenApply(v -> null); - } - - @Override - public CompletableFuture setexAsync(String key, int expireSeconds, Convert convert, final Type type, T value) { - return sendAsync(Command.SETEX, key, String.valueOf(expireSeconds), formatValue(key, cryptor, convert, type, value)).thenApply(v -> null); - } - - @Override - public CompletableFuture setnxexAsync(String key, int expireSeconds, final Type type, T value) { - return sendAsync(Command.SET, key, formatValue(key, cryptor, (Convert) null, type, value), "NX", "EX", String.valueOf(expireSeconds)).thenApply(v -> v != null && ("OK".equals(v.toString()) || v.toInteger() > 0)); - } - - @Override - public CompletableFuture setnxexAsync(String key, int expireSeconds, Convert convert, final Type type, T value) { - return sendAsync(Command.SET, key, formatValue(key, cryptor, convert, type, value), "NX", "EX", String.valueOf(expireSeconds)).thenApply(v -> v != null && ("OK".equals(v.toString()) || v.toInteger() > 0)); - } - - @Override - public void setex(String key, int expireSeconds, final Type type, T value) { - setexAsync(key, expireSeconds, type, value).join(); - } - - @Override - public void setex(String key, int expireSeconds, Convert convert, final Type type, T value) { - setexAsync(key, expireSeconds, convert, type, value).join(); - } - - @Override - public boolean setnxex(String key, int expireSeconds, final Type type, T value) { - return setnxexAsync(key, expireSeconds, type, value).join(); - } - - @Override - public boolean setnxex(String key, int expireSeconds, Convert convert, final Type type, T value) { - return setnxexAsync(key, expireSeconds, convert, type, value).join(); - } - - @Override - public CompletableFuture setexStringAsync(String key, int expireSeconds, String value) { - return sendAsync(Command.SETEX, key, String.valueOf(expireSeconds), formatValue(key, cryptor, value)).thenApply(v -> null); - } - - @Override - public CompletableFuture setnxexStringAsync(String key, int expireSeconds, String value) { - return sendAsync(Command.SET, key, formatValue(key, cryptor, value), "NX", "EX", String.valueOf(expireSeconds)).thenApply(v -> v != null && ("OK".equals(v.toString()) || v.toInteger() > 0)); - } - - @Override - public void setexString(String key, int expireSeconds, String value) { - setexStringAsync(key, expireSeconds, value).join(); - } - - @Override - public boolean setnxexString(String key, int expireSeconds, String value) { - return setnxexStringAsync(key, expireSeconds, value).join(); - } - - @Override - public CompletableFuture setexLongAsync(String key, int expireSeconds, long value) { - return sendAsync(Command.SETEX, key, String.valueOf(expireSeconds), formatValue(value)).thenApply(v -> null); - } - - @Override - public void setexLong(String key, int expireSeconds, long value) { - setexLongAsync(key, expireSeconds, value).join(); - } - - @Override - public CompletableFuture setnxexLongAsync(String key, int expireSeconds, long value) { - return sendAsync(Command.SET, key, formatValue(value), "NX", "EX", String.valueOf(expireSeconds)).thenApply(v -> v != null && ("OK".equals(v.toString()) || v.toInteger() > 0)); - } - - @Override - public boolean setnxexLong(String key, int expireSeconds, long value) { - return setnxexLongAsync(key, expireSeconds, value).join(); - } - - //--------------------- expire ------------------------------ - @Override - public CompletableFuture expireAsync(String key, int expireSeconds) { - return sendAsync(Command.EXPIRE, key, String.valueOf(expireSeconds)).thenApply(v -> null); - } - - @Override - public void expire(String key, int expireSeconds) { - expireAsync(key, expireSeconds).join(); - } - - //--------------------- del ------------------------------ - @Override - public CompletableFuture delAsync(String... keys) { - return sendAsync(Command.DEL, keys).thenApply(v -> v.toInteger()); - } - - @Override - public int del(String... keys) { - return delAsync(keys).join(); - } - - //--------------------- incrby ------------------------------ - @Override - public long incr(final String key) { - return incrAsync(key).join(); - } - - @Override - public CompletableFuture incrAsync(final String key) { - return sendAsync(Command.INCR, key).thenApply(v -> getLongValue(v, 0L)); - } - - @Override - public long incrby(final String key, long num) { - return incrbyAsync(key, num).join(); - } - - @Override - public double incrbyFloat(final String key, double num) { - return incrbyFloatAsync(key, num).join(); - } - - @Override - public CompletableFuture incrbyAsync(final String key, long num) { - return sendAsync(Command.INCRBY, key, String.valueOf(num)).thenApply(v -> getLongValue(v, 0L)); - } - - @Override - public CompletableFuture incrbyFloatAsync(final String key, double num) { - return sendAsync(Command.INCRBYFLOAT, key, String.valueOf(num)).thenApply(v -> getDoubleValue(v, 0.d)); - } - - //--------------------- decrby ------------------------------ - @Override - public long decr(final String key) { - return decrAsync(key).join(); - } - - @Override - public CompletableFuture decrAsync(final String key) { - return sendAsync(Command.DECR, key).thenApply(v -> getLongValue(v, 0L)); - } - - @Override - public long decrby(final String key, long num) { - return decrbyAsync(key, num).join(); - } - - @Override - public CompletableFuture decrbyAsync(final String key, long num) { - return sendAsync(Command.DECRBY, key, String.valueOf(num)).thenApply(v -> getLongValue(v, 0L)); - } - - @Override - public int hdel(final String key, String... fields) { - return hdelAsync(key, fields).join(); - } - - @Override - public int hlen(final String key) { - return hlenAsync(key).join(); - } - - @Override - public List hkeys(final String key) { - return hkeysAsync(key).join(); - } - - @Override - public long hincr(final String key, String field) { - return hincrAsync(key, field).join(); - } - - @Override - public long hincrby(final String key, String field, long num) { - return hincrbyAsync(key, field, num).join(); - } - - @Override - public double hincrbyFloat(final String key, String field, double num) { - return hincrbyFloatAsync(key, field, num).join(); - } - - @Override - public long hdecr(final String key, String field) { - return hdecrAsync(key, field).join(); - } - - @Override - public long hdecrby(final String key, String field, long num) { - return hdecrbyAsync(key, field, num).join(); - } - - @Override - public boolean hexists(final String key, String field) { - return hexistsAsync(key, field).join(); - } - - @Override - public void hset(final String key, final String field, final Type type, final T value) { - hsetAsync(key, field, type, value).join(); - } - - @Override - public void hset(final String key, final String field, final Convert convert, final Type type, final T value) { - hsetAsync(key, field, convert, type, value).join(); - } - - @Override - public void hsetString(final String key, final String field, final String value) { - hsetStringAsync(key, field, value).join(); - } - - @Override - public void hsetLong(final String key, final String field, final long value) { - hsetLongAsync(key, field, value).join(); - } - - @Override - public boolean hsetnx(final String key, final String field, final Type type, final T value) { - return hsetnxAsync(key, field, type, value).join(); - } - - @Override - public boolean hsetnx(final String key, final String field, final Convert convert, final Type type, final T value) { - return hsetnxAsync(key, field, convert, type, value).join(); - } - - @Override - public boolean hsetnxString(final String key, final String field, final String value) { - return hsetnxStringAsync(key, field, value).join(); - } - - @Override - public boolean hsetnxLong(final String key, final String field, final long value) { - return hsetnxLongAsync(key, field, value).join(); - } - - @Override - public void hmset(final String key, final Serializable... values) { - hmsetAsync(key, values).join(); - } - - @Override - public void hmset(final String key, final Map map) { - hmsetAsync(key, map).join(); - } - - @Override - public List hmget(final String key, final Type type, final String... fields) { - return hmgetAsync(key, type, fields).join(); - } - - @Override - public Map hmap(final String key, final Type type, int offset, int limit, String pattern) { - return (Map) hmapAsync(key, type, offset, limit, pattern).join(); - } - - @Override - public Map hmap(final String key, final Type type, int offset, int limit) { - return (Map) hmapAsync(key, type, offset, limit).join(); - } - - @Override - public T hget(final String key, final String field, final Type type) { - return (T) hgetAsync(key, field, type).join(); - } - - @Override - public String hgetString(final String key, final String field) { - return hgetStringAsync(key, field).join(); - } - - @Override - public long hgetLong(final String key, final String field, long defValue) { - return hgetLongAsync(key, field, defValue).join(); - } - - @Override - public CompletableFuture hdelAsync(final String key, String... fields) { - String[] args = new String[fields.length + 1]; - args[0] = key; - System.arraycopy(fields, 0, args, 1, fields.length); - return sendAsync(Command.HDEL, args).thenApply(v -> getIntValue(v, 0)); - } - - @Override - public CompletableFuture hlenAsync(final String key) { - return sendAsync(Command.HLEN, key).thenApply(v -> getIntValue(v, 0)); - } - - @Override - public CompletableFuture> hkeysAsync(final String key) { - return sendAsync(Command.HKEYS, key).thenApply(v -> (List) getCollectionValue(key, cryptor, v, false, String.class)); - } - - @Override - public CompletableFuture hincrAsync(final String key, String field) { - return hincrbyAsync(key, field, 1); - } - - @Override - public CompletableFuture hincrbyAsync(final String key, String field, long num) { - return sendAsync(Command.HINCRBY, key, field, String.valueOf(num)).thenApply(v -> getLongValue(v, 0L)); - } - - @Override - public CompletableFuture hincrbyFloatAsync(final String key, String field, double num) { - return sendAsync(Command.HINCRBYFLOAT, key, field, String.valueOf(num)).thenApply(v -> getDoubleValue(v, 0d)); - } - - @Override - public CompletableFuture hdecrAsync(final String key, String field) { - return hincrbyAsync(key, field, -1); - } - - @Override - public CompletableFuture hdecrbyAsync(final String key, String field, long num) { - return hincrbyAsync(key, field, -num); - } - - @Override - public CompletableFuture hexistsAsync(final String key, String field) { - return sendAsync(Command.HEXISTS, key, field).thenApply(v -> getIntValue(v, 0) > 0); - } - - @Override - public CompletableFuture hsetAsync(final String key, final String field, final Type type, final T value) { - if (value == null) { - return CompletableFuture.completedFuture(null); - } - return sendAsync(Command.HSET, key, field, formatValue(key, cryptor, null, type, value)).thenApply(v -> null); - } - - @Override - public CompletableFuture hsetAsync(final String key, final String field, final Convert convert, final Type type, final T value) { - if (value == null) { - return CompletableFuture.completedFuture(null); - } - return sendAsync(Command.HSET, key, field, formatValue(key, cryptor, convert, type, value)).thenApply(v -> null); - } - - @Override - public CompletableFuture hsetStringAsync(final String key, final String field, final String value) { - if (value == null) { - return CompletableFuture.completedFuture(null); - } - return sendAsync(Command.HSET, key, field, formatValue(key, cryptor, value)).thenApply(v -> null); - } - - @Override - public CompletableFuture hsetLongAsync(final String key, final String field, final long value) { - return sendAsync(Command.HSET, key, field, formatValue(value)).thenApply(v -> null); - } - - @Override - public CompletableFuture hsetnxAsync(final String key, final String field, final Type type, final T value) { - if (value == null) { - return CompletableFuture.completedFuture(null); - } - return sendAsync(Command.HSETNX, key, field, formatValue(key, cryptor, null, type, value)).thenApply(v -> getBoolValue(v)); - } - - @Override - public CompletableFuture hsetnxAsync(final String key, final String field, final Convert convert, final Type type, final T value) { - if (value == null) { - return CompletableFuture.completedFuture(null); - } - return sendAsync(Command.HSETNX, key, field, formatValue(key, cryptor, convert, type, value)).thenApply(v -> getBoolValue(v)); - } - - @Override - public CompletableFuture hsetnxStringAsync(final String key, final String field, final String value) { - if (value == null) { - return CompletableFuture.completedFuture(null); - } - return sendAsync(Command.HSETNX, key, field, formatValue(key, cryptor, value)).thenApply(v -> getBoolValue(v)); - } - - @Override - public CompletableFuture hsetnxLongAsync(final String key, final String field, final long value) { - return sendAsync(Command.HSETNX, key, field, formatValue(value)).thenApply(v -> getBoolValue(v)); - } - - @Override - public CompletableFuture hmsetAsync(final String key, final Serializable... values) { - String[] args = new String[values.length + 1]; - args[0] = key; - for (int i = 0; i < values.length; i += 2) { - args[i + 1] = String.valueOf(values[i]); - args[i + 2] = formatValue(key, cryptor, values[i + 1]); - } - return sendAsync(Command.HMSET, args).thenApply(v -> null); - } - - @Override - public CompletableFuture hmsetAsync(final String key, final Map map) { - if (map == null || map.isEmpty()) { - return CompletableFuture.completedFuture(null); - } - List bs = new ArrayList<>(); - bs.add(key); - map.forEach((k, v) -> { - bs.add(k.toString()); - bs.add(formatValue(k.toString(), cryptor, convert, v.getClass(), v)); - }); - return sendAsync(Command.HMSET, bs.toArray(new String[bs.size()])).thenApply(v -> null); - } - - @Override - public CompletableFuture> hmgetAsync(final String key, final Type type, final String... fields) { - String[] args = new String[fields.length + 1]; - args[0] = key; - for (int i = 0; i < fields.length; i++) { - args[i + 1] = fields[i]; - } - return sendAsync(Command.HMGET, args).thenApply(v -> (List) getCollectionValue(key, cryptor, v, false, type)); - } - - @Override - public CompletableFuture> hmapAsync(final String key, final Type type, int offset, int limit) { - return hmapAsync(key, type, offset, limit, null); - } - - @Override - public CompletableFuture> hmapAsync(final String key, final Type type, int offset, int limit, String pattern) { - String[] args = new String[pattern == null || pattern.isEmpty() ? 4 : 6]; - int index = -1; - args[++index] = key; - args[++index] = String.valueOf(offset); - if (pattern != null && !pattern.isEmpty()) { - args[++index] = "MATCH"; - args[++index] = pattern; - } - args[++index] = "COUNT"; - args[++index] = String.valueOf(limit); - return sendAsync(Command.HSCAN, args).thenApply(v -> getMapValue(key, cryptor, v, type)); - } - - @Override - public CompletableFuture hgetAsync(final String key, final String field, final Type type) { - return sendAsync(Command.HGET, key, field).thenApply(v -> getObjectValue(key, cryptor, v, type)); - } - - @Override - public CompletableFuture hgetStringAsync(final String key, final String field) { - return sendAsync(Command.HGET, key, field).thenApply(v -> getStringValue(key, cryptor, v)); - } - - @Override - public CompletableFuture hgetLongAsync(final String key, final String field, long defValue) { - return sendAsync(Command.HGET, key, field).thenApply(v -> getLongValue(v, defValue)); - } - - //--------------------- collection ------------------------------ - @Override - public CompletableFuture llenAsync(String key) { - return sendAsync(Command.TYPE, key).thenCompose(t -> sendAsync(Command.LLEN, key).thenApply(v -> getIntValue(v, 0))); - } - - @Override - public CompletableFuture scardAsync(String key) { - return sendAsync(Command.TYPE, key).thenCompose(t -> sendAsync(Command.SCARD, key).thenApply(v -> getIntValue(v, 0))); - } - - @Override - public int llen(String key) { - return llenAsync(key).join(); - } - - @Override - public int scard(String key) { - return scardAsync(key).join(); - } - - @Override - public CompletableFuture> smembersAsync(String key, final Type componentType) { - return sendAsync(Command.SMEMBERS, keyArgs(true, key)).thenApply(v -> (Set) getCollectionValue(key, cryptor, v, true, componentType)); - } - - @Override - public CompletableFuture> lrangeAsync(String key, final Type componentType) { - return sendAsync(Command.LRANGE, keyArgs(false, key)).thenApply(v -> (List) getCollectionValue(key, cryptor, v, false, componentType)); - } - - @Override - public CompletableFuture> mgetLongAsync(String... keys) { - return sendAsync(Command.MGET, keys).thenApply(v -> { - List list = (List) getCollectionValue(null, null, v, false, long.class); - Map map = new LinkedHashMap<>(); - for (int i = 0; i < keys.length; i++) { - Object obj = list.get(i); - if (obj != null) { - map.put(keys[i], list.get(i)); - } - } - return map; - }); - } - - @Override - public CompletableFuture> mgetStringAsync(String... keys) { - return sendAsync(Command.MGET, keys).thenApply(v -> { - List list = (List) getCollectionValue(keys[0], cryptor, v, false, String.class); - Map map = new LinkedHashMap<>(); - for (int i = 0; i < keys.length; i++) { - Object obj = list.get(i); - if (obj != null) { - map.put(keys[i], list.get(i)); - } - } - return map; - }); - } - - @Override - public CompletableFuture> mgetAsync(final Type componentType, String... keys) { - return sendAsync(Command.MGET, keys).thenApply(v -> { - List list = (List) getCollectionValue(keys[0], cryptor, v, false, componentType); - Map map = new LinkedHashMap<>(); - for (int i = 0; i < keys.length; i++) { - Object obj = list.get(i); - if (obj != null) { - map.put(keys[i], list.get(i)); - } - } - return map; - }); - } - - @Override - public CompletableFuture> mgetBytesAsync(String... keys) { - return sendAsync(Command.MGET, keys).thenApply(v -> { - List list = (List) getCollectionValue(keys[0], cryptor, v, false, byte[].class); - Map map = new LinkedHashMap<>(); - for (int i = 0; i < keys.length; i++) { - Object obj = list.get(i); - if (obj != null) { - map.put(keys[i], list.get(i)); - } - } - return map; - }); - } - - @Override - public CompletableFuture>> lrangeAsync(final Type componentType, final String... keys) { - final CompletableFuture>> rsFuture = new CompletableFuture<>(); - final Map> map = new LinkedHashMap<>(); - final ReentrantLock mapLock = new ReentrantLock(); - final CompletableFuture[] futures = new CompletableFuture[keys.length]; - for (int i = 0; i < keys.length; i++) { - final String key = keys[i]; - futures[i] = sendAsync(Command.LRANGE, keyArgs(false, key)).thenAccept(v -> { - List c = (List) getCollectionValue(key, cryptor, v, false, componentType); - if (c != null) { - mapLock.lock(); - try { - map.put(key, c); - } finally { - mapLock.unlock(); - } - } - }); - } - CompletableFuture.allOf(futures) - .whenComplete((w, e) -> { - if (e != null) { - rsFuture.completeExceptionally(e); - } else { - rsFuture.complete(map); - } - } - ); - return rsFuture; - } - - @Override - public CompletableFuture>> smembersAsync(final Type componentType, final String... keys) { - final CompletableFuture>> rsFuture = new CompletableFuture<>(); - final Map> map = new LinkedHashMap<>(); - final ReentrantLock mapLock = new ReentrantLock(); - final CompletableFuture[] futures = new CompletableFuture[keys.length]; - for (int i = 0; i < keys.length; i++) { - final String key = keys[i]; - futures[i] = sendAsync(Command.SMEMBERS, keyArgs(true, key)).thenAccept(v -> { - Set c = (Set) getCollectionValue(key, cryptor, v, true, componentType); - if (c != null) { - mapLock.lock(); - try { - map.put(key, c); - } finally { - mapLock.unlock(); - } - } - }); - } - CompletableFuture.allOf(futures) - .whenComplete((w, e) -> { - if (e != null) { - rsFuture.completeExceptionally(e); - } else { - rsFuture.complete(map); - } - } - ); - return rsFuture; - } - - @Override - public Set smembers(String key, final Type componentType) { - return (Set) smembersAsync(key, componentType).join(); - } - - @Override - public List lrange(String key, final Type componentType) { - return (List) lrangeAsync(key, componentType).join(); - } - - @Override - public Map mgetLong(final String... keys) { - return mgetLongAsync(keys).join(); - } - - @Override - public Map mgetString(final String... keys) { - return mgetStringAsync(keys).join(); - } - - @Override - public Map mget(final Type componentType, final String... keys) { - return (Map) mgetAsync(componentType, keys).join(); - } - - @Override - public Map mgetBytes(final String... keys) { - return (Map) mgetBytesAsync(keys).join(); - } - - @Override - public Map> smembers(final Type componentType, String... keys) { - return (Map) smembersAsync(componentType, keys).join(); - } - - @Override - public Map> lrange(final Type componentType, String... keys) { - return (Map) lrangeAsync(componentType, keys).join(); - } - - //--------------------- existsItem ------------------------------ - @Override - public boolean sismember(String key, final Type componentType, T value) { - return sismemberAsync(key, componentType, value).join(); - } - - @Override - public CompletableFuture sismemberAsync(String key, final Type componentType, T value) { - return sendAsync(Command.SISMEMBER, key, formatValue(key, cryptor, (Convert) null, componentType, value)).thenApply(v -> getIntValue(v, 0) > 0); - } - - @Override - public boolean sismemberString(String key, String value) { - return sismemberStringAsync(key, value).join(); - } - - @Override - public CompletableFuture sismemberStringAsync(String key, String value) { - return sendAsync(Command.SISMEMBER, key, formatValue(key, cryptor, value)).thenApply(v -> getIntValue(v, 0) > 0); - } - - @Override - public boolean sismemberLong(String key, long value) { - return sismemberLongAsync(key, value).join(); - } - - @Override - public CompletableFuture sismemberLongAsync(String key, long value) { - return sendAsync(Command.SISMEMBER, key, formatValue(value)).thenApply(v -> getIntValue(v, 0) > 0); - } - - //--------------------- rpush ------------------------------ - @Override - public CompletableFuture rpushAsync(String key, final Type componentType, T value) { - return sendAsync(Command.RPUSH, key, formatValue(key, cryptor, (Convert) null, componentType, value)).thenApply(v -> null); - } - - @Override - public void rpush(String key, final Type componentType, T value) { - rpushAsync(key, componentType, value).join(); - } - - @Override - public CompletableFuture rpushStringAsync(String key, String value) { - return sendAsync(Command.RPUSH, key, formatValue(key, cryptor, value)).thenApply(v -> null); - } - - @Override - public void rpushString(String key, String value) { - rpushStringAsync(key, value).join(); - } - - @Override - public CompletableFuture rpushLongAsync(String key, long value) { - return sendAsync(Command.RPUSH, key, formatValue(value)).thenApply(v -> null); - } - - @Override - public void rpushLong(String key, long value) { - rpushLongAsync(key, value).join(); - } - - //--------------------- lrem ------------------------------ - @Override - public CompletableFuture lremAsync(String key, final Type componentType, T value) { - return sendAsync(Command.LREM, key, "0", formatValue(key, cryptor, (Convert) null, componentType, value)).thenApply(v -> getIntValue(v, 0)); - } - - @Override - public int lrem(String key, final Type componentType, T value) { - return lremAsync(key, componentType, value).join(); - } - - @Override - public CompletableFuture lremStringAsync(String key, String value) { - return sendAsync(Command.LREM, key, "0", formatValue(key, cryptor, value)).thenApply(v -> getIntValue(v, 0)); - } - - @Override - public int lremString(String key, String value) { - return lremStringAsync(key, value).join(); - } - - @Override - public CompletableFuture lremLongAsync(String key, long value) { - return sendAsync(Command.LREM, key, "0", formatValue(value)).thenApply(v -> getIntValue(v, 0)); - } - - @Override - public int lremLong(String key, long value) { - return lremLongAsync(key, value).join(); - } - - //--------------------- sadd ------------------------------ - @Override - public CompletableFuture saddAsync(String key, Type componentType, T value) { - return sendAsync(Command.SADD, key, formatValue(key, cryptor, (Convert) null, componentType, value)).thenApply(v -> null); - } - - @Override - public CompletableFuture spopAsync(String key, Type componentType) { - return sendAsync(Command.SPOP, key).thenApply(v -> getObjectValue(key, cryptor, v, componentType)); - } - - @Override - public CompletableFuture> spopAsync(String key, int count, Type componentType) { - return sendAsync(Command.SPOP, key, String.valueOf(count)).thenApply(v -> getObjectValue(key, cryptor, v, componentType)); - } - - @Override - public CompletableFuture spopStringAsync(String key) { - return sendAsync(Command.SPOP, key).thenApply(v -> getStringValue(key, cryptor, v)); - } - - @Override - public CompletableFuture> spopStringAsync(String key, int count) { - return sendAsync(Command.SPOP, key, String.valueOf(count)).thenApply(v -> (Set) getCollectionValue(key, cryptor, v, true, String.class)); - } - - @Override - public CompletableFuture spopLongAsync(String key) { - return sendAsync(Command.SPOP, key).thenApply(v -> getLongValue(v, 0L)); - } - - @Override - public CompletableFuture> spopLongAsync(String key, int count) { - return sendAsync(Command.SPOP, key, String.valueOf(count)).thenApply(v -> (Set) getCollectionValue(key, cryptor, v, true, long.class)); - } - - @Override - public void sadd(String key, final Type componentType, T value) { - saddAsync(key, componentType, value).join(); - } - - @Override - public T spop(String key, final Type componentType) { - return (T) spopAsync(key, componentType).join(); - } - - @Override - public Set spop(String key, int count, final Type componentType) { - return (Set) spopAsync(key, count, componentType).join(); - } - - @Override - public String spopString(String key) { - return spopStringAsync(key).join(); - } - - @Override - public Set spopString(String key, int count) { - return spopStringAsync(key, count).join(); - } - - @Override - public Long spopLong(String key) { - return spopLongAsync(key).join(); - } - - @Override - public Set spopLong(String key, int count) { - return spopLongAsync(key, count).join(); - } - - @Override - public CompletableFuture saddStringAsync(String key, String value) { - return sendAsync(Command.SADD, key, formatValue(key, cryptor, value)).thenApply(v -> null); - } - - @Override - public void saddString(String key, String value) { - saddStringAsync(key, value).join(); - } - - @Override - public CompletableFuture saddLongAsync(String key, long value) { - return sendAsync(Command.SADD, key, formatValue(value)).thenApply(v -> null); - } - - @Override - public void saddLong(String key, long value) { - saddLongAsync(key, value).join(); - } - - //--------------------- srem ------------------------------ - @Override - public CompletableFuture sremAsync(String key, final Type componentType, T value) { - return sendAsync(Command.SREM, key, formatValue(key, cryptor, (Convert) null, componentType, value)).thenApply(v -> getIntValue(v, 0)); - } - - @Override - public int srem(String key, final Type componentType, T value) { - return sremAsync(key, componentType, value).join(); - } - - @Override - public CompletableFuture sremStringAsync(String key, String value) { - return sendAsync(Command.SREM, key, formatValue(key, cryptor, value)).thenApply(v -> getIntValue(v, 0)); - } - - @Override - public int sremString(String key, String value) { - return sremStringAsync(key, value).join(); - } - - @Override - public CompletableFuture sremLongAsync(String key, long value) { - return sendAsync(Command.SREM, key, formatValue(value)).thenApply(v -> getIntValue(v, 0)); - } - - @Override - public int sremLong(String key, long value) { - return sremLongAsync(key, value).join(); - } - - //--------------------- keys ------------------------------ - @Override - public List keys(String pattern) { - return keysAsync(pattern).join(); - } - - @Override - public byte[] getBytes(final String key) { - return getBytesAsync(key).join(); - } - - @Override - public byte[] getSetBytes(final String key, byte[] value) { - return getSetBytesAsync(key, value).join(); - } - - @Override - public byte[] getexBytes(final String key, final int expireSeconds) { - return getexBytesAsync(key, expireSeconds).join(); - } - - @Override - public void setBytes(final String key, final byte[] value) { - setBytesAsync(key, value).join(); - } - - @Override - public void setexBytes(final String key, final int expireSeconds, final byte[] value) { - setexBytesAsync(key, expireSeconds, value).join(); - } - - @Override - public boolean setnxexBytes(final String key, final int expireSeconds, final byte[] value) { - return setnxexBytesAsync(key, expireSeconds, value).join(); - } - - @Override - public CompletableFuture getBytesAsync(final String key) { - return sendAsync(Command.GET, key).thenApply(v -> v.toBytes()); - } - - @Override - public CompletableFuture getSetBytesAsync(final String key, byte[] value) { - return sendAsync(Command.GETSET, key, new String(value, StandardCharsets.UTF_8)).thenApply(v -> v.toBytes()); - } - - @Override - public CompletableFuture getexBytesAsync(final String key, final int expireSeconds) { - return sendAsync(Command.GETEX, key, "EX", String.valueOf(expireSeconds)).thenApply(v -> v.toBytes()); - } - - @Override - public CompletableFuture setBytesAsync(final String key, final byte[] value) { - return sendAsync(Command.SET, key, new String(value, StandardCharsets.UTF_8)).thenApply(v -> null); - } - - @Override - public boolean setnxBytes(final String key, final byte[] value) { - return setnxBytesAsync(key, value).join(); - } - - @Override - public CompletableFuture setnxBytesAsync(final String key, byte[] value) { - return sendAsync(Command.SETNX, key, new String(value, StandardCharsets.UTF_8)).thenApply(v -> getBoolValue(v)); - } - - @Override - public CompletableFuture setexBytesAsync(final String key, final int expireSeconds, final byte[] value) { - return sendAsync(Command.SETEX, key, String.valueOf(expireSeconds), new String(value, StandardCharsets.UTF_8)).thenApply(v -> null); - } - - @Override - public CompletableFuture setnxexBytesAsync(final String key, final int expireSeconds, final byte[] value) { - return sendAsync(Command.SET, key, new String(value, StandardCharsets.UTF_8), "NX", "EX", String.valueOf(expireSeconds)).thenApply(v -> v != null && ("OK".equals(v.toString()) || v.toInteger() > 0)); - } - - @Override - public CompletableFuture> keysAsync(String pattern) { - return sendAsync(Command.KEYS, pattern == null || pattern.isEmpty() ? "*" : pattern).thenApply(v -> (List) getCollectionValue(null, null, v, false, String.class)); - } - - //--------------------- dbsize ------------------------------ - @Override - public long dbsize() { - return dbsizeAsync().join(); - } - - @Override - public CompletableFuture dbsizeAsync() { - return sendAsync(Command.DBSIZE).thenApply(v -> getLongValue(v, 0L)); - } - - @Override - public CompletableFuture> getStringCollectionAsync(String key) { - return sendAsync(Command.TYPE, key).thenCompose(t -> { - String type = t.toString(); - if (type == null) { - return CompletableFuture.completedFuture(null); - } - boolean set = !type.contains("list"); - return sendAsync(set ? Command.SMEMBERS : Command.LRANGE, keyArgs(set, key)).thenApply(v -> getCollectionValue(key, cryptor, v, set, String.class)); - }); - } - - @Override - public CompletableFuture>> getStringCollectionMapAsync(final boolean set, String... keys) { - final CompletableFuture>> rsFuture = new CompletableFuture<>(); - final Map> map = new LinkedHashMap<>(); - final ReentrantLock mapLock = new ReentrantLock(); - final CompletableFuture[] futures = new CompletableFuture[keys.length]; - for (int i = 0; i < keys.length; i++) { - final String key = keys[i]; - futures[i] = sendAsync(set ? Command.SMEMBERS : Command.LRANGE, keyArgs(set, key)).thenAccept(v -> { - Collection c = getCollectionValue(key, cryptor, v, set, String.class); - if (c != null) { - mapLock.lock(); - try { - map.put(key, (Collection) c); - } finally { - mapLock.unlock(); - } - } - }); - } - CompletableFuture.allOf(futures).whenComplete((w, e) -> { - if (e != null) { - rsFuture.completeExceptionally(e); - } else { - rsFuture.complete(map); - } - }); - return rsFuture; - } - - @Override - public Collection getCollection(String key, final Type componentType) { - return (Collection) getCollectionAsync(key, componentType).join(); - } - - @Override - public Long[] getLongArray(final String... keys) { - return getLongArrayAsync(keys).join(); - } - - @Override - public String[] getStringArray(final String... keys) { - return getStringArrayAsync(keys).join(); - } - - @Override - public Map> getCollectionMap(final boolean set, final Type componentType, String... keys) { - return (Map) getCollectionMapAsync(set, componentType, keys).join(); - } - - @Override - public CompletableFuture getCollectionSizeAsync(String key) { - return sendAsync(Command.TYPE, key).thenCompose(t -> { - String type = t.toString(); - if (type == null) { - return CompletableFuture.completedFuture(0); - } - return sendAsync(type.contains("list") ? Command.LLEN : Command.SCARD, key).thenApply(v -> getIntValue(v, 0)); - }); - } - - @Override - public int getCollectionSize(String key) { - return getCollectionSizeAsync(key).join(); - } - - @Override - public CompletableFuture> getCollectionAsync(String key, final Type componentType) { - return sendAsync(Command.TYPE, key).thenCompose(t -> { - String type = t.toString(); - if (type == null) { - return CompletableFuture.completedFuture(null); - } - boolean set = !type.contains("list"); - return sendAsync(set ? Command.SMEMBERS : Command.LRANGE, keyArgs(set, key)).thenApply(v -> getCollectionValue(key, cryptor, v, set, componentType)); - }); - } - - @Override - public CompletableFuture getLongArrayAsync(String... keys) { - return sendAsync(Command.MGET, keys).thenApply(v -> { - List list = (List) getCollectionValue(null, null, v, false, long.class); - Long[] rs = new Long[keys.length]; - for (int i = 0; i < keys.length; i++) { - Number obj = (Number) list.get(i); - rs[i] = obj == null ? null : obj.longValue(); - } - return rs; - }); - } - - @Override - public CompletableFuture getStringArrayAsync(String... keys) { - return sendAsync(Command.MGET, keys).thenApply(v -> { - List list = (List) getCollectionValue(keys[0], cryptor, v, false, String.class); - String[] rs = new String[keys.length]; - for (int i = 0; i < keys.length; i++) { - Object obj = list.get(i); - rs[i] = obj == null ? null : obj.toString(); - } - return rs; - }); - } - - @Override - public CompletableFuture>> getCollectionMapAsync(final boolean set, final Type componentType, final String... keys) { - final CompletableFuture>> rsFuture = new CompletableFuture<>(); - final Map> map = new LinkedHashMap<>(); - final ReentrantLock mapLock = new ReentrantLock(); - final CompletableFuture[] futures = new CompletableFuture[keys.length]; - for (int i = 0; i < keys.length; i++) { - final String key = keys[i]; - futures[i] = sendAsync(set ? Command.SMEMBERS : Command.LRANGE, keyArgs(set, key)).thenAccept(v -> { - Collection c = getCollectionValue(key, cryptor, v, set, componentType); - if (c != null) { - mapLock.lock(); - try { - map.put(key, (Collection) c); - } finally { - mapLock.unlock(); - } - } - }); - } - - CompletableFuture.allOf(futures) - .whenComplete((w, e) -> { - if (e != null) { - rsFuture.completeExceptionally(e); - } else { - rsFuture.complete(map); - } - } - ); - return rsFuture; - } - - @Override - public Collection getStringCollection(String key) { - return getStringCollectionAsync(key).join(); - } - - @Override - public Map> getStringCollectionMap(final boolean set, String... keys) { - return getStringCollectionMapAsync(set, keys).join(); - } - - @Override - public CompletableFuture> getLongCollectionAsync(String key) { - return sendAsync(Command.TYPE, key).thenCompose(t -> { - String type = t.toString(); - if (type == null) { - return CompletableFuture.completedFuture(null); - } - boolean set = !type.contains("list"); - return sendAsync(set ? Command.SMEMBERS : Command.LRANGE, keyArgs(set, key)).thenApply(v -> getCollectionValue(key, cryptor, v, set, long.class)); - }); - } - - @Override - public CompletableFuture>> getLongCollectionMapAsync(final boolean set, String... keys) { - final CompletableFuture>> rsFuture = new CompletableFuture<>(); - final Map> map = new LinkedHashMap<>(); - final ReentrantLock mapLock = new ReentrantLock(); - final CompletableFuture[] futures = new CompletableFuture[keys.length]; - for (int i = 0; i < keys.length; i++) { - final String key = keys[i]; - futures[i] = sendAsync(set ? Command.SMEMBERS : Command.LRANGE, keyArgs(set, key)).thenAccept(v -> { - Collection c = getCollectionValue(key, cryptor, v, set, long.class); - if (c != null) { - mapLock.lock(); - try { - map.put(key, (Collection) c); - } finally { - mapLock.unlock(); - } - } - }); - } - CompletableFuture.allOf(futures).whenComplete((w, e) -> { - if (e != null) { - rsFuture.completeExceptionally(e); - } else { - rsFuture.complete(map); - } - }); - return rsFuture; - } - - @Override - public Collection getLongCollection(String key) { - return getLongCollectionAsync(key).join(); - } - - @Override - public Map> getLongCollectionMap(final boolean set, String... keys) { - return getLongCollectionMapAsync(set, keys).join(); - } - - //--------------------- getexCollection ------------------------------ - @Override - public CompletableFuture> getexCollectionAsync(String key, int expireSeconds, final Type componentType) { - return (CompletableFuture) expireAsync(key, expireSeconds).thenCompose(v -> getCollectionAsync(key, componentType)); - } - - @Override - public Collection getexCollection(String key, final int expireSeconds, final Type componentType) { - return (Collection) getexCollectionAsync(key, expireSeconds, componentType).join(); - } - - @Override - public CompletableFuture> getexStringCollectionAsync(String key, int expireSeconds) { - return (CompletableFuture) expireAsync(key, expireSeconds).thenCompose(v -> getStringCollectionAsync(key)); - } - - @Override - public Collection getexStringCollection(String key, final int expireSeconds) { - return getexStringCollectionAsync(key, expireSeconds).join(); - } - - @Override - public CompletableFuture> getexLongCollectionAsync(String key, int expireSeconds) { - return (CompletableFuture) expireAsync(key, expireSeconds).thenCompose(v -> getLongCollectionAsync(key)); - } - - @Override - public Collection getexLongCollection(String key, final int expireSeconds) { - return getexLongCollectionAsync(key, expireSeconds).join(); - } - -} diff --git a/src/org/redkalex/cache/redis/vertx/RedisVertxCacheSourceProvider.java b/src/org/redkalex/cache/redis/vertx/RedisVertxCacheSourceProvider.java deleted file mode 100644 index a54235c..0000000 --- a/src/org/redkalex/cache/redis/vertx/RedisVertxCacheSourceProvider.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - */ -package org.redkalex.cache.redis.vertx; - -import org.redkale.annotation.Priority; -import org.redkale.source.CacheSource; -import org.redkale.source.CacheSourceProvider; -import org.redkale.util.AnyValue; - -/** - * - * @author zhangjx - */ -@Priority(-200) -public class RedisVertxCacheSourceProvider implements CacheSourceProvider { - - @Override - public boolean acceptsConf(AnyValue config) { - try { - Object.class.isAssignableFrom(io.vertx.redis.client.RedisOptions.class); //试图加载vertx-redis相关类 - return new RedisVertxCacheSource().acceptsConf(config); - } catch (Throwable e) { - return false; - } - } - - @Override - public CacheSource createInstance() { - return new RedisVertxCacheSource(); - } - -}