# telnet localhost 6379Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
SET hello world
+OK
GET hello
$5world
quit
+OK
Connection closed by foreign host.
或者使用redis-cli客户端:
12345
# redis-cliredis 127.0.0.1:6379> SET hello world
OK
redis 127.0.0.1:6379> GET hello
"world"
redis 127.0.0.1:6379> SET name "Redis 2.6.16"OK
redis 127.0.0.1:6379> GET name
"Redis 2.6.16"
另外,它还支持批量读写:
12345
redis 127.0.0.1:6379> MSET age 30 sex Male
OK
redis 127.0.0.1:6379> MGET age sex
1)"30"2)"Male"
还可以当成数字来使用,并支持对数字的加减操作:
123456789101112
redis 127.0.0.1:6379> INCR age
(integer) 31
redis 127.0.0.1:6379> INCRBY age 2
(integer) 33
redis 127.0.0.1:6379> GET age
"33"redis 127.0.0.1:6379> DECR age
(integer) 32
redis 127.0.0.1:6379> DECRBY age 2
(integer) 30
redis 127.0.0.1:6379> GET age
"30"
还支持对字符串进行部分修改或获取操作
12345678
redis 127.0.0.1:6379> STRLEN name
(integer) 12
redis 127.0.0.1:6379> GETRANGE name 0 4
"Redis"redis 127.0.0.1:6379> APPEND name ", NoSQL"(integer) 19
redis 127.0.0.1:6379> GET name
"Redis 2.6.16, NoSQL"
List类型
Redis能够把数据存储成一个链表,并能对这个链表进行操作:
1234567891011121314151617181920
redis 127.0.0.1:6379> LPUSH language Java
(integer) 1
redis 127.0.0.1:6379> LPUSH language C++
(integer) 2
redis 127.0.0.1:6379> RPUSH language C
(integer) 3
redis 127.0.0.1:6379> LLEN language
(integer) 3
redis 127.0.0.1:6379> LRANGE language 0 2
1)"C++"2)"Java"3)"C"redis 127.0.0.1:6379> LPOP language
"C++"redis 127.0.0.1:6379> LLEN language
(integer) 2
redis 127.0.0.1:6379> LREM language 1 Java
(integer) 1
redis 127.0.0.1:6379> LLEN language
(integer) 1
Redis也支持很多修改操作
123456789101112131415161718
redis 127.0.0.1:6379> LRANGE language 0 2
1)"C"redis 127.0.0.1:6379> LINSERT language BEFORE C C++
(integer) 2
redis 127.0.0.1:6379> LINSERT language BEFORE C Java
(integer) 3
redis 127.0.0.1:6379> LLEN language
(integer) 3
redis 127.0.0.1:6379> LRANGE language 0 2
1)"C++"2)"Java"3)"C"redis 127.0.0.1:6379> LTRIM language 2 -1
OK
redis 127.0.0.1:6379> LLEN language
(integer) 1
redis 127.0.0.1:6379> LRANGE language 0 2
1)"C"
Sets类型
Redis能够将一系列不重复的值存储成一个集合,并支持修改和集合关系操作。
123456789101112
redis 127.0.0.1:6379> SADD system Win
(integer) 1
redis 127.0.0.1:6379> SADD system Linux
(integer) 1
redis 127.0.0.1:6379> SADD system Mac
(integer) 1
redis 127.0.0.1:6379> SADD system Linux
(integer) 0
redis 127.0.0.1:6379> SMEMBERS system
1)"Win"2)"Mac"3)"Linux"
Sets结构也支持相应的修改操作
1234567891011
redis 127.0.0.1:6379> SREM system Win
(integer) 1
redis 127.0.0.1:6379> SMEMBERS system
1)"Mac"2)"Linux"redis 127.0.0.1:6379> SADD system Win
(integer) 1
redis 127.0.0.1:6379> SMEMBERS system
1)"Mac"2)"Win"3)"Linux"
redis 127.0.0.1:6379> ZADD days 0 mon
(integer) 1
redis 127.0.0.1:6379> ZADD days 1 tue
(integer) 1
redis 127.0.0.1:6379> ZADD days 2 wed
(integer) 1
redis 127.0.0.1:6379> ZADD days 3 thu
(integer) 1
redis 127.0.0.1:6379> ZADD days 4 fri
(integer) 1
redis 127.0.0.1:6379> ZADD days 5 sat
(integer) 1
redis 127.0.0.1:6379> ZADD days 6 sun
(integer) 1
redis 127.0.0.1:6379> ZCARD days
(integer) 7
redis 127.0.0.1:6379> ZRANGE days 0 6
1)"mon"2)"tue"3)"wed"4)"thu"5)"fri"6)"sat"7)"sun"redis 127.0.0.1:6379> ZSCORE days sat
"5"redis 127.0.0.1:6379> ZCOUNT days 3 6
(integer) 4
redis 127.0.0.1:6379> ZRANGEBYSCORE days 3 6
1)"thu"2)"fri"3)"sat"4)"sun"
Hash类型
Redis能够存储多个键值对的数据
123456789101112131415161718192021222324
redis 127.0.0.1:6379> HMSET student name Tom age 12 sex Male
OK
redis 127.0.0.1:6379> HKEYS student
1)"name"2)"age"3)"sex"redis 127.0.0.1:6379> HVALS student
1)"Tom"2)"12"3)"Male"redis 127.0.0.1:6379> HGETALL student
1)"name"2)"Tom"3)"age"4)"12"5)"sex"6)"Male"redis 127.0.0.1:6379> HDEL student sex
(integer) 1
redis 127.0.0.1:6379> HGETALL student
1)"name"2)"Tom"3)"age"4)"12"
Redis能够支持Hash的批量修改和获取
123456
redis 127.0.0.1:6379> HMSET kid name Akshi age 2 sex Female
OK
redis 127.0.0.1:6379> HMGET kid name age sex
1)"Akshi"2)"2"3)"Female"