org.springframework.data.redis.connection.jedis.JedisKeyCommands Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of org.apache.servicemix.bundles.spring-data-redis
Show all versions of org.apache.servicemix.bundles.spring-data-redis
This OSGi bundle wraps ${pkgArtifactId} ${pkgVersion} jar files.
/*
* Copyright 2017-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.connection.jedis;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.SortingParams;
import java.time.Duration;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.connection.RedisKeyCommands;
import org.springframework.data.redis.connection.SortParameters;
import org.springframework.data.redis.connection.ValueEncoding;
import org.springframework.data.redis.connection.ValueEncoding.RedisValueEncoding;
import org.springframework.data.redis.connection.convert.Converters;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.ScanCursor;
import org.springframework.data.redis.core.ScanIteration;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* @author Christoph Strobl
* @author Mark Paluch
* @since 2.0
*/
@RequiredArgsConstructor
class JedisKeyCommands implements RedisKeyCommands {
private final @NonNull JedisConnection connection;
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#exists(byte[])
*/
@Override
public Boolean exists(byte[] key) {
Assert.notNull(key, "Key must not be null!");
try {
if (isPipelined()) {
pipeline(connection.newJedisResult(connection.getRequiredPipeline().exists(key)));
return null;
}
if (isQueueing()) {
transaction(connection.newJedisResult(connection.getRequiredTransaction().exists(key)));
return null;
}
return connection.getJedis().exists(key);
} catch (Exception ex) {
throw connection.convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#exists(byte[][])
*/
@Nullable
@Override
public Long exists(byte[]... keys) {
Assert.notNull(keys, "Keys must not be null!");
Assert.noNullElements(keys, "Keys must not contain null elements!");
try {
if (isPipelined()) {
pipeline(connection.newJedisResult(connection.getRequiredPipeline().exists(keys)));
return null;
}
if (isQueueing()) {
transaction(connection.newJedisResult(connection.getRequiredTransaction().exists(keys)));
return null;
}
return connection.getJedis().exists(keys);
} catch (Exception ex) {
throw connection.convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#del(byte[][])
*/
@Override
public Long del(byte[]... keys) {
Assert.notNull(keys, "Keys must not be null!");
Assert.noNullElements(keys, "Keys must not contain null elements!");
try {
if (isPipelined()) {
pipeline(connection.newJedisResult(connection.getRequiredPipeline().del(keys)));
return null;
}
if (isQueueing()) {
transaction(connection.newJedisResult(connection.getRequiredTransaction().del(keys)));
return null;
}
return connection.getJedis().del(keys);
} catch (Exception ex) {
throw connection.convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#unlink(byte[][])
*/
@Nullable
@Override
public Long unlink(byte[]... keys) {
Assert.notNull(keys, "Keys must not be null!");
return Long.class.cast(connection.execute("UNLINK", keys));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#type(byte[])
*/
@Override
public DataType type(byte[] key) {
Assert.notNull(key, "Key must not be null!");
try {
if (isPipelined()) {
pipeline(
connection.newJedisResult(connection.getRequiredPipeline().type(key), JedisConverters.stringToDataType()));
return null;
}
if (isQueueing()) {
transaction(connection.newJedisResult(connection.getRequiredTransaction().type(key),
JedisConverters.stringToDataType()));
return null;
}
return JedisConverters.toDataType(connection.getJedis().type(key));
} catch (Exception ex) {
throw connection.convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#touch(byte[][])
*/
@Nullable
@Override
public Long touch(byte[]... keys) {
Assert.notNull(keys, "Keys must not be null!");
return Long.class.cast(connection.execute("TOUCH", keys));
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#keys(byte[])
*/
@Override
public Set keys(byte[] pattern) {
Assert.notNull(pattern, "Pattern must not be null!");
try {
if (isPipelined()) {
pipeline(connection.newJedisResult(connection.getRequiredPipeline().keys(pattern)));
return null;
}
if (isQueueing()) {
transaction(connection.newJedisResult(connection.getRequiredTransaction().keys(pattern)));
return null;
}
return connection.getJedis().keys(pattern);
} catch (Exception ex) {
throw connection.convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#scan(org.springframework.data.redis.core.ScanOptions)
*/
@Override
public Cursor scan(ScanOptions options) {
return scan(0, options != null ? options : ScanOptions.NONE);
}
/**
* @since 1.4
* @param cursorId
* @param options
* @return
*/
public Cursor scan(long cursorId, ScanOptions options) {
return new ScanCursor(cursorId, options) {
@Override
protected ScanIteration doScan(long cursorId, ScanOptions options) {
if (isQueueing() || isPipelined()) {
throw new UnsupportedOperationException("'SCAN' cannot be called in pipeline / transaction mode.");
}
ScanParams params = JedisConverters.toScanParams(options);
redis.clients.jedis.ScanResult result = connection.getJedis().scan(Long.toString(cursorId), params);
return new ScanIteration<>(Long.valueOf(result.getCursor()),
JedisConverters.stringListToByteList().convert(result.getResult()));
}
protected void doClose() {
JedisKeyCommands.this.connection.close();
};
}.open();
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#randomKey()
*/
@Override
public byte[] randomKey() {
try {
if (isPipelined()) {
pipeline(connection.newJedisResult(connection.getRequiredPipeline().randomKeyBinary()));
return null;
}
if (isQueueing()) {
transaction(connection.newJedisResult(connection.getRequiredTransaction().randomKeyBinary()));
return null;
}
return connection.getJedis().randomBinaryKey();
} catch (Exception ex) {
throw connection.convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#rename(byte[], byte[])
*/
@Override
public void rename(byte[] sourceKey, byte[] targetKey) {
Assert.notNull(sourceKey, "Source key must not be null!");
Assert.notNull(targetKey, "Target key must not be null!");
try {
if (isPipelined()) {
pipeline(connection.newStatusResult(connection.getRequiredPipeline().rename(sourceKey, targetKey)));
return;
}
if (isQueueing()) {
transaction(connection.newStatusResult(connection.getRequiredTransaction().rename(sourceKey, targetKey)));
return;
}
connection.getJedis().rename(sourceKey, targetKey);
} catch (Exception ex) {
throw connection.convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#renameNX(byte[], byte[])
*/
@Override
public Boolean renameNX(byte[] sourceKey, byte[] targetKey) {
Assert.notNull(sourceKey, "Source key must not be null!");
Assert.notNull(targetKey, "Target key must not be null!");
try {
if (isPipelined()) {
pipeline(connection.newJedisResult(connection.getRequiredPipeline().renamenx(sourceKey, targetKey),
JedisConverters.longToBoolean()));
return null;
}
if (isQueueing()) {
transaction(connection.newJedisResult(connection.getRequiredTransaction().renamenx(sourceKey, targetKey),
JedisConverters.longToBoolean()));
return null;
}
return JedisConverters.toBoolean(connection.getJedis().renamenx(sourceKey, targetKey));
} catch (Exception ex) {
throw connection.convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#expire(byte[], long)
*/
@Override
public Boolean expire(byte[] key, long seconds) {
Assert.notNull(key, "Key must not be null!");
if (seconds > Integer.MAX_VALUE) {
return pExpire(key, TimeUnit.SECONDS.toMillis(seconds));
}
try {
if (isPipelined()) {
pipeline(connection.newJedisResult(connection.getRequiredPipeline().expire(key, (int) seconds),
JedisConverters.longToBoolean()));
return null;
}
if (isQueueing()) {
transaction(connection.newJedisResult(connection.getRequiredTransaction().expire(key, (int) seconds),
JedisConverters.longToBoolean()));
return null;
}
return JedisConverters.toBoolean(connection.getJedis().expire(key, (int) seconds));
} catch (Exception ex) {
throw connection.convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#pExpire(byte[], long)
*/
@Override
public Boolean pExpire(byte[] key, long millis) {
Assert.notNull(key, "Key must not be null!");
try {
if (isPipelined()) {
pipeline(connection.newJedisResult(connection.getRequiredPipeline().pexpire(key, millis),
JedisConverters.longToBoolean()));
return null;
}
if (isQueueing()) {
transaction(connection.newJedisResult(connection.getRequiredTransaction().pexpire(key, millis),
JedisConverters.longToBoolean()));
return null;
}
return JedisConverters.toBoolean(connection.getJedis().pexpire(key, millis));
} catch (Exception ex) {
throw connection.convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#expireAt(byte[], long)
*/
@Override
public Boolean expireAt(byte[] key, long unixTime) {
Assert.notNull(key, "Key must not be null!");
try {
if (isPipelined()) {
pipeline(connection.newJedisResult(connection.getRequiredPipeline().expireAt(key, unixTime),
JedisConverters.longToBoolean()));
return null;
}
if (isQueueing()) {
transaction(connection.newJedisResult(connection.getRequiredTransaction().expireAt(key, unixTime),
JedisConverters.longToBoolean()));
return null;
}
return JedisConverters.toBoolean(connection.getJedis().expireAt(key, unixTime));
} catch (Exception ex) {
throw connection.convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#pExpireAt(byte[], long)
*/
@Override
public Boolean pExpireAt(byte[] key, long unixTimeInMillis) {
Assert.notNull(key, "Key must not be null!");
try {
if (isPipelined()) {
pipeline(connection.newJedisResult(connection.getRequiredPipeline().pexpireAt(key, unixTimeInMillis),
JedisConverters.longToBoolean()));
return null;
}
if (isQueueing()) {
transaction(connection.newJedisResult(connection.getRequiredTransaction().pexpireAt(key, unixTimeInMillis),
JedisConverters.longToBoolean()));
return null;
}
return JedisConverters.toBoolean(connection.getJedis().pexpireAt(key, unixTimeInMillis));
} catch (Exception ex) {
throw connection.convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#persist(byte[])
*/
@Override
public Boolean persist(byte[] key) {
Assert.notNull(key, "Key must not be null!");
try {
if (isPipelined()) {
pipeline(
connection.newJedisResult(connection.getRequiredPipeline().persist(key), JedisConverters.longToBoolean()));
return null;
}
if (isQueueing()) {
transaction(connection.newJedisResult(connection.getRequiredTransaction().persist(key),
JedisConverters.longToBoolean()));
return null;
}
return JedisConverters.toBoolean(connection.getJedis().persist(key));
} catch (Exception ex) {
throw connection.convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#move(byte[], int)
*/
@Override
public Boolean move(byte[] key, int dbIndex) {
Assert.notNull(key, "Key must not be null!");
try {
if (isPipelined()) {
pipeline(connection.newJedisResult(connection.getRequiredPipeline().move(key, dbIndex),
JedisConverters.longToBoolean()));
return null;
}
if (isQueueing()) {
transaction(connection.newJedisResult(connection.getRequiredTransaction().move(key, dbIndex),
JedisConverters.longToBoolean()));
return null;
}
return JedisConverters.toBoolean(connection.getJedis().move(key, dbIndex));
} catch (Exception ex) {
throw connection.convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#ttl(byte[])
*/
@Override
public Long ttl(byte[] key) {
Assert.notNull(key, "Key must not be null!");
try {
if (isPipelined()) {
pipeline(connection.newJedisResult(connection.getRequiredPipeline().ttl(key)));
return null;
}
if (isQueueing()) {
transaction(connection.newJedisResult(connection.getRequiredTransaction().ttl(key)));
return null;
}
return connection.getJedis().ttl(key);
} catch (Exception ex) {
throw connection.convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#ttl(byte[], java.util.concurrent.TimeUnit)
*/
@Override
public Long ttl(byte[] key, TimeUnit timeUnit) {
Assert.notNull(key, "Key must not be null!");
try {
if (isPipelined()) {
pipeline(connection.newJedisResult(connection.getRequiredPipeline().ttl(key),
Converters.secondsToTimeUnit(timeUnit)));
return null;
}
if (isQueueing()) {
transaction(connection.newJedisResult(connection.getRequiredTransaction().ttl(key),
Converters.secondsToTimeUnit(timeUnit)));
return null;
}
return Converters.secondsToTimeUnit(connection.getJedis().ttl(key), timeUnit);
} catch (Exception ex) {
throw connection.convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#pTtl(byte[])
*/
@Override
public Long pTtl(byte[] key) {
Assert.notNull(key, "Key must not be null!");
try {
if (isPipelined()) {
pipeline(connection.newJedisResult(connection.getRequiredPipeline().pttl(key)));
return null;
}
if (isQueueing()) {
transaction(connection.newJedisResult(connection.getRequiredTransaction().pttl(key)));
return null;
}
return connection.getJedis().pttl(key);
} catch (Exception ex) {
throw connection.convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#pTtl(byte[], java.util.concurrent.TimeUnit)
*/
@Override
public Long pTtl(byte[] key, TimeUnit timeUnit) {
Assert.notNull(key, "Key must not be null!");
try {
if (isPipelined()) {
pipeline(connection.newJedisResult(connection.getRequiredPipeline().pttl(key),
Converters.millisecondsToTimeUnit(timeUnit)));
return null;
}
if (isQueueing()) {
transaction(connection.newJedisResult(connection.getRequiredTransaction().pttl(key),
Converters.millisecondsToTimeUnit(timeUnit)));
return null;
}
return Converters.millisecondsToTimeUnit(connection.getJedis().pttl(key), timeUnit);
} catch (Exception ex) {
throw connection.convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#sort(byte[], org.springframework.data.redis.connection.SortParameters)
*/
@Override
public List sort(byte[] key, SortParameters params) {
Assert.notNull(key, "Key must not be null!");
SortingParams sortParams = JedisConverters.toSortingParams(params);
try {
if (isPipelined()) {
if (sortParams != null) {
pipeline(connection.newJedisResult(connection.getRequiredPipeline().sort(key, sortParams)));
} else {
pipeline(connection.newJedisResult(connection.getRequiredPipeline().sort(key)));
}
return null;
}
if (isQueueing()) {
if (sortParams != null) {
transaction(connection.newJedisResult(connection.getRequiredTransaction().sort(key, sortParams)));
} else {
transaction(connection.newJedisResult(connection.getRequiredTransaction().sort(key)));
}
return null;
}
return (sortParams != null ? connection.getJedis().sort(key, sortParams) : connection.getJedis().sort(key));
} catch (Exception ex) {
throw connection.convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#sort(byte[], org.springframework.data.redis.connection.SortParameters, byte[])
*/
@Override
public Long sort(byte[] key, @Nullable SortParameters params, byte[] storeKey) {
Assert.notNull(key, "Key must not be null!");
SortingParams sortParams = JedisConverters.toSortingParams(params);
try {
if (isPipelined()) {
if (sortParams != null) {
pipeline(connection.newJedisResult(connection.getRequiredPipeline().sort(key, sortParams, storeKey)));
} else {
pipeline(connection.newJedisResult(connection.getRequiredPipeline().sort(key, storeKey)));
}
return null;
}
if (isQueueing()) {
if (sortParams != null) {
transaction(connection.newJedisResult(connection.getRequiredTransaction().sort(key, sortParams, storeKey)));
} else {
transaction(connection.newJedisResult(connection.getRequiredTransaction().sort(key, storeKey)));
}
return null;
}
return (sortParams != null ? connection.getJedis().sort(key, sortParams, storeKey)
: connection.getJedis().sort(key, storeKey));
} catch (Exception ex) {
throw connection.convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#dump(byte[])
*/
@Override
public byte[] dump(byte[] key) {
Assert.notNull(key, "Key must not be null!");
try {
if (isPipelined()) {
pipeline(connection.newJedisResult(connection.getRequiredPipeline().dump(key)));
return null;
}
if (isQueueing()) {
transaction(connection.newJedisResult(connection.getRequiredTransaction().dump(key)));
return null;
}
return connection.getJedis().dump(key);
} catch (Exception ex) {
throw connection.convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#restore(byte[], long, byte[], boolean)
*/
@Override
public void restore(byte[] key, long ttlInMillis, byte[] serializedValue, boolean replace) {
Assert.notNull(key, "Key must not be null!");
Assert.notNull(serializedValue, "Serialized value must not be null!");
if (replace) {
this.connection.execute("RESTORE", new byte[][] { key, JedisConverters.toBytes(ttlInMillis), serializedValue,
JedisConverters.toBytes("REPLACE") });
return;
}
if (ttlInMillis > Integer.MAX_VALUE) {
throw new IllegalArgumentException("TtlInMillis must be less than Integer.MAX_VALUE for restore in Jedis.");
}
try {
if (isPipelined()) {
pipeline(connection
.newStatusResult(connection.getRequiredPipeline().restore(key, (int) ttlInMillis, serializedValue)));
return;
}
if (isQueueing()) {
transaction(connection
.newStatusResult(connection.getRequiredTransaction().restore(key, (int) ttlInMillis, serializedValue)));
return;
}
connection.getJedis().restore(key, (int) ttlInMillis, serializedValue);
} catch (Exception ex) {
throw connection.convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#encoding(byte[])
*/
@Nullable
@Override
public ValueEncoding encodingOf(byte[] key) {
Assert.notNull(key, "Key must not be null!");
try {
if (isPipelined()) {
pipeline(connection.newJedisResult(connection.getRequiredPipeline().objectEncoding(key),
JedisConverters::toEncoding, () -> RedisValueEncoding.VACANT));
return null;
}
if (isQueueing()) {
transaction(connection.newJedisResult(connection.getRequiredTransaction().objectEncoding(key),
JedisConverters::toEncoding, () -> RedisValueEncoding.VACANT));
return null;
}
return JedisConverters.toEncoding(connection.getJedis().objectEncoding(key));
} catch (Exception ex) {
throw connection.convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#idletime(byte[])
*/
@Nullable
@Override
public Duration idletime(byte[] key) {
Assert.notNull(key, "Key must not be null!");
try {
if (isPipelined()) {
pipeline(connection.newJedisResult(connection.getRequiredPipeline().objectIdletime(key),
Converters::secondsToDuration));
return null;
}
if (isQueueing()) {
transaction(connection.newJedisResult(connection.getRequiredTransaction().objectIdletime(key),
Converters::secondsToDuration));
return null;
}
return Converters.secondsToDuration(connection.getJedis().objectIdletime(key));
} catch (Exception ex) {
throw connection.convertJedisAccessException(ex);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.RedisKeyCommands#refcount(byte[])
*/
@Nullable
@Override
public Long refcount(byte[] key) {
Assert.notNull(key, "Key must not be null!");
try {
if (isPipelined()) {
pipeline(connection.newJedisResult(connection.getRequiredPipeline().objectRefcount(key)));
return null;
}
if (isQueueing()) {
transaction(connection.newJedisResult(connection.getRequiredTransaction().objectRefcount(key)));
return null;
}
return connection.getJedis().objectRefcount(key);
} catch (Exception ex) {
throw connection.convertJedisAccessException(ex);
}
}
private boolean isPipelined() {
return connection.isPipelined();
}
private void pipeline(JedisResult result) {
connection.pipeline(result);
}
private boolean isQueueing() {
return connection.isQueueing();
}
private void transaction(JedisResult result) {
connection.transaction(result);
}
}