Thursday, August 25, 2016

redis commands

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
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
1DEL key
This command deletes the key, if exists
2DUMP key 
This command returns a serialized version of the value stored at the specified key.
3EXISTS key 
This command checks whether the key exists or not.
4EXPIRE key seconds
Expires the key after the specified time
5EXPIREAT key timestamp 
Expires the key after the specified time. Here time is in Unix timestamp format
6PEXPIRE key milliseconds 
Set the expiry of key in milliseconds
7PEXPIREAT key milliseconds-timestamp 
Set the expiry of key in unix timestamp specified as milliseconds
8KEYS pattern 
Find all keys matching the specified pattern
9MOVE key db 
Move a key to another database
10PERSIST key 
Remove the expiration from the key
11PTTL key 
Get the remaining time in keys expiry in milliseconds.
12TTL key 
Get the remaining time in keys expiry.
13RANDOMKEY 
Return a random key from redis
14RENAME key newkey 
Change the key name
15RENAMENX key newkey 
Rename key, if new key doesn't exist
16TYPE 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
1SET key value 
This command sets the value at the specified key
2GET key 
Get the value of a key.
3GETRANGE key start end 
Get a substring of the string stored at a key
4GETSET key value
Set the string value of a key and return its old value
5GETBIT key offset
Returns the bit value at offset in the string value stored at key
6MGET key1 [key2..]
Get the values of all the given keys
7SETBIT key offset value
Sets or clears the bit at offset in the string value stored at key
8SETEX key seconds value
Set the value with expiry of a key
9SETNX key value
Set the value of a key, only if the key does not exist
10SETRANGE key offset value
Overwrite part of a string at key starting at the specified offset
11STRLEN key
Get the length of the value stored in a key
12MSET key value [key value ...]
Set multiple keys to multiple values
13MSETNX key value [key value ...] 
Set multiple keys to multiple values, only if none of the keys exist
14PSETEX key milliseconds value
Set the value and expiration in milliseconds of a key
15INCR key
Increment the integer value of a key by one
16INCRBY key increment
Increment the integer value of a key by the given amount
17INCRBYFLOAT key increment
Increment the float value of a key by the given amount
18DECR key
Decrement the integer value of a key by one
19DECRBY key decrement
Decrement the integer value of a key by the given number
20APPEND 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
1HDEL key field2 [field2] 
Delete one or more hash fields
2HEXISTS key field 
Determine whether a hash field exists or not
3HGET key field 
Get the value of a hash field stored at specified key
4HGETALL key 
Get all the fields and values stored in a hash at specified key
5HINCRBY key field increment 
Increment the integer value of a hash field by the given number
6HINCRBYFLOAT key field increment 
Increment the float value of a hash field by the given amount
7HKEYS key 
Get all the fields in a hash
8HLEN key 
Get the number of fields in a hash
9HMGET key field1 [field2] 
Get the values of all the given hash fields
10HMSET key field1 value1 [field2 value2 ] 
Set multiple hash fields to multiple values
11HSET key field value 
Set the string value of a hash field
12HSETNX key field value 
Set the value of a hash field, only if the field does not exist
13HVALS key 
Get all the values in a hash
14HSCAN key cursor [MATCH pattern] [COUNT count] 
Incrementally iterate hash fields and associated values



No comments:

Post a Comment