Redis commands are used to perform some operations on redis server.
1) To connect redis server
# redis-cli -h 172.16.1.X
output
redis 172.16.1.X:6379>
2) To check redis path
redis 172.16.1.X:6379>CONFIG GET dir
output
1) "dir"
2) "/tmp"
3) To change redis path
redis 172.16.1.X:6379> CONFIG SET dir "/var/lib/redis/"
check path
redis 172.16.1.X:6379> CONFIG GET dir
1) "dir"
2) "/var/lib/redis"
4) To exit
redis 172.16.1.X:6379> exit
5) Syntax
6) Syntax
7) Syntax
8) In redis every hash can store up to more than 4 billion field-value pairs.
1) To connect redis server
# redis-cli -h 172.16.1.X
output
redis 172.16.1.X:6379>
2) To check redis path
redis 172.16.1.X:6379>CONFIG GET dir
output
1) "dir"
2) "/tmp"
3) To change redis path
redis 172.16.1.X:6379> CONFIG SET dir "/var/lib/redis/"
check path
redis 172.16.1.X:6379> CONFIG GET dir
1) "dir"
2) "/var/lib/redis"
4) To exit
redis 172.16.1.X:6379> exit
5) Syntax
Basic syntax of redis client is as follows:
$redis-cli
Example
Following example explains how we can start redis client.
To start redis client, open terminal and type the command redis-cli. This will connect to your local server and now you can run any command.
$redis-cli redis 127.0.0.1:6379> redis 127.0.0.1:6379> PING PONG
In the above example we connect to redis server running on local machine and executes a command PING, that checks whether server is running or not.
Run commands on remote server
To run commands on redis remote server you need to connect to server by same client redis-cli
Syntax
$ redis-cli -h host -p port -a password
Example
Following example shows how to connect to redis remote server running on host 127.0.0.1, port 6379 and has password mypass.
$redis-cli -h 127.0.0.1 -p 6379 -a "mypass" redis 127.0.0.1:6379> redis 127.0.0.1:6379> PING PONG
6) Syntax
redis 127.0.0.1:6379> COMMAND KEY_NAME
Example
redis 127.0.0.1:6379> SET tutorialspoint redis OK redis 127.0.0.1:6379> DEL tutorialspoint (integer) 1
In the above example DEL is the command, while tutorialspoint is the key. If the key is deleted, then output of the command will be (integer) 1, otherwise it will be (integer) 0
Redis keys commands
Below given table shows some basic commands related to keys:
S.N. | Command & Description |
---|---|
1 | DEL key This command deletes the key, if exists |
2 | DUMP key This command returns a serialized version of the value stored at the specified key. |
3 | EXISTS key This command checks whether the key exists or not. |
4 | EXPIRE key seconds Expires the key after the specified time |
5 | EXPIREAT key timestamp Expires the key after the specified time. Here time is in Unix timestamp format |
6 | PEXPIRE key milliseconds Set the expiry of key in milliseconds |
7 | PEXPIREAT key milliseconds-timestamp Set the expiry of key in unix timestamp specified as milliseconds |
8 | KEYS pattern Find all keys matching the specified pattern |
9 | MOVE key db Move a key to another database |
10 | PERSIST key Remove the expiration from the key |
11 | PTTL key Get the remaining time in keys expiry in milliseconds. |
12 | TTL key Get the remaining time in keys expiry. |
13 | RANDOMKEY Return a random key from redis |
14 | RENAME key newkey Change the key name |
15 | RENAMENX key newkey Rename key, if new key doesn't exist |
16 | TYPE key Return the data type of value stored in key. |
7) Syntax
redis 127.0.0.1:6379> COMMAND KEY_NAME
Example
redis 127.0.0.1:6379> SET tutorialspoint redis OK redis 127.0.0.1:6379> GET tutorialspoint "redis"
In the above example SET and GET are the command, while tutorialspoint is the key.
Redis strings commands
Below given table shows some basic commands to manage strings in redis:
S.N. | Command & Description |
---|---|
1 | SET key value This command sets the value at the specified key |
2 | GET key Get the value of a key. |
3 | GETRANGE key start end Get a substring of the string stored at a key |
4 | GETSET key value Set the string value of a key and return its old value |
5 | GETBIT key offset Returns the bit value at offset in the string value stored at key |
6 | MGET key1 [key2..] Get the values of all the given keys |
7 | SETBIT key offset value Sets or clears the bit at offset in the string value stored at key |
8 | SETEX key seconds value Set the value with expiry of a key |
9 | SETNX key value Set the value of a key, only if the key does not exist |
10 | SETRANGE key offset value Overwrite part of a string at key starting at the specified offset |
11 | STRLEN key Get the length of the value stored in a key |
12 | MSET key value [key value ...] Set multiple keys to multiple values |
13 | MSETNX key value [key value ...] Set multiple keys to multiple values, only if none of the keys exist |
14 | PSETEX key milliseconds value Set the value and expiration in milliseconds of a key |
15 | INCR key Increment the integer value of a key by one |
16 | INCRBY key increment Increment the integer value of a key by the given amount |
17 | INCRBYFLOAT key increment Increment the float value of a key by the given amount |
18 | DECR key Decrement the integer value of a key by one |
19 | DECRBY key decrement Decrement the integer value of a key by the given number |
20 | APPEND key value Append a value to a key |
8) In redis every hash can store up to more than 4 billion field-value pairs.
Example
redis 127.0.0.1:6379> HMSET tutorialspoint name "redis tutorial" description "redis basic commands for caching" likes 20 visitors 23000 OK redis 127.0.0.1:6379> HGETALL tutorialspoint 1) "name" 2) "redis tutorial" 3) "description" 4) "redis basic commands for caching" 5) "likes" 6) "20" 7) "visitors" 8) "23000"
In the above example we have set redis tutorials detail (name, description, likes, visitors) in hash named tutorialspoint
Redis hash commands
Below given table shows some basic commands related to hash:
S.N. | Command & Description |
---|---|
1 | HDEL key field2 [field2] Delete one or more hash fields |
2 | HEXISTS key field Determine whether a hash field exists or not |
3 | HGET key field Get the value of a hash field stored at specified key |
4 | HGETALL key Get all the fields and values stored in a hash at specified key |
5 | HINCRBY key field increment Increment the integer value of a hash field by the given number |
6 | HINCRBYFLOAT key field increment Increment the float value of a hash field by the given amount |
7 | HKEYS key Get all the fields in a hash |
8 | HLEN key Get the number of fields in a hash |
9 | HMGET key field1 [field2] Get the values of all the given hash fields |
10 | HMSET key field1 value1 [field2 value2 ] Set multiple hash fields to multiple values |
11 | HSET key field value Set the string value of a hash field |
12 | HSETNX key field value Set the value of a hash field, only if the field does not exist |
13 | HVALS key Get all the values in a hash |
14 | HSCAN key cursor [MATCH pattern] [COUNT count] Incrementally iterate hash fields and associated values |
No comments:
Post a Comment