io.lettuce.core.api.StatefulConnection Java Examples

The following examples show how to use io.lettuce.core.api.StatefulConnection. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LettuceFactory.java    From jetcache with Apache License 2.0 6 votes vote down vote up
public LettuceFactory(String key, Class<?> clazz) {
    this.clazz = clazz;
    if (AbstractRedisClient.class.isAssignableFrom(clazz)) {
        key += ".client";
    } else if (StatefulConnection.class.isAssignableFrom(clazz)) {
        key += ".connection";
    } else if (RedisClusterCommands.class.isAssignableFrom(clazz)) {
        // RedisCommands extends RedisClusterCommands
        key += ".commands";
    } else if (RedisClusterAsyncCommands.class.isAssignableFrom(clazz)) {
        // RedisAsyncCommands extends RedisClusterAsyncCommands
        key += ".asyncCommands";
    } else if (RedisClusterReactiveCommands.class.isAssignableFrom(clazz)) {
        // RedisReactiveCommands extends RedisClusterReactiveCommands
        key += ".reactiveCommands";
    } else {
        throw new IllegalArgumentException(clazz.getName());
    }
    this.key = key;
}
 
Example #2
Source File: LettuceCache.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
protected BaseRedisCommands sync(StatefulConnection conn) {
    if(conn instanceof StatefulRedisClusterConnection)
        return ((StatefulRedisClusterConnection)conn).sync();
    else if(conn instanceof StatefulRedisConnection)
        return ((StatefulRedisConnection)conn).sync();
    return null;
}
 
Example #3
Source File: LettuceConnectionManager.java    From jetcache with Apache License 2.0 5 votes vote down vote up
public StatefulConnection connection(AbstractRedisClient redisClient) {
    LettuceObjects lo = getLettuceObjectsFromMap(redisClient);
    if (lo.connection == null) {
        if (redisClient instanceof RedisClient) {
            lo.connection = ((RedisClient) redisClient).connect(new JetCacheCodec());
        } else if (redisClient instanceof RedisClusterClient) {
            lo.connection = ((RedisClusterClient) redisClient).connect(new JetCacheCodec());
        } else {
            throw new CacheConfigException("type " + redisClient.getClass() + " is not supported");
        }
    }
    return lo.connection;
}
 
Example #4
Source File: LettuceConnectionManager.java    From jetcache with Apache License 2.0 5 votes vote down vote up
public void init(AbstractRedisClient redisClient, StatefulConnection connection) {
    map.computeIfAbsent(redisClient, key -> {
        LettuceObjects lo = new LettuceObjects();
        lo.connection = connection;
        return lo;
    });
}
 
Example #5
Source File: LettuceGenericCache.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
@Override
public void clear() {
    try(StatefulConnection<String, byte[]> connection = super.connect()) {
        RedisKeyCommands<String, byte[]> cmd = (RedisKeyCommands)super.sync(connection);
        Collection<String> keys = keys(cmd);
        if(keys != null && keys.size() > 0)
            cmd.del(keys.stream().toArray(String[]::new));
    }
}
 
Example #6
Source File: LettuceGenericCache.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
@Override
public void evict(String... keys) {
    try(StatefulConnection<String, byte[]> connection = super.connect()) {
        RedisKeyCommands<String, byte[]> cmd = (RedisKeyCommands)super.sync(connection);
        cmd.del(Arrays.stream(keys).map(k -> _key(k)).toArray(String[]::new));
    }
}
 
Example #7
Source File: LettuceGenericCache.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
/**
 * 批量设置带 TTL 的缓存数据
 * @param bytes  cache data
 * @param timeToLiveInSeconds cache ttl
 */
@Override
public void setBytes(Map<String,byte[]> bytes, long timeToLiveInSeconds) {
    try(StatefulConnection<String, byte[]> connection = super.connect()) {
        RedisStringCommands<String, byte[]> cmd = (RedisStringCommands)super.sync(connection);
        if (timeToLiveInSeconds > 0)
            bytes.forEach((k,v)->cmd.setex(_key(k), timeToLiveInSeconds, v));
        else
            bytes.forEach((k,v)->cmd.set(_key(k), v));
    }
}
 
Example #8
Source File: LettuceGenericCache.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
/**
 * 设置缓存数据字节数组(带有效期)
 * @param key  cache key
 * @param bytes cache data
 * @param timeToLiveInSeconds cache ttl
 */
@Override
public void setBytes(String key, byte[] bytes, long timeToLiveInSeconds){
    try(StatefulConnection<String, byte[]> connection = super.connect()) {
        RedisStringCommands<String, byte[]> cmd = (RedisStringCommands)super.sync(connection);
        if (timeToLiveInSeconds > 0)
            cmd.setex(_key(key), timeToLiveInSeconds, bytes);
        else
            cmd.set(_key(key), bytes);
    }
}
 
Example #9
Source File: LettuceGenericCache.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
@Override
public void setBytes(Map<String, byte[]> bytes) {
    try(StatefulConnection<String, byte[]> connection = super.connect()) {
        RedisStringCommands<String, byte[]> cmd = (RedisStringCommands)super.sync(connection);
        cmd.mset(bytes.entrySet().stream().collect(Collectors.toMap(k -> _key(k.getKey()), Map.Entry::getValue)));
    }
}
 
Example #10
Source File: LettuceGenericCache.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
@Override
public void setBytes(String key, byte[] bytes) {
    try(StatefulConnection<String, byte[]> connection = super.connect()) {
        RedisStringCommands<String, byte[]> cmd = (RedisStringCommands)super.sync(connection);
        cmd.set(_key(key), bytes);
    }
}
 
Example #11
Source File: LettuceGenericCache.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
@Override
public List<byte[]> getBytes(Collection<String> keys) {
    try(StatefulConnection<String, byte[]> connection = super.connect()) {
        RedisStringCommands<String, byte[]> cmd = (RedisStringCommands)super.sync(connection);
        return cmd.mget(keys.stream().map(k -> _key(k)).toArray(String[]::new)).stream().map(kv -> kv.hasValue()?kv.getValue():null).collect(Collectors.toList());
    }
}
 
Example #12
Source File: LettuceGenericCache.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
public LettuceGenericCache(String namespace, String region, GenericObjectPool<StatefulConnection<String, byte[]>> pool, int scanCount) {
    if (region == null || region.isEmpty())
        region = "_"; // 缺省region

    super.pool = pool;
    this.namespace = namespace;
    this.region = getRegionName(region);
    this.scanCount = scanCount;
}
 
Example #13
Source File: LettuceHashCache.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
@Override
public void clear() {
    try(StatefulConnection<String, byte[]> connection = super.connect()) {
        RedisKeyCommands<String, byte[]> cmd = (RedisKeyCommands)super.sync(connection);
        cmd.del(this.region);
    }
}
 
Example #14
Source File: LettuceHashCache.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
@Override
public void evict(String... keys) {
    try(StatefulConnection<String, byte[]> connection = super.connect()) {
        RedisHashCommands<String, byte[]> cmd = (RedisHashCommands)super.sync(connection);
        cmd.hdel(this.region, keys);
    }
}
 
Example #15
Source File: LettuceHashCache.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<String> keys() {
    try(StatefulConnection<String, byte[]> connection = super.connect()) {
        RedisHashCommands<String, byte[]> cmd = (RedisHashCommands)super.sync(connection);
        return cmd.hkeys(this.region);
    }
}
 
Example #16
Source File: LettuceHashCache.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
@Override
public void setBytes(Map<String, byte[]> bytes) {
    try(StatefulConnection<String, byte[]> connection = super.connect()) {
        RedisHashCommands<String, byte[]> cmd = (RedisHashCommands)super.sync(connection);
        cmd.hmset(this.region, bytes);
    }
}
 
Example #17
Source File: LettuceHashCache.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
@Override
public void setBytes(String key, byte[] bytes) {
    try(StatefulConnection<String, byte[]> connection = super.connect()) {
        RedisHashCommands<String, byte[]> cmd = (RedisHashCommands)super.sync(connection);
        cmd.hset(this.region, key, bytes);
    }
}
 
Example #18
Source File: LettuceHashCache.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
@Override
public List<byte[]> getBytes(Collection<String> keys) {
    try(StatefulConnection<String, byte[]> connection = super.connect()) {
        RedisHashCommands<String, byte[]> cmd = (RedisHashCommands)super.sync(connection);
        return cmd.hmget(this.region, keys.stream().toArray(String[]::new)).stream().map(kv -> kv.hasValue()?kv.getValue():null).collect(Collectors.toList());
    }
}
 
Example #19
Source File: LettuceGenericCache.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] getBytes(String key) {
    try(StatefulConnection<String, byte[]> connection = super.connect()) {
        RedisStringCommands<String, byte[]> cmd = (RedisStringCommands)super.sync(connection);
        return cmd.get(_key(key));
    }
}
 
Example #20
Source File: LettuceHashCache.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] getBytes(String key) {
    try(StatefulConnection<String, byte[]> connection = super.connect()) {
        RedisHashCommands<String, byte[]> cmd = (RedisHashCommands)super.sync(connection);
        return cmd.hget(this.region, key);
    }
}
 
Example #21
Source File: LettuceHashCache.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
public LettuceHashCache(String namespace, String region, GenericObjectPool<StatefulConnection<String, byte[]>> pool) {
    if (region == null || region.isEmpty())
        region = "_"; // 缺省region

    super.pool = pool;
    super.namespace = namespace;
    super.region = getRegionName(region);
}
 
Example #22
Source File: LettuceCache.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
protected StatefulConnection connect() {
    try {
        return pool.borrowObject();
    } catch (Exception e) {
        throw new CacheException(e);
    }
}
 
Example #23
Source File: RedisLettuceCacheBuilder.java    From jetcache with Apache License 2.0 4 votes vote down vote up
public void setConnection(StatefulConnection connection) {
    getConfig().setConnection(connection);
}
 
Example #24
Source File: RedisSession.java    From pulsar with Apache License 2.0 4 votes vote down vote up
public StatefulConnection connection() {
    return this.connection;
}
 
Example #25
Source File: RedisSession.java    From pulsar with Apache License 2.0 4 votes vote down vote up
public RedisSession(AbstractRedisClient client, StatefulConnection connection, RedisClusterAsyncCommands<byte[], byte[]> asyncCommands) {
    this.client = client;
    this.connection = connection;
    this.asyncCommands = asyncCommands;
}
 
Example #26
Source File: RedisSessionFactoryImpl.java    From kafka-connect-redis with Apache License 2.0 4 votes vote down vote up
public StatefulConnection connection() {
  return this.connection;
}
 
Example #27
Source File: RedisSessionFactoryImpl.java    From kafka-connect-redis with Apache License 2.0 4 votes vote down vote up
RedisSessionImpl(AbstractRedisClient client, StatefulConnection connection, RedisClusterAsyncCommands<byte[], byte[]> asyncCommands, RedisConnectorConfig config) {
  this.client = client;
  this.connection = connection;
  this.asyncCommands = asyncCommands;
  this.config = config;
}
 
Example #28
Source File: RedisLettuceCacheTest.java    From jetcache with Apache License 2.0 4 votes vote down vote up
private void test(AbstractRedisClient client, StatefulConnection connection) throws Exception {
    cache = RedisLettuceCacheBuilder.createRedisLettuceCacheBuilder()
            .redisClient(client)
            .connection(connection)
            .keyConvertor(FastjsonKeyConvertor.INSTANCE)
            .valueEncoder(JavaValueEncoder.INSTANCE)
            .valueDecoder(JavaValueDecoder.INSTANCE)
            .keyPrefix(new Random().nextInt() + "")
            .expireAfterWrite(500, TimeUnit.MILLISECONDS)
            .buildCache();
    baseTest();
    expireAfterWriteTest(cache.config().getExpireAfterWriteInMillis());
    fastjsonKeyCoverterTest();
    testUnwrap(client);

    LoadingCacheTest.loadingCacheTest(RedisLettuceCacheBuilder.createRedisLettuceCacheBuilder()
            .redisClient(client)
            .keyConvertor(FastjsonKeyConvertor.INSTANCE)
            .valueEncoder(JavaValueEncoder.INSTANCE)
            .valueDecoder(JavaValueDecoder.INSTANCE)
            .keyPrefix(new Random().nextInt() + ""), 50);

    cache = RedisLettuceCacheBuilder.createRedisLettuceCacheBuilder()
            .redisClient(client)
            .keyConvertor(null)
            .valueEncoder(KryoValueEncoder.INSTANCE)
            .valueDecoder(KryoValueDecoder.INSTANCE)
            .keyPrefix(new Random().nextInt() + "")
            .buildCache();
    nullKeyConvertorTest();

    int thread = 10;
    int time = 3000;
    cache = RedisLettuceCacheBuilder.createRedisLettuceCacheBuilder()
            .redisClient(client)
            .keyConvertor(FastjsonKeyConvertor.INSTANCE)
            .valueEncoder(KryoValueEncoder.INSTANCE)
            .valueDecoder(KryoValueDecoder.INSTANCE)
            .keyPrefix(new Random().nextInt() + "")
            .buildCache();
    ((RedisLettuceCacheConfig) cache.config()).setAsyncResultTimeoutInMillis(1100);
    concurrentTest(thread, 500, time);
    LettuceConnectionManager.defaultManager().removeAndClose(client);
}
 
Example #29
Source File: RedisLettuceCacheConfig.java    From jetcache with Apache License 2.0 4 votes vote down vote up
public void setConnection(StatefulConnection connection) {
    this.connection = connection;
}
 
Example #30
Source File: RedisLettuceCacheConfig.java    From jetcache with Apache License 2.0 4 votes vote down vote up
public StatefulConnection getConnection() {
    return connection;
}