private byte[][] rawKeys(Collection<K> keys) { final byte[][] rawKeys = new byte[keys.size()][]; int i = 0; for (K key : keys) { rawKeys[i++] = rawKey(key); } return rawKeys; }
@Override public Boolean unlink(K key) { byte[] rawKey = rawKey(key); Long result = execute(connection -> connection.unlink(rawKey), true); return result != null && result.intValue() == 1; }
@Override public Boolean delete(K key) { byte[] rawKey = rawKey(key); Long result = execute(connection -> connection.del(rawKey), true); return result != null && result.intValue() == 1; }
@Override public DataType type(K key) { byte[] rawKey = rawKey(key); return execute(connection -> connection.type(rawKey), true); }
@Override public Boolean hasKey(K key) { byte[] rawKey = rawKey(key); return execute(connection -> connection.exists(rawKey), true); }
@Override public Long getExpire(K key) { byte[] rawKey = rawKey(key); return execute(connection -> connection.ttl(rawKey), true); }
@Override public Boolean persist(K key) { byte[] rawKey = rawKey(key); return execute(connection -> connection.persist(rawKey), true); }
@Override public Boolean move(K key, final int dbIndex) { byte[] rawKey = rawKey(key); return execute(connection -> connection.move(rawKey, dbIndex), true); }
@Override public Boolean expireAt(K key, final Date date) { byte[] rawKey = rawKey(key); return execute(connection -> { try { return connection.pExpireAt(rawKey, date.getTime()); } catch (Exception e) { return connection.expireAt(rawKey, date.getTime() / 1000); } }, true); }
@Override public void watch(K key) { byte[] rawKey = rawKey(key); execute(connection -> { connection.watch(rawKey); return null; }, true); }
/** * Executes the Redis dump command and returns the results. Redis uses a non-standard serialization mechanism and * includes checksum information, thus the raw bytes are returned as opposed to deserializing with valueSerializer. * Use the return value of dump as the value argument to restore * * @param key The key to dump * @return results The results of the dump operation */ @Override public byte[] dump(K key) { byte[] rawKey = rawKey(key); return execute(connection -> connection.dump(rawKey), true); }
@Override @SuppressWarnings("unchecked") public Set<K> keys(K pattern) { byte[] rawKey = rawKey(pattern); Set<byte[]> rawKeys = execute(connection -> connection.keys(rawKey), true); return keySerializer != null ? SerializationUtils.deserialize(rawKeys, keySerializer) : (Set<K>) rawKeys; }
@Override public Long getExpire(K key, final TimeUnit timeUnit) { byte[] rawKey = rawKey(key); return execute(connection -> { try { return connection.pTtl(rawKey, timeUnit); } catch (Exception e) { // Driver may not support pTtl or we may be running on Redis 2.4 return connection.ttl(rawKey, timeUnit); } }, true); }
@Override public <T> List<T> sort(SortQuery<K> query, @Nullable RedisSerializer<T> resultSerializer) { byte[] rawKey = rawKey(query.getKey()); SortParameters params = QueryUtils.convertQuery(query, stringSerializer); List<byte[]> vals = execute(connection -> connection.sort(rawKey, params), true); return SerializationUtils.deserialize(vals, resultSerializer); }
/** * Executes the Redis restore command. The value passed in should be the exact serialized data returned from * {@link #dump(Object)}, since Redis uses a non-standard serialization mechanism. * * @param key The key to restore * @param value The value to restore, as returned by {@link #dump(Object)} * @param timeToLive An expiration for the restored key, or 0 for no expiration * @param unit The time unit for timeToLive * @param replace use {@literal true} to replace a potentially existing value instead of erroring. * @throws RedisSystemException if the key you are attempting to restore already exists and {@code replace} is set to * {@literal false}. */ @Override public void restore(K key, final byte[] value, long timeToLive, TimeUnit unit, boolean replace) { byte[] rawKey = rawKey(key); long rawTimeout = TimeoutUtils.toMillis(timeToLive, unit); execute(connection -> { connection.restore(rawKey, rawTimeout, value, replace); return null; }, true); }
@Override public Boolean expire(K key, final long timeout, final TimeUnit unit) { byte[] rawKey = rawKey(key); long rawTimeout = TimeoutUtils.toMillis(timeout, unit); return execute(connection -> { try { return connection.pExpire(rawKey, rawTimeout); } catch (Exception e) { // Driver may not support pExpire or we may be running on Redis 2.4 return connection.expire(rawKey, TimeoutUtils.toSeconds(timeout, unit)); } }, true); }