Class RedisServerConnection
- java.lang.Object
-
- com.blackrook.redis.RedisConnectionAbstract
-
- com.blackrook.redis.RedisServerConnection
-
- All Implemented Interfaces:
RedisServerCommands,AutoCloseable
public class RedisServerConnection extends RedisConnectionAbstract implements RedisServerCommands
A connection to a Redis server primarily for server and administrative operations.- Author:
- Matthew Tropiano
-
-
Field Summary
-
Fields inherited from class com.blackrook.redis.RedisConnectionAbstract
reader, writer
-
-
Constructor Summary
Constructors Constructor Description RedisServerConnection()Creates an open connection to localhost, port 6379, the default Redis port.RedisServerConnection(RedisInfo info)Creates an open connection.RedisServerConnection(String host, int port)Creates an open connection.RedisServerConnection(String host, int port, String password)Creates an open connection.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanbgrewriteaof()booleanbgsave()booleanclientKill(String ip, int port)String[]clientList()booleanclientPause(long millis)StringconfigGet(String configKey)booleanconfigResetStat()booleanconfigRewrite()booleanconfigSet(String parameter, String value)longdbsize()booleanflushall()booleanflushdb()Stringinfo()Stringinfo(String section)Map<String,String>infoMap()Map<String,String>infoMap(String section)longlastsave()booleanmigrate(String host, int port, String key, long destinationDB, long timeout)booleanmigrate(String host, int port, String key, long destinationDB, long timeout, boolean copy, boolean replace)RedisObjectobject(String subcommand, String key)EncodingTypeobjectEncoding(String key)longobjectIdletime(String key)longobjectRefcount(String key)RedisObjectpubsub(String subcommand, String... arguments)String[]pubsubChannels(String pattern)longpubsubNumpat()String[]pubsubNumsub(String... arguments)booleansave()voidshutdown(boolean save)booleanslaveof(String host, String port)booleanslaveofNoOne()RedisObjectslowlog(String subcommand, String argument)RedisObjectslowlogGet(long recentCount)RedisObjectslowlogLen()RedisObjectslowlogReset()long[]time()-
Methods inherited from class com.blackrook.redis.RedisConnectionAbstract
close, disconnect, isBound, isClosed, isConnected, reconnect
-
-
-
-
Constructor Detail
-
RedisServerConnection
public RedisServerConnection() throws IOExceptionCreates an open connection to localhost, port 6379, the default Redis port.- Throws:
IOException- if an I/O error occurs when creating the socket.UnknownHostException- if the IP address of the host could not be determined.SecurityException- if a security manager exists and doesn't allow the connection to be made.
-
RedisServerConnection
public RedisServerConnection(String host, int port) throws IOException
Creates an open connection.- Parameters:
host- the server hostname or address.port- the server connection port.- Throws:
IOException- if an I/O error occurs when creating the socket.UnknownHostException- if the IP address of the host could not be determined.SecurityException- if a security manager exists and doesn't allow the connection to be made.
-
RedisServerConnection
public RedisServerConnection(String host, int port, String password) throws IOException
Creates an open connection.- Parameters:
host- the server hostname or address.port- the server connection port.password- the server database password.- Throws:
IOException- if an I/O error occurs when creating the socket.UnknownHostException- if the IP address of the host could not be determined.SecurityException- if a security manager exists and doesn't allow the connection to be made.RedisException- if the password in the server information is incorrect.
-
RedisServerConnection
public RedisServerConnection(RedisInfo info) throws IOException
Creates an open connection.- Parameters:
info- theRedisInfoclass detailing a connection.- Throws:
IOException- if an I/O error occurs when creating the socket.UnknownHostException- if the IP address of the host could not be determined.SecurityException- if a security manager exists and doesn't allow the connection to be made.RedisException- if the password in the server information is incorrect.
-
-
Method Detail
-
bgrewriteaof
public boolean bgrewriteaof()
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/bgrewriteaof:
Available since 1.0.0.
Instruct Redis to start an Append Only File rewrite process. The rewrite will create a small optimized version of the current Append Only File.
- Specified by:
bgrewriteaofin interfaceRedisServerCommands- Returns:
- always true.
-
bgsave
public boolean bgsave()
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/bgsave:
Available since 1.0.0.
Save the DB in background. The OK code is immediately returned. Redis forks, the parent continues to serve the clients, the child saves the DB on disk then exits. A client my be able to check if the operation succeeded using the
RedisServerCommands.lastsave()command.- Specified by:
bgsavein interfaceRedisServerCommands- Returns:
- always true.
-
clientKill
public boolean clientKill(String ip, int port)
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/client-kill:
Available since 2.4.0.
Time complexity: O(N) where N is the number of client connections
The
CLIENT KILLcommand closes a given client connection identified by ip:port.- Specified by:
clientKillin interfaceRedisServerCommands- Parameters:
ip- the IP address or hostname.port- the port number.- Returns:
- true once the client connection was closed.
-
clientList
public String[] clientList()
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/client-list:
Available since 2.4.0.
Time complexity: O(N) where N is the number of client connections
The
CLIENT LISTcommand returns information and statistics about the client connections server in a mostly human readable format.- Specified by:
clientListin interfaceRedisServerCommands- Returns:
- a list of client strings that describe each connection.
-
clientPause
public boolean clientPause(long millis)
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/client-pause:
Available since 2.9.50.
Time complexity: O(1)
CLIENT PAUSEis a connections control command able to suspend all the Redis clients for the specified amount of time (in milliseconds).- Specified by:
clientPausein interfaceRedisServerCommands- Parameters:
millis- the amount of time in milliseconds.- Returns:
- always true.
-
configGet
public String configGet(String configKey)
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/config-get:
Available since 2.0.0.
The
CONFIG GETcommand is used to read the configuration parameters of a running Redis server. Not all the configuration parameters are supported in Redis 2.4, while Redis 2.6 can read the whole configuration of a server using this command.- Specified by:
configGetin interfaceRedisServerCommands- Parameters:
configKey- the config key.- Returns:
- the config value.
-
configRewrite
public boolean configRewrite()
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/config-rewrite:
Available since 2.8.0.
The
CONFIG REWRITEcommand rewrites theredis.conffile the server was started with, applying the minimal changes needed to make it reflecting the configuration currently used by the server, that may be different compared to the original one because of the use of theRedisServerCommands.configSet(java.lang.String, java.lang.String)command.- Specified by:
configRewritein interfaceRedisServerCommands- Returns:
- always true.
-
configSet
public boolean configSet(String parameter, String value)
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/config-set:
Available since 2.0.0.
The
CONFIG SETcommand is used in order to reconfigure the server at run time without the need to restart Redis. You can change both trivial parameters or switch from one to another persistence option using this command.- Specified by:
configSetin interfaceRedisServerCommands- Parameters:
parameter- the parameter.value- the value.- Returns:
- always true.
-
configResetStat
public boolean configResetStat()
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/config-resetstat:
Available since 2.0.0.
Time complexity: O(1)
Resets the statistics reported by Redis using the INFO command.
- Specified by:
configResetStatin interfaceRedisServerCommands- Returns:
- always true.
-
dbsize
public long dbsize()
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/dbsize:
Available since 1.0.0.
Return the number of keys in the currently-selected database.
- Specified by:
dbsizein interfaceRedisServerCommands- Returns:
- the number of keys.
-
flushall
public boolean flushall()
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/flushall:
Available since 1.0.0.
Delete all the keys of all the existing databases, not just the currently selected one. This command never fails.
- Specified by:
flushallin interfaceRedisServerCommands- Returns:
- always true.
-
flushdb
public boolean flushdb()
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/flushdb:
Available since 1.0.0.
Delete all the keys of the currently selected DB. This command never fails.
- Specified by:
flushdbin interfaceRedisServerCommands- Returns:
- true if successful, false otherwise.
-
info
public String info()
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/info:
Available since 1.0.0.
The INFO command returns information and statistics about the server in a format that is simple to parse by computers and easy to read by humans.
- Specified by:
infoin interfaceRedisServerCommands- Returns:
- a collection of text lines parseable for info.
-
info
public String info(String section)
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/info:
Available since 1.0.0.
The INFO command returns information and statistics about the server in a format that is simple to parse by computers and easy to read by humans.
- Specified by:
infoin interfaceRedisServerCommands- Parameters:
section- the section to get info on.- Returns:
- a collection of text lines parseable for info.
-
lastsave
public long lastsave()
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/lastsave:
Available since 1.0.0.
Return the UNIX TIME of the last DB save executed with success. A client may check if a
RedisServerCommands.bgsave()command succeeded reading theRedisServerCommands.lastsave()value, then issuing aRedisServerCommands.bgsave()command and checking at regular intervals every N seconds ifRedisServerCommands.lastsave()changed.- Specified by:
lastsavein interfaceRedisServerCommands- Returns:
- a UNIX time stamp.
-
migrate
public boolean migrate(String host, int port, String key, long destinationDB, long timeout)
From http://redis.io/commands/migrate:
Available since 2.6.0.
Time complexity: This command actually executes a
RedisConnectionCommands.dump(java.lang.String)andRedisConnectionCommands.del(java.lang.String, java.lang.String...)in the source instance, and aRedisConnectionCommands.restore(java.lang.String, long, java.lang.String)in the target instance. See the pages of these commands for time complexity. Also an O(N) data transfer between the two instances is performed.Atomically transfer a key from a source Redis instance to a destination Redis instance. On success the key is deleted from the original instance and is guaranteed to exist in the target instance.
- Parameters:
host- the hostname/address of the target server.port- the port.key- the key to migrate.destinationDB- the database to target on the server.timeout- the timeout for the connection.- Returns:
- always true.
-
migrate
public boolean migrate(String host, int port, String key, long destinationDB, long timeout, boolean copy, boolean replace)
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/migrate:
Available since 2.6.0.
Time complexity: This command actually executes a
RedisConnectionCommands.dump(java.lang.String)andRedisConnectionCommands.del(java.lang.String, java.lang.String...)in the source instance, and aRedisConnectionCommands.restore(java.lang.String, long, java.lang.String)in the target instance. See the pages of these commands for time complexity. Also an O(N) data transfer between the two instances is performed.Atomically transfer a key from a source Redis instance to a destination Redis instance. On success the key is deleted from the original instance and is guaranteed to exist in the target instance.
- Specified by:
migratein interfaceRedisServerCommands- Parameters:
host- the hostname/address of the target server.port- the port.key- the key to migrate.destinationDB- the database to target on the server.timeout- the timeout for the connection.copy- if true, the key is copied, not removed from the source.replace- if true, the remote key is replaced.- Returns:
- always true.
-
object
public RedisObject object(String subcommand, String key)
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/object:
Available since 2.2.3.
Time complexity: O(1).
The
objectcommand allows to inspect the internals of Redis Objects associated with keys. It is useful for debugging or to understand if your keys are using the specially encoded data types to save space. Your application may also use the information reported by theobjectcommand to implement application level key eviction policies when using Redis as a Cache.This call is here in order to support commands that don't have signatures.
- Specified by:
objectin interfaceRedisServerCommands- Parameters:
subcommand- the object subcommand.key- the key to operate on.- Returns:
- the Redis object returned.
-
objectRefcount
public long objectRefcount(String key)
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/object:
Available since 2.2.3.
Time complexity: O(1).
The
objectcommand allows to inspect the internals of Redis Objects associated with keys. It is useful for debugging or to understand if your keys are using the specially encoded data types to save space. Your application may also use the information reported by theobjectcommand to implement application level key eviction policies when using Redis as a Cache.- Specified by:
objectRefcountin interfaceRedisServerCommands- Parameters:
key- the key to count.- Returns:
- the number of references of the value associated with the specified key. This command is mainly useful for debugging.
-
objectEncoding
public EncodingType objectEncoding(String key)
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/object:
Available since 2.2.3.
Time complexity: O(1).
The
objectcommand allows to inspect the internals of Redis Objects associated with keys. It is useful for debugging or to understand if your keys are using the specially encoded data types to save space. Your application may also use the information reported by theobjectcommand to implement application level key eviction policies when using Redis as a Cache.- Specified by:
objectEncodingin interfaceRedisServerCommands- Parameters:
key- the key to inspect.- Returns:
- the kind of internal representation used in order to store the value associated with a key, or null for missing key.
-
objectIdletime
public long objectIdletime(String key)
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/object:
Available since 2.2.3.
Time complexity: O(1).
The
objectcommand allows to inspect the internals of Redis Objects associated with keys. It is useful for debugging or to understand if your keys are using the specially encoded data types to save space. Your application may also use the information reported by theobjectcommand to implement application level key eviction policies when using Redis as a Cache.- Specified by:
objectIdletimein interfaceRedisServerCommands- Parameters:
key- the key to inspect.- Returns:
- the number of seconds since the object stored at the specified key is idle (not requested by read or write operations). While the value is returned in seconds the actual resolution of this timer is 10 seconds, but may vary in future implementations.
-
pubsub
public RedisObject pubsub(String subcommand, String... arguments)
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/pubsub:
Available since 2.8.0.
Time complexity: O(N) for the CHANNELS subcommand, where N is the number of active channels, and assuming constant time pattern matching (relatively short channels and patterns). O(N) for the NUMSUB subcommand, where N is the number of requested channels. O(1) for the NUMPAT subcommand.
The PUBSUB command is an introspection command that allows to inspect the state of the Pub/Sub subsystem.
This call is here in order to support commands that don't have signatures.
- Specified by:
pubsubin interfaceRedisServerCommands- Parameters:
subcommand- the pubsub subcommand.arguments- the additional arguments.- Returns:
- the Redis object returned.
-
pubsubChannels
public String[] pubsubChannels(String pattern)
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/pubsub:
Available since 2.8.0.
Time complexity: O(N), where N is the number of active channels, and assuming constant time pattern matching (relatively short channels and patterns).
The PUBSUB command is an introspection command that allows to inspect the state of the Pub/Sub subsystem.
- Specified by:
pubsubChannelsin interfaceRedisServerCommands- Parameters:
pattern- the pattern to search for.- Returns:
- a list of active channels, optionally matching the specified pattern.
-
pubsubNumsub
public String[] pubsubNumsub(String... arguments)
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/pubsub:
Available since 2.8.0.
Time complexity: O(N), where N is the number of requested channels.
The PUBSUB command is an introspection command that allows to inspect the state of the Pub/Sub subsystem.
- Specified by:
pubsubNumsubin interfaceRedisServerCommands- Parameters:
arguments- the channel names.- Returns:
- a list of channels and number of subscribers for every channel. The format is channel, count, channel, count, ..., so the list is flat. The order in which the channels are listed is the same as the order of the channels specified in the command call.
-
pubsubNumpat
public long pubsubNumpat()
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/pubsub:
Available since 2.8.0.
Time complexity: O(1) for the NUMPAT subcommand.
The PUBSUB command is an introspection command that allows to inspect the state of the Pub/Sub subsystem.
- Specified by:
pubsubNumpatin interfaceRedisServerCommands- Returns:
- the number of patterns all the clients are subscribed to.
-
save
public boolean save()
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/save:
Available since 1.0.0.
The SAVE commands performs a synchronous save of the dataset producing a point in time snapshot of all the data inside the Redis instance, in the form of an RDB file.
- Specified by:
savein interfaceRedisServerCommands- Returns:
- always true.
-
shutdown
public void shutdown(boolean save)
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/shutdown:
Available since 1.0.0.
The command behavior is the following:
- Specified by:
shutdownin interfaceRedisServerCommands- Parameters:
save- if true, force save before shutdown.
-
slaveof
public boolean slaveof(String host, String port)
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/slaveof:
Available since 1.0.0.
The
SLAVEOFcommand can change the replication settings of a slave on the fly. If a Redis server is already acting as slave, the commandSLAVEOFNO ONE will turn off the replication, turning the Redis server into a MASTER. In the proper formRedisServerCommands.slaveof(String, String)hostname port will make the server a slave of another server listening at the specified hostname and port.- Specified by:
slaveofin interfaceRedisServerCommands- Parameters:
host- the hostname.port- the port.- Returns:
- always true.
-
slaveofNoOne
public boolean slaveofNoOne()
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/slaveof:
Available since 1.0.0.
The
SLAVEOFcommand can change the replication settings of a slave on the fly. If a Redis server is already acting as slave, the commandSLAVEOFNO ONE will turn off the replication, turning the Redis server into a MASTER. In the proper formRedisServerCommands.slaveof(String, String)hostname port will make the server a slave of another server listening at the specified hostname and port.- Specified by:
slaveofNoOnein interfaceRedisServerCommands- Returns:
- always true.
-
slowlog
public RedisObject slowlog(String subcommand, String argument)
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/slowlog:
Available since 2.2.12.
This command is used in order to read and reset the Redis slow queries log.
- Specified by:
slowlogin interfaceRedisServerCommands- Parameters:
subcommand- the slowlog subcommand.argument- the command argument.- Returns:
- the object returned.
-
slowlogGet
public RedisObject slowlogGet(long recentCount)
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/slowlog:
Available since 2.2.12.
This command is used in order to read and reset the Redis slow queries log.
- Specified by:
slowlogGetin interfaceRedisServerCommands- Parameters:
recentCount- the amount of recent entries to view.- Returns:
- the object returned.
-
slowlogLen
public RedisObject slowlogLen()
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/slowlog:
Available since 2.2.12.
Gets just the length of the slow log.
- Specified by:
slowlogLenin interfaceRedisServerCommands- Returns:
- the object returned.
-
slowlogReset
public RedisObject slowlogReset()
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/slowlog:
Available since 2.2.12.
Resets the slow log. Once deleted the information is lost forever.
- Specified by:
slowlogResetin interfaceRedisServerCommands- Returns:
- the object returned.
-
time
public long[] time()
Description copied from interface:RedisServerCommandsFrom http://redis.io/commands/time:
Available since 2.6.0.
Time complexity: O(1)
The TIME command returns the current server time as a two items lists: a Unix timestamp and the amount of microseconds already elapsed in the current second. Basically the interface is very similar to the one of the
gettimeofdaysystem call.- Specified by:
timein interfaceRedisServerCommands- Returns:
- UNIX time in seconds, microseconds.
-
-