redis培训学习总结

时间:2022-09-05 10:33:18

redis支持的数据结构:strings,lists,hashes,set,sorted set。下面一一来做实验

strings的具体操作有:get/set/setnx,append,strlen,getrange/setrange,incr/decr/incrby/decrby,mget/mset,意思很简单大致都能看明白

get/set就是插数据去数据(key----value对应着来):

redis 127.0.0.1:6379[1]> set test chen
OK
redis 127.0.0.1:6379[1]> set test2 zhou
OK
redis 127.0.0.1:6379[1]> get test
"chen"
redis 127.0.0.1:6379[1]> get test2
"zhou"
setnx就是插入数据看看是否存在相同的key值,如果存在则不执行返回0,如果不存在则执行返回1:

redis 127.0.0.1:6379[1]> setnx test 11111
(integer) 0
redis 127.0.0.1:6379[1]> get test
"chen"
append更是简单了,就是平时用的追加字符:

redis 127.0.0.1:6379[1]> get test
"chen"
redis 127.0.0.1:6379[1]> append test @gmail.com
(integer) 14
redis 127.0.0.1:6379[1]> get test
"chen@gmail.com"
getrange/setrange的用法就是简单的字符串操作,取出相对位置的字符,或者设置。这里实验的时候出现了问题,主要是对setrange的操作不熟悉,大家看看下面操作就明白了setrange是对相应位置进行等数量替换的,5的位置就是@那里,QQ.com是6个字符去替换mysina.com10个字符时,只能是代替前6个了。

redis 127.0.0.1:6379[1]> set test3 chen@gmail.com
OK
redis 127.0.0.1:6379[1]> get test3
"chen@gmail.com"
redis 127.0.0.1:6379[1]> setrange test3 5 mysina.com
(integer) 15
redis 127.0.0.1:6379[1]> get test3
"chen@mysina.com"
redis 127.0.0.1:6379[1]> setrange test3 5 QQ.com
(integer) 15
redis 127.0.0.1:6379[1]> get test3
"chen@QQ.com.com"
incr/decr/incrby/decrby就是自加自减的意思后面的incrby/decrby加入了步长,还有就是如果没有key值的话,它也可以操作:

redis 127.0.0.1:6379> set age 20
OK
redis 127.0.0.1:6379> incr age
(integer) 21
redis 127.0.0.1:6379> get age
"21"
redis 127.0.0.1:6379> incrby age 5
(integer) 26
redis 127.0.0.1:6379> incrby ss 3
(integer) 3
redis 127.0.0.1:6379> incr tt 2
(error) ERR wrong number of arguments for 'incr' command
redis 127.0.0.1:6379> get tt
(nil)
mget/mset就是批量的插入取出。

hash表的基本操作与string一样,hget/hset/hsetnx,happend,hstrlen,hgetrange/hsetrange,hincr/hdecr/hincrby/hdecrby,hmget/hmset,就是在前面加了个H而已。它特别适合存储对象数据。基本的操作跟上面是一样的,这里的key作为hash的名字来用了,在操作时需要给出你要插入取出的hash域:

redis 127.0.0.1:6379[1]> hset myhash ID 001
(integer) 1
redis 127.0.0.1:6379[1]> hset myhash name chen
(integer) 1
redis 127.0.0.1:6379[1]> hset myhash address ncut
(integer) 1
redis 127.0.0.1:6379[1]> hset myhash telephone 12332123
(integer) 1
redis 127.0.0.1:6379[1]> hget myhash id
(nil)
redis 127.0.0.1:6379[1]> hget myhash ID
"001"
redis 127.0.0.1:6379[1]> hget myhash name
"chen"
redis 127.0.0.1:6379[1]> hget myhash0000 name
(nil)
其他的操作跟上面一样雷同即可:

redis 127.0.0.1:6379[1]> hkeys myhash
1) "ID"
2) "name"
3) "address"
4) "telephone"
redis 127.0.0.1:6379[1]> hvals myhash
1) "001"
2) "chen"
3) "ncut"
4) "12332123"
redis 127.0.0.1:6379[1]> hgetall myhash
1) "ID"
2) "001"
3) "name"
4) "chen"
5) "address"
6) "ncut"
7) "telephone"
8) "12332123"
lists链表结构,主要的功能就是pop/push,其中啊key作为链表的名字来用了。还有主要的就是(lr)pop/()lrpush就决定了它能够左(右)插入右(左)取出,即可做队列又可做堆栈来用。它还有一个奇怪的地方在于它的阻塞机制,b(lr)pop,b(lr)push

redis 127.0.0.1:6379[1]> lpush mylist chen
(integer) 1
redis 127.0.0.1:6379[1]> lpush mylist chen2
(integer) 2
redis 127.0.0.1:6379[1]> lpush mylist chen3
(integer) 3
redis 127.0.0.1:6379[1]> rpush mylist chen2
(integer) 4
redis 127.0.0.1:6379[1]> rpush mylist chen3
(integer) 5
redis 127.0.0.1:6379[1]> lrange mylist 0 -1
1) "chen3"
2) "chen2"
3) "chen"
4) "chen2"
5) "chen3"
redis 127.0.0.1:6379[1]> lpop mylist
"chen3"
redis 127.0.0.1:6379[1]> lrange mylist 0 -1
1) "chen2"
2) "chen"
3) "chen2"
4) "chen3"
这里是set和insert的用法:

redis 127.0.0.1:6379[1]> linsert mylist before chen chenInsert
(integer) 5
redis 127.0.0.1:6379[1]> lrange mylist 0 -1
1) "chen2"
2) "chenInsert"
3) "chen"
4) "chen2"
5) "chen3"
redis 127.0.0.1:6379[1]> lset mylist 0 setTest
OK
redis 127.0.0.1:6379[1]> lrange mylist 0 -1
1) "setTest"
2) "chenInsert"
3) "chen"
4) "chen2"
5) "chen3"
这里是lrem的用法:这里需要一个参数,当给出的值是0时默认是全部删除

redis 127.0.0.1:6379[1]> lrange mylist 0 -1
1) "chen"
2) "chen"
3) "setTest"
4) "chenInsert"
5) "chen"
6) "chen2"
7) "chen3"
redis 127.0.0.1:6379[1]> lrem mylist chen
(error) ERR wrong number of arguments for 'lrem' command
redis 127.0.0.1:6379[1]> lrem mylist 1 chen ###此处需要表明删除的个数,就是说从左边删除几个值
(integer) 1
redis 127.0.0.1:6379[1]> lrange mylist 0 -1
1) "chen"
2) "setTest"
3) "chenInsert"
4) "chen"
5) "chen2"
6) "chen3"
redis 127.0.0.1:6379[1]> lrange mylist 0 -11) "chen"2) "setTest"3) "chenInsert"4) "chen"5) "chen2"6) "chen3"redis 127.0.0.1:6379[1]> lrem mylist 0 chen(integer) 2redis 127.0.0.1:6379[1]> lrange mylist 0 -11) "setTest"2) "chenInsert"3) "chen2"4) "chen3"
其他的自己试验下吧,这里就不做演示了,太多了

还有set和sorted set 比较上面几种不常用还复杂,就不做演示了。