Class JedisConnection
java.lang.Object
org.springframework.data.redis.connection.AbstractRedisConnection
org.springframework.data.redis.connection.jedis.JedisConnection
- All Implemented Interfaces:
AutoCloseable
,DefaultedRedisConnection
,RedisCommands
,RedisCommandsProvider
,RedisConnection
,RedisConnectionCommands
,RedisGeoCommands
,RedisHashCommands
,RedisHyperLogLogCommands
,RedisKeyCommands
,RedisListCommands
,RedisPubSubCommands
,RedisScriptingCommands
,RedisServerCommands
,RedisSetCommands
,RedisStreamCommands
,RedisStringCommands
,RedisTxCommands
,RedisZSetCommands
RedisConnection
implementation on top of Jedis library.
This class is not Thread-safe and instances should not be shared across threads.
- Author:
- Costin Leau, Jennifer Hickey, Christoph Strobl, Thomas Darimont, Jungtaek Lim, Konstantin Shchepanovskyi, David Liu, Milan Agatonovic, Mark Paluch, Ninad Divadkar, Guy Korland, Dengliming, John Blum
- See Also:
-
Jedis
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.springframework.data.redis.connection.RedisGeoCommands
RedisGeoCommands.DistanceUnit, RedisGeoCommands.GeoCommandArgs, RedisGeoCommands.GeoLocation<T>, RedisGeoCommands.GeoRadiusCommandArgs, RedisGeoCommands.GeoSearchCommandArgs, RedisGeoCommands.GeoSearchStoreCommandArgs
Nested classes/interfaces inherited from interface org.springframework.data.redis.connection.RedisListCommands
RedisListCommands.Direction, RedisListCommands.Position
Nested classes/interfaces inherited from interface org.springframework.data.redis.connection.RedisServerCommands
RedisServerCommands.FlushOption, RedisServerCommands.MigrateOption, RedisServerCommands.ShutdownOption
Nested classes/interfaces inherited from interface org.springframework.data.redis.connection.RedisStreamCommands
RedisStreamCommands.XAddOptions, RedisStreamCommands.XClaimOptions, RedisStreamCommands.XPendingOptions
Nested classes/interfaces inherited from interface org.springframework.data.redis.connection.RedisStringCommands
RedisStringCommands.BitOperation, RedisStringCommands.SetOption
Nested classes/interfaces inherited from interface org.springframework.data.redis.connection.RedisZSetCommands
RedisZSetCommands.Limit, RedisZSetCommands.Range, RedisZSetCommands.ZAddArgs
-
Constructor Summary
ModifierConstructorDescriptionJedisConnection
(redis.clients.jedis.Jedis jedis) Constructs a newJedisConnection
instance.JedisConnection
(redis.clients.jedis.Jedis jedis, redis.clients.jedis.util.Pool<redis.clients.jedis.Jedis> pool, int dbIndex) Constructs a newJedisConnection
instance backed by a jedis pool.protected
JedisConnection
(redis.clients.jedis.Jedis jedis, redis.clients.jedis.util.Pool<redis.clients.jedis.Jedis> pool, int dbIndex, String clientName) Constructs a newJedisConnection
instance backed by a jedis pool.protected
JedisConnection
(redis.clients.jedis.Jedis jedis, redis.clients.jedis.util.Pool<redis.clients.jedis.Jedis> pool, redis.clients.jedis.JedisClientConfig nodeConfig, redis.clients.jedis.JedisClientConfig sentinelConfig) Constructs a newJedisConnection
instance backed by a jedis pool. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes (or quits) the connection.Executes the commands in the pipeline and returns their result.commands()
GetRedisCommands
.protected DataAccessException
void
discard()
Discard all commands issued afterRedisTxCommands.multi()
.byte[]
echo
(byte[] message) Returnsmessage
via server roundtrip.exec()
Executes all queued commands in a transaction started withRedisTxCommands.multi()
.Native or raw execution of the given Redis command along with the given arguments.GetRedisGeoCommands
.redis.clients.jedis.Jedis
getJedis()
protected redis.clients.jedis.Jedis
redis.clients.jedis.Jedis
Returns the native connection (the underlying library/driver object).redis.clients.jedis.Pipeline
redis.clients.jedis.Pipeline
redis.clients.jedis.Transaction
protected JedisSentinelConnection
getSentinelConnection
(RedisNode sentinel) GetRedisSentinelCommands
connected to given node.Returns the current subscription for this connection or null if the connection is not subscribed.redis.clients.jedis.Transaction
GetRedisHashCommands
.protected boolean
Check if node is active by sending ping.boolean
isClosed()
Indicates whether the underlying connection is closed or not.boolean
Indicates whether the connection is currently pipelined or not.boolean
Indicates whether the connection is in "queue"(or "MULTI") mode or not.boolean
Indicates whether the current connection is subscribed (to at least one channel) or not.GetRedisKeyCommands
.GetRedisListCommands
.void
multi()
Mark the start of a transaction block.void
Activates the pipeline mode for this connection.ping()
Test connection.void
pSubscribe
(MessageListener listener, byte[]... patterns) Subscribes the connection to all channels matching the given patterns.publish
(byte[] channel, byte[] message) Publishes the given message to the given channel.void
select
(int dbIndex) Select the DB with given positivedbIndex
.GetRedisServerCommands
.GetRedisSetCommands
.void
setConvertPipelineAndTxResults
(boolean convertPipelineAndTxResults) Specifies if pipelined results should be converted to the expected data type.GetRedisStreamCommands
.GetRedisStringCommands
.void
subscribe
(MessageListener listener, byte[]... channels) Subscribes the connection to the given channels.void
unwatch()
Flushes all the previouslyRedisTxCommands.watch(byte[]...)
keys.void
watch
(byte[]... keys) Watch givenkeys
for modifications during transaction started withRedisTxCommands.multi()
.GetRedisZSetCommands
.Methods inherited from class org.springframework.data.redis.connection.AbstractRedisConnection
getSentinelConnection, hasRedisSentinelConfigured, setSentinelConfiguration
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.data.redis.connection.DefaultedRedisConnection
append, bgReWriteAof, bgSave, bitCount, bitCount, bitField, bitOp, bitPos, bLMove, bLPop, bRPop, bRPopLPush, bZPopMax, bZPopMin, copy, dbSize, decr, decrBy, del, dump, encodingOf, eval, evalSha, evalSha, exists, exists, expire, expireAt, flushAll, flushAll, flushDb, flushDb, geoAdd, geoAdd, geoAdd, geoDist, geoDist, geoHash, geoPos, geoRadius, geoRadius, geoRadiusByMember, geoRadiusByMember, geoRemove, geoSearch, geoSearchStore, get, getBit, getClientList, getClientName, getConfig, getDel, getEx, getRange, getSet, hDel, hExists, hGet, hGetAll, hIncrBy, hIncrBy, hKeys, hLen, hMGet, hMSet, hRandField, hRandField, hRandFieldWithValues, hRandFieldWithValues, hScan, hSet, hSetNX, hStrLen, hVals, idletime, incr, incrBy, incrBy, info, info, keys, killClient, lastSave, lIndex, lInsert, lLen, lMove, lPop, lPop, lPos, lPush, lPushX, lRange, lRem, lSet, lTrim, mGet, migrate, migrate, move, mSet, mSetNX, persist, pExpire, pExpireAt, pfAdd, pfCount, pfMerge, pSetEx, pTtl, pTtl, randomKey, refcount, rename, renameNX, replicaOf, replicaOfNoOne, resetConfigStats, restore, rewriteConfig, rPop, rPop, rPopLPush, rPush, rPushX, sAdd, save, scan, sCard, scriptExists, scriptFlush, scriptKill, scriptLoad, sDiff, sDiffStore, set, set, setBit, setClientName, setConfig, setEx, setNX, setRange, shutdown, shutdown, sInter, sInterStore, sIsMember, sMembers, sMIsMember, sMove, sort, sort, sPop, sPop, sRandMember, sRandMember, sRem, sScan, strLen, sUnion, sUnionStore, time, time, touch, ttl, ttl, type, unlink, xAck, xAdd, xClaim, xClaimJustId, xDel, xGroupCreate, xGroupCreate, xGroupDelConsumer, xGroupDestroy, xInfo, xInfoConsumers, xInfoGroups, xLen, xPending, xPending, xRange, xRange, xRead, xRead, xReadGroup, xReadGroup, xRevRange, xRevRange, xTrim, xTrim, zAdd, zAdd, zCard, zCount, zCount, zDiff, zDiffStore, zDiffWithScores, zIncrBy, zInter, zInterStore, zInterStore, zInterStore, zInterWithScores, zInterWithScores, zInterWithScores, zLexCount, zMScore, zPopMax, zPopMax, zPopMin, zPopMin, zRandMember, zRandMember, zRandMemberWithScore, zRandMemberWithScore, zRange, zRangeByLex, zRangeByScore, zRangeByScore, zRangeByScore, zRangeByScoreWithScores, zRangeStoreByLex, zRangeStoreByScore, zRangeStoreRevByLex, zRangeStoreRevByScore, zRangeWithScores, zRank, zRem, zRemRange, zRemRangeByLex, zRemRangeByScore, zRemRangeByScore, zRevRange, zRevRangeByLex, zRevRangeByScore, zRevRangeByScoreWithScores, zRevRangeWithScores, zRevRank, zScan, zScore, zUnion, zUnionStore, zUnionStore, zUnionStore, zUnionWithScores, zUnionWithScores, zUnionWithScores
Methods inherited from interface org.springframework.data.redis.connection.RedisGeoCommands
geoAdd, geoRadiusByMember
Methods inherited from interface org.springframework.data.redis.connection.RedisKeyCommands
restore, scan
Methods inherited from interface org.springframework.data.redis.connection.RedisListCommands
lPos
Methods inherited from interface org.springframework.data.redis.connection.RedisStreamCommands
xAck, xAdd, xAdd, xClaim, xDel, xGroupDelConsumer, xPending, xPending, xPending, xPending, xPending
Methods inherited from interface org.springframework.data.redis.connection.RedisStringCommands
bitPos
Methods inherited from interface org.springframework.data.redis.connection.RedisZSetCommands
zAdd, zAdd, zRangeByLex, zRangeByLex, zRangeByScore, zRangeByScore, zRangeByScore, zRangeByScoreWithScores, zRangeByScoreWithScores, zRangeByScoreWithScores, zRangeStoreByLex, zRangeStoreByScore, zRangeStoreRevByLex, zRangeStoreRevByScore, zRevRangeByLex, zRevRangeByLex, zRevRangeByScore, zRevRangeByScore, zRevRangeByScore, zRevRangeByScoreWithScores, zRevRangeByScoreWithScores, zRevRangeByScoreWithScores
-
Constructor Details
-
JedisConnection
public JedisConnection(redis.clients.jedis.Jedis jedis) Constructs a newJedisConnection
instance.- Parameters:
jedis
- Jedis entity
-
JedisConnection
public JedisConnection(redis.clients.jedis.Jedis jedis, redis.clients.jedis.util.Pool<redis.clients.jedis.Jedis> pool, int dbIndex) Constructs a newJedisConnection
instance backed by a jedis pool.- Parameters:
jedis
-pool
- can be null, if no pool is useddbIndex
-
-
JedisConnection
protected JedisConnection(redis.clients.jedis.Jedis jedis, @Nullable redis.clients.jedis.util.Pool<redis.clients.jedis.Jedis> pool, int dbIndex, @Nullable String clientName) Constructs a newJedisConnection
instance backed by a jedis pool.- Parameters:
jedis
-pool
- can be null, if no pool is useddbIndex
-clientName
- the client name, can be null.- Since:
- 1.8
-
JedisConnection
protected JedisConnection(redis.clients.jedis.Jedis jedis, @Nullable redis.clients.jedis.util.Pool<redis.clients.jedis.Jedis> pool, redis.clients.jedis.JedisClientConfig nodeConfig, redis.clients.jedis.JedisClientConfig sentinelConfig) Constructs a newJedisConnection
instance backed by a jedis pool.- Parameters:
jedis
-pool
- can be null, if no pool is usednodeConfig
- node configurationsentinelConfig
- sentinel configuration- Since:
- 2.5
-
-
Method Details
-
convertJedisAccessException
-
commands
Description copied from interface:RedisCommandsProvider
GetRedisCommands
.- Returns:
- never null.
-
geoCommands
Description copied from interface:RedisCommandsProvider
GetRedisGeoCommands
.- Returns:
- never null.
-
hashCommands
Description copied from interface:RedisCommandsProvider
GetRedisHashCommands
.- Returns:
- never null.
-
hyperLogLogCommands
Description copied from interface:RedisCommandsProvider
- Returns:
- never null.
-
keyCommands
Description copied from interface:RedisCommandsProvider
GetRedisKeyCommands
.- Returns:
- never null.
-
listCommands
Description copied from interface:RedisCommandsProvider
GetRedisListCommands
.- Returns:
- never null.
-
setCommands
Description copied from interface:RedisCommandsProvider
GetRedisSetCommands
.- Returns:
- never null.
-
streamCommands
Description copied from interface:RedisCommandsProvider
GetRedisStreamCommands
.- Returns:
- never null.
-
stringCommands
Description copied from interface:RedisCommandsProvider
GetRedisStringCommands
.- Returns:
- never null.
-
zSetCommands
Description copied from interface:RedisCommandsProvider
GetRedisZSetCommands
.- Returns:
- never null.
-
scriptingCommands
Description copied from interface:RedisCommandsProvider
- Returns:
- never null.
-
serverCommands
Description copied from interface:RedisCommandsProvider
GetRedisServerCommands
.- Returns:
- never null.
-
execute
Description copied from interface:RedisCommands
Native or raw execution of the given Redis command along with the given arguments.The command is executed as is, with as little interpretation as possible - it is up to the caller to take care of any processing of arguments or the result.
- Parameters:
command
- Rediscommand
to execute; must not be null.args
- optional array of command arguments; may be empty;- Returns:
- the execution result; may be null.
-
close
Description copied from interface:RedisConnection
Closes (or quits) the connection.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceRedisConnection
- Overrides:
close
in classAbstractRedisConnection
- Throws:
DataAccessException
-
getNativeConnection
public redis.clients.jedis.Jedis getNativeConnection()Description copied from interface:RedisConnection
Returns the native connection (the underlying library/driver object).- Returns:
- underlying, native object
-
isClosed
public boolean isClosed()Description copied from interface:RedisConnection
Indicates whether the underlying connection is closed or not.- Returns:
- true if the connection is closed, false otherwise.
-
isQueueing
public boolean isQueueing()Description copied from interface:RedisConnection
Indicates whether the connection is in "queue"(or "MULTI") mode or not. When queueing, all commands are postponed until EXEC or DISCARD commands are issued. Since in queueing no results are returned, the connection will return NULL on all operations that interact with the data.- Returns:
- true if the connection is in queue/MULTI mode, false otherwise
-
isPipelined
public boolean isPipelined()Description copied from interface:RedisConnection
Indicates whether the connection is currently pipelined or not.- Returns:
- true if the connection is pipelined, false otherwise
- See Also:
-
openPipeline
public void openPipeline()Description copied from interface:RedisConnection
Activates the pipeline mode for this connection. When pipelined, all commands return null (the reply is read at the end throughRedisConnection.closePipeline()
. Calling this method when the connection is already pipelined has no effect. Pipelining is used for issuing commands without requesting the response right away but rather at the end of the batch. While somewhat similar to MULTI, pipelining does not guarantee atomicity - it only tries to improve performance when issuing a lot of commands (such as in batching scenarios).Note:
Consider doing some performance testing before using this feature since in many cases the performance benefits are minimal yet the impact on usage are not.- See Also:
-
closePipeline
Description copied from interface:RedisConnection
Executes the commands in the pipeline and returns their result. If the connection is not pipelined, an empty collection is returned.- Returns:
- the result of the executed commands.
-
echo
public byte[] echo(byte[] message) Description copied from interface:RedisConnectionCommands
Returnsmessage
via server roundtrip.- Parameters:
message
- the message to echo.- Returns:
- the message or null when used in pipeline / transaction.
- See Also:
-
ping
Description copied from interface:RedisConnectionCommands
Test connection.- Returns:
- Server response message - usually PONG. null when used in pipeline / transaction.
- See Also:
-
discard
public void discard()Description copied from interface:RedisTxCommands
Discard all commands issued afterRedisTxCommands.multi()
.- See Also:
-
exec
Description copied from interface:RedisTxCommands
Executes all queued commands in a transaction started withRedisTxCommands.multi()
.
If used along withRedisTxCommands.watch(byte[]...)
the operation will fail if any of watched keys has been modified.- Returns:
- List of replies for each executed command.
- See Also:
-
getPipeline
-
getRequiredPipeline
public redis.clients.jedis.Pipeline getRequiredPipeline() -
getTransaction
-
getRequiredTransaction
public redis.clients.jedis.Transaction getRequiredTransaction() -
getJedis
public redis.clients.jedis.Jedis getJedis() -
multi
public void multi()Description copied from interface:RedisTxCommands
Mark the start of a transaction block.
Commands will be queued and can then be executed by callingRedisTxCommands.exec()
or rolled back usingRedisTxCommands.discard()
- See Also:
-
select
public void select(int dbIndex) Description copied from interface:RedisConnectionCommands
Select the DB with given positivedbIndex
.- Parameters:
dbIndex
- the database index.- See Also:
-
unwatch
public void unwatch()Description copied from interface:RedisTxCommands
Flushes all the previouslyRedisTxCommands.watch(byte[]...)
keys.- See Also:
-
watch
public void watch(byte[]... keys) Description copied from interface:RedisTxCommands
Watch givenkeys
for modifications during transaction started withRedisTxCommands.multi()
.- Parameters:
keys
- must not be null.- See Also:
-
publish
Description copied from interface:RedisPubSubCommands
Publishes the given message to the given channel.- Parameters:
channel
- the channel to publish to. Must not be null.message
- message to publish. Must not be null.- Returns:
- the number of clients that received the message or null when used in pipeline / transaction.
- See Also:
-
getSubscription
Description copied from interface:RedisPubSubCommands
Returns the current subscription for this connection or null if the connection is not subscribed.- Returns:
- the current subscription, null if none is available.
-
isSubscribed
public boolean isSubscribed()Description copied from interface:RedisPubSubCommands
Indicates whether the current connection is subscribed (to at least one channel) or not.- Returns:
- true if the connection is subscribed, false otherwise
-
pSubscribe
Description copied from interface:RedisPubSubCommands
Subscribes the connection to all channels matching the given patterns. Once subscribed, a connection enters listening mode and can only subscribe to other channels or unsubscribe. No other commands are accepted until the connection is unsubscribed.Note that this operation is blocking and the current thread starts waiting for new messages immediately.
- Parameters:
listener
- message listener, must not be null.patterns
- channel name patterns, must not be null.- See Also:
-
subscribe
Description copied from interface:RedisPubSubCommands
Subscribes the connection to the given channels. Once subscribed, a connection enters listening mode and can only subscribe to other channels or unsubscribe. No other commands are accepted until the connection is unsubscribed.Note that this operation is blocking and the current thread starts waiting for new messages immediately.
- Parameters:
listener
- message listener, must not be null.channels
- channel names, must not be null.- See Also:
-
setConvertPipelineAndTxResults
public void setConvertPipelineAndTxResults(boolean convertPipelineAndTxResults) Specifies if pipelined results should be converted to the expected data type. If false, results ofclosePipeline()
andexec()
will be of the type returned by the Jedis driver- Parameters:
convertPipelineAndTxResults
- Whether or not to convert pipeline and tx results
-
isActive
Description copied from class:AbstractRedisConnection
Check if node is active by sending ping.- Overrides:
isActive
in classAbstractRedisConnection
- Returns:
-
getSentinelConnection
Description copied from class:AbstractRedisConnection
GetRedisSentinelCommands
connected to given node.- Overrides:
getSentinelConnection
in classAbstractRedisConnection
- Returns:
-
getJedis
-