io.lettuce.core.api.sync.RedisStringCommands Java Examples

The following examples show how to use io.lettuce.core.api.sync.RedisStringCommands. 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: RedisLettuceCache.java    From jetcache with Apache License 2.0 6 votes vote down vote up
public RedisLettuceCache(RedisLettuceCacheConfig<K, V> config) {
    super(config);
    this.config = config;
    this.valueEncoder = config.getValueEncoder();
    this.valueDecoder = config.getValueDecoder();
    if (config.getRedisClient() == null) {
        throw new CacheConfigException("RedisClient is required");
    }
    if (config.isExpireAfterAccess()) {
        throw new CacheConfigException("expireAfterAccess is not supported");
    }

    client = config.getRedisClient();

    lettuceConnectionManager = LettuceConnectionManager.defaultManager();
    lettuceConnectionManager.init(client, config.getConnection());
    stringCommands = (RedisStringCommands<byte[], byte[]>) lettuceConnectionManager.commands(client);
    stringAsyncCommands = (RedisStringAsyncCommands<byte[], byte[]>) lettuceConnectionManager.asyncCommands(client);
    keyAsyncCommands = (RedisKeyAsyncCommands<byte[], byte[]>) stringAsyncCommands;
}
 
Example #2
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 #3
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 #4
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 #5
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 #6
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 #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));
    }
}