The first thing to do in order to check if Redis is working properly is sending a PING command using redis-cli:
127.0.0.1:6379> ping
PONG
saving key & retreving saved value
127.0.0.1:6379> set mykey myvalue
OK
127.0.0.1:6379> get mykey
"myvalue"
127.0.0.1:6379>
setting password
open "/etc/redis/redis.conf" and look for "requirepass" commented line,uncomment it set password as
requirepass sangram
then we need to restart service
sudo service redis-server restart .now we need to open redis-cli again
on redis-cli we will check password is enforced or not,lets try to set some random key value as follows
127.0.0.1:6379> set mykey test
(error) NOAUTH Authentication required.
Now we will keyin password as follows
127.0.0.1:6379> auth sangram
OK
after getting authenticated we are able to save key
127.0.0.1:6379> set mykey test
OK
Getting config value
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "sangram"
Overrride key
127.0.0.1:6379> set mykey test2
OK
127.0.0.1:6379> get mykey
"test2"
Deleteing key:
127.0.0.1:6379> DEL mykey
(integer) 1
List keys by pattern:
127.0.0.1:6379> set mykey1 test2
OK
127.0.0.1:6379> set mykey2 test2
OK
127.0.0.1:6379> KEYS my*
1) "mykey2"
2) "mykey"
3) "mykey1
List All Keys:
127.0.0.1:6379> KEYS *
1) "mykey2"
2) "mykey"
3) "mykey1"
Expiry:
Redis Expire command is used to set the expiry of a key. After the expiry time, the key will not be available in Redis.It returns 1, if timeout is set for the key else 0.
127.0.0.1:6379> EXPIRE mykey 10
(integer) 1
unit of expiry time is in seconds
checking if key expired
127.0.0.1:6379> KEYS *
1) "mykey1"
2) "mykey2"
3) "\xe2\x80\x9ca-test\xe2\x80\x9d"
4) "a-test"
no occurance of mykey found.
setting key in milli seconds
127.0.0.1:6379> set mykey3 test3 PX 5000
OK
we can also set expiry at the time of adding key
127.0.0.1:6379> set mykey3 test3 EX 10
OK
Check if key exist:returns 1 if key exist else 0.
127.0.0.1:6379> EXISTS mykey3
(integer) 0
127.0.0.1:6379> EXISTS mykey1
(integer) 1
Store Javascript objects in Redis
127.0.0.1:6379> HSET "a-test" "name.first" "Kyle"
(integer) 1
127.0.0.1:6379> HSET "a-test" "name.family" "Davis"
(integer) 1
127.0.0.1:6379> HSET "a-test" "address" "123 Main Street"
(integer) 1
Get value for saved key:
127.0.0.1:6379> HGET "a-test" "name.family"
"Davis"
127.0.0.1:6379> HGET "a-test" "address"
"123 Main Street"
Get value for saved keys:
127.0.0.1:6379> HMGET "a-test" "name.first" "address"
1) "Kyle"
2) "123 Main Street"
this is same as saving below json in javascript world
var aTest = {
"name.first" : ‘Kyle’,
"name.family" : ‘Davis’,
"address" : ‘123 Main Street’
}
Storing multiple key at in one go:
127.0.0.1:6379> HMSET adrees city "mumbai" zip "400074"
OK
127.0.0.1:6379> HGET adrees city
"mumbai"
add key only if key does not exist do not override existing key value
127.0.0.1:6379> HSETNX adrees city "pune"
(integer) 0
127.0.0.1:6379> HSETNX adrees state "maharshtra"
(integer) 1
confirming insertion:
127.0.0.1:6379> HGETALL adrees
1) "city"
2) "mumbai"
3) "zip"
4) "400074"
5) "state"
6) "maharshtra"
removing key:
127.0.0.1:6379> HDEL "a-test" "name.family"
(integer) 1
view complete hash:
127.0.0.1:6379> HGETALL "a-test"
1) "name.first"
2) "Kyle"
3) "address"
4) "123 Main Street"
list all keys only
127.0.0.1:6379> HKEYS "a-test"
1) "name.first"
2) "address"
list all values:
127.0.0.1:6379> HVALS a-test
1) "Kyle"
2) "123 Main Street"
total keys:
127.0.0.1:6379> HLEN "a-test"
(integer) 2
check key exist in hash:
127.0.0.1:6379> HEXISTS "a-test" address
(integer) 1
127.0.0.1:6379> HEXISTS "a-test" addresss
(integer) 0
Database:
In Redis the number of Redis databases is fixed, and set in the configuration file. By default, you have 16 databases. Each database is identified by a number (not a name).
You can use the following command to know the number of databases:
127.0.0.1:6379> CONFIG GET databases
1) "databases"
2) "16"
selecting database of specific number
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> set mykey3 test3
OK
127.0.0.1:6379[1]> KEYS *
1) "mykey3"
on redis-cli switching between databases the key persist.
You can use the following command to list the databases for which some keys are defined:
127.0.0.1:6379> INFO keyspace
# Keyspace
db0:keys=4,expires=0,avg_ttl=0
To get redis backup dir
127.0.0.1:6379> CONFIG get dir
1) "dir"
2) "/var/lib/redis"
Redis SAVE command is used to create a backup of the current Redis database.
127.0.0.1:6379> SAVE
OK
This command will create a dump.rdb file in your Redis directory.
root@sangram-HP-Laptop-15-bs0xx:/home/sangram/workspace/node/hoisting# ls -lth /var/lib/redis/dump.rdb
-rw-rw---- 1 redis redis 358 Sep 22 20:24 /var/lib/redis/dump.rdb
Notice time file has been created.
BGSAVE command will start the backup process and run this in the background.
127.0.0.1:6379> BGSAVE
Background saving started
To restore Redis data, move Redis backup file (dump.rdb) into your Redis backup directory and start the server.
127.0.0.1:6379> ping
PONG
saving key & retreving saved value
127.0.0.1:6379> set mykey myvalue
OK
127.0.0.1:6379> get mykey
"myvalue"
127.0.0.1:6379>
setting password
open "/etc/redis/redis.conf" and look for "requirepass" commented line,uncomment it set password as
requirepass sangram
then we need to restart service
sudo service redis-server restart .now we need to open redis-cli again
on redis-cli we will check password is enforced or not,lets try to set some random key value as follows
127.0.0.1:6379> set mykey test
(error) NOAUTH Authentication required.
Now we will keyin password as follows
127.0.0.1:6379> auth sangram
OK
after getting authenticated we are able to save key
127.0.0.1:6379> set mykey test
OK
Getting config value
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "sangram"
Overrride key
127.0.0.1:6379> set mykey test2
OK
127.0.0.1:6379> get mykey
"test2"
Deleteing key:
127.0.0.1:6379> DEL mykey
(integer) 1
List keys by pattern:
127.0.0.1:6379> set mykey1 test2
OK
127.0.0.1:6379> set mykey2 test2
OK
127.0.0.1:6379> KEYS my*
1) "mykey2"
2) "mykey"
3) "mykey1
List All Keys:
127.0.0.1:6379> KEYS *
1) "mykey2"
2) "mykey"
3) "mykey1"
Expiry:
Redis Expire command is used to set the expiry of a key. After the expiry time, the key will not be available in Redis.It returns 1, if timeout is set for the key else 0.
127.0.0.1:6379> EXPIRE mykey 10
(integer) 1
unit of expiry time is in seconds
checking if key expired
127.0.0.1:6379> KEYS *
1) "mykey1"
2) "mykey2"
3) "\xe2\x80\x9ca-test\xe2\x80\x9d"
4) "a-test"
no occurance of mykey found.
setting key in milli seconds
127.0.0.1:6379> set mykey3 test3 PX 5000
OK
we can also set expiry at the time of adding key
127.0.0.1:6379> set mykey3 test3 EX 10
OK
Check if key exist:returns 1 if key exist else 0.
127.0.0.1:6379> EXISTS mykey3
(integer) 0
127.0.0.1:6379> EXISTS mykey1
(integer) 1
Store Javascript objects in Redis
127.0.0.1:6379> HSET "a-test" "name.first" "Kyle"
(integer) 1
127.0.0.1:6379> HSET "a-test" "name.family" "Davis"
(integer) 1
127.0.0.1:6379> HSET "a-test" "address" "123 Main Street"
(integer) 1
Get value for saved key:
127.0.0.1:6379> HGET "a-test" "name.family"
"Davis"
127.0.0.1:6379> HGET "a-test" "address"
"123 Main Street"
Get value for saved keys:
127.0.0.1:6379> HMGET "a-test" "name.first" "address"
1) "Kyle"
2) "123 Main Street"
this is same as saving below json in javascript world
var aTest = {
"name.first" : ‘Kyle’,
"name.family" : ‘Davis’,
"address" : ‘123 Main Street’
}
Storing multiple key at in one go:
127.0.0.1:6379> HMSET adrees city "mumbai" zip "400074"
OK
127.0.0.1:6379> HGET adrees city
"mumbai"
add key only if key does not exist do not override existing key value
127.0.0.1:6379> HSETNX adrees city "pune"
(integer) 0
127.0.0.1:6379> HSETNX adrees state "maharshtra"
(integer) 1
confirming insertion:
127.0.0.1:6379> HGETALL adrees
1) "city"
2) "mumbai"
3) "zip"
4) "400074"
5) "state"
6) "maharshtra"
removing key:
127.0.0.1:6379> HDEL "a-test" "name.family"
(integer) 1
view complete hash:
127.0.0.1:6379> HGETALL "a-test"
1) "name.first"
2) "Kyle"
3) "address"
4) "123 Main Street"
list all keys only
127.0.0.1:6379> HKEYS "a-test"
1) "name.first"
2) "address"
list all values:
127.0.0.1:6379> HVALS a-test
1) "Kyle"
2) "123 Main Street"
total keys:
127.0.0.1:6379> HLEN "a-test"
(integer) 2
check key exist in hash:
127.0.0.1:6379> HEXISTS "a-test" address
(integer) 1
127.0.0.1:6379> HEXISTS "a-test" addresss
(integer) 0
Database:
In Redis the number of Redis databases is fixed, and set in the configuration file. By default, you have 16 databases. Each database is identified by a number (not a name).
You can use the following command to know the number of databases:
127.0.0.1:6379> CONFIG GET databases
1) "databases"
2) "16"
selecting database of specific number
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> set mykey3 test3
OK
127.0.0.1:6379[1]> KEYS *
1) "mykey3"
on redis-cli switching between databases the key persist.
You can use the following command to list the databases for which some keys are defined:
127.0.0.1:6379> INFO keyspace
# Keyspace
db0:keys=4,expires=0,avg_ttl=0
To get redis backup dir
127.0.0.1:6379> CONFIG get dir
1) "dir"
2) "/var/lib/redis"
Redis SAVE command is used to create a backup of the current Redis database.
127.0.0.1:6379> SAVE
OK
This command will create a dump.rdb file in your Redis directory.
root@sangram-HP-Laptop-15-bs0xx:/home/sangram/workspace/node/hoisting# ls -lth /var/lib/redis/dump.rdb
-rw-rw---- 1 redis redis 358 Sep 22 20:24 /var/lib/redis/dump.rdb
Notice time file has been created.
BGSAVE command will start the backup process and run this in the background.
127.0.0.1:6379> BGSAVE
Background saving started
To restore Redis data, move Redis backup file (dump.rdb) into your Redis backup directory and start the server.
No comments:
Post a Comment