Redis Basics
Going over the basics
// to check if there is connection with Redis
127.0.0.1:6379> ping
PONG
// echo a string
127.0.0.1:6379> echo 'hello'
"hello"
// insert a record
127.0.0.1:6379> SET foo 100
OK
// get record based on the key
127.0.0.1:6379> GET foo
"100"
127.0.0.1:6379> SET bar "Hello world"
OK
// increment value by 1
127.0.0.1:6379> INCR foo
(integer) 101
// can't increment text
127.0.0.1:6379> INCR bar
(error) ERR value is not an integer or out of range
// value of foo is increased by 1
127.0.0.1:6379> GET foo
"101"
// decrease by 1
127.0.0.1:6379> DECR foo
(integer) 100
127.0.0.1:6379> GET foo
"100"
// check if the key exists, if exists, return 1, else return 0
127.0.0.1:6379> EXISTS foo
(integer) 1
127.0.0.1:6379> EXISTS bar
(integer) 1
127.0.0.1:6379> EXISTS foo1231
(integer) 0
// remove key
127.0.0.1:6379> DEL bar
(integer) 1
127.0.0.1:6379> EXISTS bar
(integer) 0
127.0.0.1:6379> GET bar
(nil)
// remove everything
127.0.0.1:6379> FLUSHALL
OK
127.0.0.1:6379> GET foo
(nil)
// use key spaces
127.0.0.1:6379> SET server:name "some server"
OK
127.0.0.1:6379> GET server:name
"some server"
127.0.0.1:6379> GET server
(nil)
127.0.0.1:6379> SET server:port 8000
OK
// set key to be expired in 10 sec
127.0.0.1:6379> SET greeting "Howdy" EX 10
OK
127.0.0.1:6379> GET greeting
"Howdy"
127.0.0.1:6379> GET greeting
"Howdy"
127.0.0.1:6379> GET greeting
"Howdy"
127.0.0.1:6379> GET greeting
"Howdy"
127.0.0.1:6379> GET greeting
"Howdy"
127.0.0.1:6379> GET greeting
"Howdy"
127.0.0.1:6379> GET greeting
(nil)
127.0.0.1:6379> SET greeting "Howdy" EX 10
OK
// check how long the key still gets to live
127.0.0.1:6379> TTL greeting
(integer) 6
127.0.0.1:6379> TTL greeting
(integer) 5
127.0.0.1:6379> TTL greeting
(integer) -2
127.0.0.1:6379> clear
127.0.0.1:6379> SETEX greeting 10 "Hello World"
OK
127.0.0.1:6379> TTL greeting
(integer) 7
127.0.0.1:6379> SETEX greeting 10 "Hello World"
OK
127.0.0.1:6379> TTL greeting
(integer) 5
// persist the key that was set to expire
127.0.0.1:6379> SETEX greeting 10 "Hello World"
OK
127.0.0.1:6379> PERSIST greeting
(integer) 1
127.0.0.1:6379> TTL greeting
(integer) -1
// set multiple key at once
127.0.0.1:6379> MSET key1 "Hello" key2 "World"
OK
// return multiple key
127.0.0.1:6379> MGET key1 key2
1) "Hello"
2) "World"
// append value to existing key
127.0.0.1:6379> APPEND key1 " Joe"
(integer) 9
127.0.0.1:6379> GET key1
"Hello Joe"
// rename key to a different name
127.0.0.1:6379> RENAME key1 greetingkey
OK
127.0.0.1:6379> GET greetingkey
"Hello Joe"
// push element to the beginning of the list
127.0.0.1:6379> LPUSH emp "Joe"
(integer) 1
127.0.0.1:6379> LPUSH emp "John"
(integer) 2
127.0.0.1:6379> LPUSH emp "TOM"
(integer) 3
// display all elements in the list
127.0.0.1:6379> LRANGE emp 0 -1
1) "TOM"
2) "John"
3) "Joe"
// display element 1 to element 2
127.0.0.1:6379> LRANGE emp 1 2
1) "John"
2) "Joe"
// push element to end of the list
127.0.0.1:6379> RPUSH emp "Val"
(integer) 4
// return length of the list
127.0.0.1:6379> LLEN emp
(integer) 4
127.0.0.1:6379> LRANGE emp 0 -1
1) "TOM"
2) "John"
3) "Joe"
4) "Val"
// remove first element of the list
127.0.0.1:6379> LPOP emp
"TOM"
127.0.0.1:6379> LRANGE emp 0 -1
1) "John"
2) "Joe"
3) "Val"
// remove last element of the list
127.0.0.1:6379> RPOP emp
"Val"
127.0.0.1:6379> LRANGE emp 0 -1
1) "John"
2) "Joe"
// insert element before the element with key value Joe
127.0.0.1:6379> LINSERT emp BEFORE "Joe" "Jen"
(integer) 3
127.0.0.1:6379> LRANGE emp 0 -1
1) "John"
2) "Jen"
3) "Joe"
// we have two "Bob"
127.0.0.1:6379> LRANGE emp 0 -1
1) "Bob"
2) "John"
3) "Jen"
4) "Joe"
5) "Bob"
// only one "Ian" is inserted before the first "Bob" found
127.0.0.1:6379> LINSERT emp BEFORE "Bob" "Ian"
(integer) 6
127.0.0.1:6379> LRANGE emp 0 -1
1) "Ian"
2) "Bob"
3) "John"
4) "Jen"
5) "Joe"
6) "Bob"
// working with set
// create a set with three members
127.0.0.1:6379> sadd fruit "Banana" "Apple" "Orange"
(integer) 3
// members in set are unique, therefore, can't insert duplicate
127.0.0.1:6379> SADD fruit "Banana" "Apple" "Orange"
(integer) 0
// shows member of the set
127.0.0.1:6379> SMEMBERS fruit
1) "Orange"
2) "Banana"
3) "Apple"
// is "<member>" in the set, return 1 if true, else 0
127.0.0.1:6379> SISMEMBER fruit "Apple"
(integer) 1
127.0.0.1:6379> SISMEMBER fruit "Pineapple"
(integer) 0
// return length of the set
127.0.0.1:6379> SCARD fruit
(integer) 3
// move member from one set to another
127.0.0.1:6379> SMOVE fruit exoticfruit "Pineapple"
(integer) 1
127.0.0.1:6379> SMEMBERS fruit
1) "Orange"
2) "Banana"
3) "Apple"
// remove member from set
127.0.0.1:6379> SREM fruit Orange
(integer) 1
127.0.0.1:6379> SMEMBERS fruit
1) "Banana"
2) "Apple"
// sorted set
// adding to sorted set
127.0.0.1:6379> ZADD users 1999 "Joe Doe"
(integer) 1
127.0.0.1:6379> ZADD users 1932 "Bob Sag"
(integer) 1
127.0.0.1:6379> ZADD users 1950 "Kat Okay"
(integer) 1
// return where the member is inserted into the sorted set
127.0.0.1:6379> ZRANK users "Kat Okay"
(integer) 1
// display the sorted set, note they are ranked by the score inserted, 1932, 1950, 1999
127.0.0.1:6379> ZRANGE users 0 -1
1) "Bob Sag"
2) "Kat Okay"
3) "Joe Doe"
// increment the score by 2
127.0.0.1:6379> ZINCRBY users 2 "Kat Okay"
"1952"
for more information, please reference to
https://redis.io/commands
for complete information
// to check if there is connection with Redis
127.0.0.1:6379> ping
PONG
// echo a string
127.0.0.1:6379> echo 'hello'
"hello"
// insert a record
127.0.0.1:6379> SET foo 100
OK
// get record based on the key
127.0.0.1:6379> GET foo
"100"
127.0.0.1:6379> SET bar "Hello world"
OK
// increment value by 1
127.0.0.1:6379> INCR foo
(integer) 101
// can't increment text
127.0.0.1:6379> INCR bar
(error) ERR value is not an integer or out of range
// value of foo is increased by 1
127.0.0.1:6379> GET foo
"101"
// decrease by 1
127.0.0.1:6379> DECR foo
(integer) 100
127.0.0.1:6379> GET foo
"100"
// check if the key exists, if exists, return 1, else return 0
127.0.0.1:6379> EXISTS foo
(integer) 1
127.0.0.1:6379> EXISTS bar
(integer) 1
127.0.0.1:6379> EXISTS foo1231
(integer) 0
// remove key
127.0.0.1:6379> DEL bar
(integer) 1
127.0.0.1:6379> EXISTS bar
(integer) 0
127.0.0.1:6379> GET bar
(nil)
// remove everything
127.0.0.1:6379> FLUSHALL
OK
127.0.0.1:6379> GET foo
(nil)
// use key spaces
127.0.0.1:6379> SET server:name "some server"
OK
127.0.0.1:6379> GET server:name
"some server"
127.0.0.1:6379> GET server
(nil)
127.0.0.1:6379> SET server:port 8000
OK
// set key to be expired in 10 sec
127.0.0.1:6379> SET greeting "Howdy" EX 10
OK
127.0.0.1:6379> GET greeting
"Howdy"
127.0.0.1:6379> GET greeting
"Howdy"
127.0.0.1:6379> GET greeting
"Howdy"
127.0.0.1:6379> GET greeting
"Howdy"
127.0.0.1:6379> GET greeting
"Howdy"
127.0.0.1:6379> GET greeting
"Howdy"
127.0.0.1:6379> GET greeting
(nil)
127.0.0.1:6379> SET greeting "Howdy" EX 10
OK
// check how long the key still gets to live
127.0.0.1:6379> TTL greeting
(integer) 6
127.0.0.1:6379> TTL greeting
(integer) 5
127.0.0.1:6379> TTL greeting
(integer) -2
127.0.0.1:6379> clear
127.0.0.1:6379> SETEX greeting 10 "Hello World"
OK
127.0.0.1:6379> TTL greeting
(integer) 7
127.0.0.1:6379> SETEX greeting 10 "Hello World"
OK
127.0.0.1:6379> TTL greeting
(integer) 5
// persist the key that was set to expire
127.0.0.1:6379> SETEX greeting 10 "Hello World"
OK
127.0.0.1:6379> PERSIST greeting
(integer) 1
127.0.0.1:6379> TTL greeting
(integer) -1
// set multiple key at once
127.0.0.1:6379> MSET key1 "Hello" key2 "World"
OK
// return multiple key
127.0.0.1:6379> MGET key1 key2
1) "Hello"
2) "World"
// append value to existing key
127.0.0.1:6379> APPEND key1 " Joe"
(integer) 9
127.0.0.1:6379> GET key1
"Hello Joe"
// rename key to a different name
127.0.0.1:6379> RENAME key1 greetingkey
OK
127.0.0.1:6379> GET greetingkey
"Hello Joe"
// push element to the beginning of the list
127.0.0.1:6379> LPUSH emp "Joe"
(integer) 1
127.0.0.1:6379> LPUSH emp "John"
(integer) 2
127.0.0.1:6379> LPUSH emp "TOM"
(integer) 3
// display all elements in the list
127.0.0.1:6379> LRANGE emp 0 -1
1) "TOM"
2) "John"
3) "Joe"
// display element 1 to element 2
127.0.0.1:6379> LRANGE emp 1 2
1) "John"
2) "Joe"
// push element to end of the list
127.0.0.1:6379> RPUSH emp "Val"
(integer) 4
// return length of the list
127.0.0.1:6379> LLEN emp
(integer) 4
127.0.0.1:6379> LRANGE emp 0 -1
1) "TOM"
2) "John"
3) "Joe"
4) "Val"
// remove first element of the list
127.0.0.1:6379> LPOP emp
"TOM"
127.0.0.1:6379> LRANGE emp 0 -1
1) "John"
2) "Joe"
3) "Val"
// remove last element of the list
127.0.0.1:6379> RPOP emp
"Val"
127.0.0.1:6379> LRANGE emp 0 -1
1) "John"
2) "Joe"
// insert element before the element with key value Joe
127.0.0.1:6379> LINSERT emp BEFORE "Joe" "Jen"
(integer) 3
127.0.0.1:6379> LRANGE emp 0 -1
1) "John"
2) "Jen"
3) "Joe"
// we have two "Bob"
127.0.0.1:6379> LRANGE emp 0 -1
1) "Bob"
2) "John"
3) "Jen"
4) "Joe"
5) "Bob"
// only one "Ian" is inserted before the first "Bob" found
127.0.0.1:6379> LINSERT emp BEFORE "Bob" "Ian"
(integer) 6
127.0.0.1:6379> LRANGE emp 0 -1
1) "Ian"
2) "Bob"
3) "John"
4) "Jen"
5) "Joe"
6) "Bob"
// working with set
// create a set with three members
127.0.0.1:6379> sadd fruit "Banana" "Apple" "Orange"
(integer) 3
// members in set are unique, therefore, can't insert duplicate
127.0.0.1:6379> SADD fruit "Banana" "Apple" "Orange"
(integer) 0
// shows member of the set
127.0.0.1:6379> SMEMBERS fruit
1) "Orange"
2) "Banana"
3) "Apple"
// is "<member>" in the set, return 1 if true, else 0
127.0.0.1:6379> SISMEMBER fruit "Apple"
(integer) 1
127.0.0.1:6379> SISMEMBER fruit "Pineapple"
(integer) 0
// return length of the set
127.0.0.1:6379> SCARD fruit
(integer) 3
// move member from one set to another
127.0.0.1:6379> SMOVE fruit exoticfruit "Pineapple"
(integer) 1
127.0.0.1:6379> SMEMBERS fruit
1) "Orange"
2) "Banana"
3) "Apple"
// remove member from set
127.0.0.1:6379> SREM fruit Orange
(integer) 1
127.0.0.1:6379> SMEMBERS fruit
1) "Banana"
2) "Apple"
// sorted set
// adding to sorted set
127.0.0.1:6379> ZADD users 1999 "Joe Doe"
(integer) 1
127.0.0.1:6379> ZADD users 1932 "Bob Sag"
(integer) 1
127.0.0.1:6379> ZADD users 1950 "Kat Okay"
(integer) 1
// return where the member is inserted into the sorted set
127.0.0.1:6379> ZRANK users "Kat Okay"
(integer) 1
// display the sorted set, note they are ranked by the score inserted, 1932, 1950, 1999
127.0.0.1:6379> ZRANGE users 0 -1
1) "Bob Sag"
2) "Kat Okay"
3) "Joe Doe"
// increment the score by 2
127.0.0.1:6379> ZINCRBY users 2 "Kat Okay"
"1952"
for more information, please reference to
https://redis.io/commands
for complete information
Comments
Post a Comment