Redis的基本数据结构

时间:2024-04-06 19:11:00

Redis的基本数据结构

Redis的基本数据结构

hset myhash username jack #设置一个键值对

hset myhash age 18 

hmset myhash2 username rose age 21 #设置多个键值对

hget myhash username #获取一个键的值

hmget myhash username age #获取多个hash键位的数据

hgetall myhash 获取某个hash的所有键值对

hincrby myhash age 5 给值加上数据

hexists myhash age 判断是否存在

hgetall myhash 获得所有的键值对

hlen myhash 获得键值对长度

hkeys myhash 获得所有的键

hvals myhash 获得所有的值

-rw-rw-r--.  1 root root 36761 1月  25 2016 00-RELEASENOTES
-rw-rw-r--.  1 root root    53 1月  25 2016 BUGS
-rw-rw-r--.  1 root root  1805 1月  25 2016 CONTRIBUTING
-rw-rw-r--.  1 root root  1487 1月  25 2016 COPYING
drwxrwxr-x.  6 root root  4096 9月  25 20:43 deps
-rw-rw-r--.  1 root root    11 1月  25 2016 INSTALL
-rw-rw-r--.  1 root root   151 1月  25 2016 Makefile
-rw-rw-r--.  1 root root  4223 1月  25 2016 MANIFESTO
-rw-rw-r--.  1 root root  5201 1月  25 2016 README
-rw-rw-r--.  1 root root 41560 1月  25 2016 redis.conf
-rwxrwxr-x.  1 root root   271 1月  25 2016 runtest
-rwxrwxr-x.  1 root root   280 1月  25 2016 runtest-cluster
-rwxrwxr-x.  1 root root   281 1月  25 2016 runtest-sentinel
-rw-rw-r--.  1 root root  7109 1月  25 2016 sentinel.conf
drwxrwxr-x.  2 root root  4096 9月  25 20:44 src
drwxrwxr-x. 10 root root  4096 1月  25 2016 tests
drwxrwxr-x.  5 root root  4096 1月  25 2016 utils
[[email protected] redis-3.0.7]# cd /usr/local
[[email protected] local]# ll
总用量 40
drwxr-xr-x. 2 root root 4096 9月  23 2011 bin
drwxr-xr-x. 2 root root 4096 9月  23 2011 etc
drwxr-xr-x. 2 root root 4096 9月  23 2011 games
drwxr-xr-x. 2 root root 4096 9月  23 2011 include
drwxr-xr-x. 2 root root 4096 9月  23 2011 lib
drwxr-xr-x. 2 root root 4096 9月  23 2011 libexec
drwxr-xr-x. 3 root root 4096 9月  25 21:05 redis
drwxr-xr-x. 2 root root 4096 9月  23 2011 sbin
drwxr-xr-x. 5 root root 4096 9月  19 17:08 share
drwxr-xr-x. 2 root root 4096 9月  23 2011 src
[[email protected] local]# cd /redis
-bash: cd: /redis: 没有那个文件或目录
[[email protected] local]# cd redis
[[email protected] redis]# ll
总用量 52
drwxr-xr-x. 2 root root  4096 9月  25 20:55 bin
-rw-r--r--. 1 root root    18 9月  25 21:05 dump.rdb
-rw-r--r--. 1 root root 41563 9月  25 21:01 redis.conf
[[email protected] redis]# ./bin/redis-server ./redis.conf
[[email protected] redis]# ps -ef|grep -i redis
root      1830     1  0 23:34 ?        00:00:00 ./bin/redis-server *:6379      
root      1836  1748  0 23:34 pts/0    00:00:00 grep -i redis
[[email protected] redis]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:6D:8A:2C  
          inet addr:192.168.179.128  Bcast:192.168.179.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe6d:8a2c/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:436 errors:0 dropped:0 overruns:0 frame:0
          TX packets:308 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:38466 (37.5 KiB)  TX bytes:36716 (35.8 KiB)
          Interrupt:19 Base address:0x2000 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

[[email protected] redis]# vim /etc/sysconfig/iptables
[[email protected] redis]# service iptables restart
iptables:清除防火墙规则:                                 [确定]
iptables:将链设置为政策 ACCEPT:filter                    [确定]
iptables:正在卸载模块:                                   [确定]
iptables:应用防火墙规则:                                 [确定]
[[email protected] redis]# ./bin/redis-cli 
127.0.0.1:6379> set company imooc
OK
127.0.0.1:6379> get company
"imooc"
127.0.0.1:6379> getset company baidu
"imooc"
127.0.0.1:6379> get company
"baidu"
127.0.0.1:6379> set person jack
OK
127.0.0.1:6379> get person
"jack"
127.0.0.1:6379> del person
(integer) 1
127.0.0.1:6379> get person
(nil)
127.0.0.1:6379> incr num
(integer) 1
127.0.0.1:6379> get num
"1"
127.0.0.1:6379> incr num
(integer) 2
127.0.0.1:6379> incr company
(error) ERR value is not an integer or out of range
127.0.0.1:6379> decr num
(integer) 1
127.0.0.1:6379> decr num2
(integer) -1
127.0.0.1:6379> incrby 5
(error) ERR wrong number of arguments for 'incrby' command
127.0.0.1:6379> incrby num  5
(integer) 6
127.0.0.1:6379> incrby num3 5
(integer) 5
127.0.0.1:6379> decrby num3 5
(integer) 0
127.0.0.1:6379> append num 5
(integer) 2
127.0.0.1:6379> get num
"65"
127.0.0.1:6379> append num5 123
(integer) 3
127.0.0.1:6379> hset myhash username jack
(integer) 1
127.0.0.1:6379> hset myhash age 18
(integer) 1
127.0.0.1:6379> hmset myhash username rose age 21
OK
127.0.0.1:6379> hget jack
(error) ERR wrong number of arguments for 'hget' command
127.0.0.1:6379> hget myhash username
"rose"
127.0.0.1:6379> hset myhash2 jack
(error) ERR wrong number of arguments for 'hset' command
127.0.0.1:6379> hmget myhash username age
1) "rose"
2) "21"
127.0.0.1:6379> hgetall myhash
1) "username"
2) "rose"
3) "age"
4) "21"
127.0.0.1:6379> hdel myhash username age
(integer) 2
127.0.0.1:6379> hgetall myhash2
(empty list or set)
127.0.0.1:6379> hdel myhash username
(integer) 0
127.0.0.1:6379> hset myhash3 username jack
(integer) 1
127.0.0.1:6379> hset myhash3 age 88
(integer) 1
127.0.0.1:6379> hmget myhash3 username age
1) "jack"
2) "88"
127.0.0.1:6379> hgetall myhash3
1) "username"
2) "jack"
3) "age"
4) "88"
127.0.0.1:6379> hget myhash3 username
"jack"
127.0.0.1:6379> hdel myhash3 username age
(integer) 2
127.0.0.1:6379> hget myhash2 username age
(error) ERR wrong number of arguments for 'hget' command
127.0.0.1:6379> hget myhash username age
(error) ERR wrong number of arguments for 'hget' command
127.0.0.1:6379> hset myhash username jiangjinhao
(integer) 1
127.0.0.1:6379> hset myhash age 15
(integer) 1
127.0.0.1:6379> hincrby myhash age 5
(integer) 20
127.0.0.1:6379> hget myhash age 
"20"
127.0.0.1:6379> hexists myhash age
(integer) 1
127.0.0.1:6379> hexists myhash password
(integer) 0
127.0.0.1:6379> hgetall myhash
1) "username"
2) "jiangjinhao"
3) "age"
4) "20"
127.0.0.1:6379> hlen myhash
(integer) 2
127.0.0.1:6379> hkeys myhash
1) "username"
2) "age"
127.0.0.1:6379> hvalues myhash
(error) ERR unknown command 'hvalues'
127.0.0.1:6379> hvals myhash
1) "jiangjinhao"
2) "20"
127.0.0.1:6379> 
127.0.0.1:6379> lpush mylist a b c
(integer) 3
127.0.0.1:6379> lpush mylist 1 2 3
(integer) 6
127.0.0.1:6379> rpush mylist2 a b c 
(integer) 3
127.0.0.1:6379> rpush mylist2 1 2 3
(integer) 6
127.0.0.1:6379> lrange mylist 0 5
1) "3"
2) "2"
3) "1"
4) "c"
5) "b"
6) "a"
127.0.0.1:6379> lrange mylist2 0 -1
1) "a"
2) "b"
3) "c"
4) "1"
5) "2"
6) "3"
127.0.0.1:6379> lrange mylist2 0 5
1) "a"
2) "b"
3) "c"
4) "1"
5) "2"
6) "3"
127.0.0.1:6379> lpop mylist
"3"
127.0.0.1:6379> rpop mylist2
"3"
127.0.0.1:6379> llen mylist
(integer) 5
127.0.0.1:6379> llen mylist3
(integer) 0
127.0.0.1:6379> lpushx mylist x
(integer) 6
127.0.0.1:6379> lrange mylist 0 -1
1) "x"
2) "2"
3) "1"
4) "c"
5) "b"
6) "a"
127.0.0.1:6379> lrange mylist3 0 -1
(empty list or set)
127.0.0.1:6379> rpush mylist2 y
(integer) 6
127.0.0.1:6379> lrange mylist2 0 -1
1) "a"
2) "b"
3) "c"
4) "1"
5) "2"
6) "y"
127.0.0.1:6379> lpush mylist3 1 2 3
(integer) 3
127.0.0.1:6379> lpush mylist3 1 2 3
(integer) 6
127.0.0.1:6379> lpush mylist3 1 2 3
(integer) 9
127.0.0.1:6379> lrange mylist3
(error) ERR wrong number of arguments for 'lrange' command
127.0.0.1:6379> lrange mylist3 0 -1
1) "3"
2) "2"
3) "1"
4) "3"
5) "2"
6) "1"
7) "3"
8) "2"
9) "1"
127.0.0.1:6379> 
127.0.0.1:6379> lrem mylist3 2 3
(integer) 2
127.0.0.1:6379> lrange mylist3 0 -1
1) "2"
2) "1"
3) "2"
4) "1"
5) "3"
6) "2"
7) "1"
127.0.0.1:6379> lrem mylist3 -2 1
(integer) 2
127.0.0.1:6379> lrange mylist3 0 -1
1) "2"
2) "1"
3) "2"
4) "3"
5) "2"
127.0.0.1:6379> lrem mylist3 0 2
(integer) 3
127.0.0.1:6379> lrange mylist3 0 -1
1) "1"
2) "3"
127.0.0.1:6379> lrange mylist 0 -1
1) "x"
2) "2"
3) "1"
4) "c"
5) "b"
6) "a"
127.0.0.1:6379> lset mylist 3 mmm
OK
127.0.0.1:6379> lrange mylist 0 -1
1) "x"
2) "2"
3) "1"
4) "mmm"
5) "b"
6) "a"
127.0.0.1:6379> lpush mylist4 a b c
(integer) 3
127.0.0.1:6379> lpush mylist4 a b c
(integer) 6
127.0.0.1:6379> lrange mylist4 0 -1
1) "c"
2) "b"
3) "a"
4) "c"
5) "b"
6) "a"
127.0.0.1:6379> linset mylist4 before b 11
(error) ERR unknown command 'linset'
127.0.0.1:6379> linsert mylist4 before b 11
(integer) 7
127.0.0.1:6379> lrange mylist4 0 -1
1) "c"
2) "11"
3) "b"
4) "a"
5) "c"
6) "b"
7) "a"
127.0.0.1:6379> linsert mylist4 after b 22
(integer) 8
127.0.0.1:6379> lrange mylist4 0 -1
1) "c"
2) "11"
3) "b"
4) "22"
5) "a"
6) "c"
7) "b"
8) "a"
127.0.0.1:6379> lpush mylist5 1 2 3
(integer) 3
127.0.0.1:6379> lpush mylist6 a b c 
(integer) 3
127.0.0.1:6379> lrange mylist5 0 -1
1) "3"
2) "2"
3) "1"
127.0.0.1:6379> lrange mulist6 0 -1
(empty list or set)
127.0.0.1:6379> lrange mylist6 0 -1
1) "c"
2) "b"
3) "a"
127.0.0.1:6379> rpoplpush mylist5 mylist6
"1"
127.0.0.1:6379> lrange mylist5 0 -1
1) "3"
2) "2"
127.0.0.1:6379> lrange mylist6 0 -1
1) "1"
2) "c"
3) "b"
4) "a"
127.0.0.1:6379>