redis基础

服用甘露聚糖肽后会不会出现副作用 https://m-mip.39.net/baidianfeng/mipso_4328642.html

Redis基础教程

一、Redis的安装

1、将redis的安装包拷贝到/opt文件夹下并解压。

2、安装gcc-c++

yuminstallgcc-c++-y

3、进入到已经解压的redis目录中。

4、在redis文件夹中使用make命令,等待make完成。

5、执行makeinstall命令。

在/usr/local/bin文件夹下会生成以下文件。

6、将/opt/redis/文件夹下的redis.conf文件复制到/usr/local/bin/kconfig/下(kconfig为自己创建的文件夹)。

7、进入kconfig将redis的运行方式改为后台运行。vimredis.conf

8、在/usr/local/bin文件夹下运行redis。

9、连接已经安装成功的redis。

至此redis的安装工作已经全部完成。

命令

ps-ef

grepredis查看redis的进程

shutown关闭redis(连接redis)

exit退出redis

10、后面会使用单机多Redis启动集群测试!

二、压力测试

1、压力测试工具。

三、Redis基础知识

1、Redis默认有16个数据库。

默认使用的是第0个,可以使用select进行切换数据库!

.0.0.1:[3]dbsize#查看数据库大小(integer)0.0.0.1:[3]setnamexuehailong#写入数据OK.0.0.1:[3]dbsize#查看数据库大小(integer)1.0.0.1:[3]select7#切换到7号数据库OK.0.0.1:[7]getname#查看刚写入的数据,由于是在3号数据库写入的,在当前7号数据库查不到数据(nil).0.0.1:[7]select3#切换到三号数据库OK.0.0.1:[3]getname#查看刚写入的数据"xuehailong".0.0.1:[3]keys*#查看数据库所有的key1)"name".0.0.1:[3].0.0.1:[7]setnumber\OK.0.0.1:[7]keys*1)"number".0.0.1:[7]getnumber"\\".0.0.1:[7]flushdb#清空当前库OK.0.0.1:[7]getnumber#查看数据(nil).0.0.1:[7].0.0.1:[7]setlikelove#在7号库写入数据OK.0.0.1:[7]getlike#查看"love".0.0.1:[7]select3#切换至三号库OK.0.0.1:[3]getname#查看"xuehailong".0.0.1:[3]flushall#清空所有库的内容OK.0.0.1:[3]getname#3号库查看(nil).0.0.1:[3]select7#切换至7号库OK.0.0.1:[7]getlike#查看(nil).0.0.1:[7]keys*#查看当前库中所有的key(emptyarray).0.0.1:[7]

flushdb清空当前库数据

flushall清空所有库中的数据

Redis是单线程的

Redis是很快的,官方表示,Redis是基于内存操作,CPU不是性能的瓶颈,Redis的瓶颈是根据机器的内存和网络带宽决定的。

Redis是基于C语言写的。

?Redis为什么基于单线程还那么快?

1、误区一:高性能的服务器一定是多线程的?

2、误区二:多线程(CPU上下文会切换)一定比单线程效率高。

先去CPU内存硬盘

核心:Redis是将所有的数据全部放在内存中的,所以说使用单线程去操作效率就是最高的,多线程(CPU上下文会切换:耗时间操作),对于内存系统来说,如果没有上下文切换效率就是最高的!多次读写都是在一个CPU上的,在内存情况下,这个就是最佳方案!

四、五大数据类型

Redis是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。它支持多种类型的数据结构,如字符串(strings),散列(hashes),列表(lists),集合(sets),有序集合(sortedsets)与范围查询,bitmaps,hyperloglogs和地理空间(geospatial)索引半径查询。Redis内置了复制(replication),LUA脚本(Luascripting),LRU驱动事件(LRUeviction),事务(transactions)和不同级别的磁盘持久化(persistence),并通过Redis哨兵(Sentinel)和自动分区(Cluster)提供高可用性(highavailability)。

Redis-Key

existskey#判断当前key是否存在.0.0.1:setage1OK.0.0.1:existsage(integer)1moveage1#将age从当前库中移至到1号库中.0.0.1:[2]getname"xuehailong".0.0.1:[2]expirename10#将当前keyname的过期时间设置为10秒(integer)1.0.0.1:[2]ttlname#ttl查看当前keyname的过期时间(integer)3.0.0.1:[2]ttlname(integer)2.0.0.1:[2]ttlname(integer)1.0.0.1:[2]ttlname(integer)1.0.0.1:[2]ttlname(integer)-2#数据已过期.0.0.1:[2]getname#数据已过期(nil).0.0.1:setnamexuehailongOK.0.0.1:setage1OK.0.0.1:typename#查看当前key的数据类型string.0.0.1:typeagestring

String(字符串类型)

#################################################################.0.0.1:setkey1v1#设置值OK.0.0.1:getkey1#查看值"v1".0.0.1:keys*#查看所有的key1)"age"2)"name"3)"key1".0.0.1:existskey1#查看key1是否存在(integer)1.0.0.1:appendkey1"hello"#追加key1的值,追加字符串,如果当前key不存在就相当于setkey(integer)7.0.0.1:getkey1yu"v1hello".0.0.1:appendkey1hello#追加key1的值(integer)12.0.0.1:getkey1"v1hellohello".0.0.1:strlenkey1#查看key1的长度(integer)12##################################################################i++#i+=.0.0.1:setviews0#初始数据为1OK.0.0.1:getviews"0".0.0.1:getviews#自增1数据自增为1"0".0.0.1:INCRviews(integer)1.0.0.1:INCRviews(integer)2.0.0.1:INCRviews(integer)3.0.0.1:getviews"3".0.0.1:getviews"3".0.0.1:decrviews#自减1数据自减一(integer)2.0.0.1:decrviews(integer)1.0.0.1:decrviews(integer)0.0.0.1:decrviews(integer)-1.0.0.1:INCRBYviews10#可以设置步长,指定增量(integer)9.0.0.1:INCRBYviews10(integer)19.0.0.1:DECRBYviews12#指定减量(integer)7#######################################################################字符串范围range.0.0.1:setkey1hello,xuehailong#设置key1的值OK.0.0.1:getkey1"hello,xuehailong".0.0.1:GETRANGEkey#查看指定范围的key值,截取key的值[0-3]"hell".0.0.1:GETRANGEkey10-1#查看key的所有值"hello,xuehailong"#替换.0.0.1:setkey2funchOK.0.0.1:getkey2"funch".0.0.1:SETRANGEkey21XX#替换指定位置开始的字符串(integer)5.0.0.1:getkey2"fXXch"########################################################################setex(setwithexpire)#设置过期时间#setnx(setifnotexists)#不存在设置(在分布式锁中经常使用).0.0.1:setexkeyhello#设置key3的值为hello,30秒后过期OK.0.0.1:ttlkey3#查看key3的过期时间(integer)24.0.0.1:ttlkey3(integer)23.0.0.1:ttlkey3(integer)-2.0.0.1:getkey3(nil).0.0.1:setnxmykeyredis#如果mykey不存在就创建mykey(integer)1.0.0.1:keys*1)"key2"2)"mykey"3)"key1".0.0.1:setnxmykeyfunch#如果mykey存在则创建失败(integer)0.0.0.1:getmykey"redis"#################################################################msetmget.0.0.1:msetk1b1k2b2k3b3#同时设置多个值OK.0.0.1:keys*1)"k3"2)"k2"3)"k1".0.0.1:mgetk1k2k3#同时获取多个值1)"b1"2)"b2"3)"b3".0.0.1:msetnxk1v1k4v4#msetnx是一个原子性的操作,要么一起成功,要么一起失败(integer)0.0.0.1:mgetk1k2k3k41)"b1"2)"b2"3)"b3"4)(nil)#对象setuser:1{name:zhangsan,age:18}#设置一个user:1对象值为json字符来保存一个对象!#这里的key是一个巧妙的设计:user:1{id}:{file}.0.0.1:msetuser:1:namezhangsanuser:1:age2OK.0.0.1:mgetuser:1:nameuser:1:age1)"zhangsan"2)"2"#######################################################################getset#先get再set.0.0.1:getsetdbredis#如果不存在值则返回nil(nil).0.0.1:getdb"redis".0.0.1:getsetdbfunch#如果存在值则先返回原来的值,再设置新的值"redis".0.0.1:getdb"funch"

String类型使用场景

?计数器

?统计数

?...

List(列表类型)

在Redis中,可以把List当成栈、队列、阻塞队列使用!

所有的List命令都是以l开头的,Redis不区分大小写。

#################################################################.0.0.1:lpushlistone#将一个值或多个值,插入到列表头部(右)(integer)1.0.0.1:lpushlisttwo(integer)2.0.0.1:lpushlistthree(integer)3.0.0.1:lrangelist0-1#获取list中的值1)"three"2)"two"3)"one".0.0.1:lrangelist01#通过区间来获取具体的值1)"three"2)"two".0.0.1:rpushlistright#将一个值或多个值,插入到列表尾部(左)(integer)4.0.0.1:lrangelist01#通过区间来获取具体的值1)"three"2)"two".0.0.1:lrangelist0-1#获取list中的值1)"three"2)"two"3)"one"4)"right"#################################################################LPOPRPOP.0.0.1:lrangelist0-11)"three"2)"two"3)"one"4)"right".0.0.1:lpoplist#移除list的第一个元素"three".0.0.1:lrangelist0-11)"two"2)"one"3)"right".0.0.1:rpoplist#移除list的最后一个元素"right".0.0.1:lrangelist0-11)"two"2)"one"#################################################################Lindex.0.0.1:lrangelist0-11)"two"2)"one".0.0.1:lindexlist1#通过下标获取list中的某一个值"one".0.0.1:lindexlist0"two"#################################################################llen.0.0.1:lpushlistone(integer)1.0.0.1:lpushlisttwo(integer)2.0.0.1:lpushlistthreee(integer)3.0.0.1:llenlist#返回列表的长度(integer)3#################################################################移除指定的值.0.0.1:lrangelist0-11)"one"2)"threee"3)"two"4)"one".0.0.1:lremlist1two#移除list列表中指定个数的值,精确匹配(integer)1.0.0.1:lrangelist0-11)"one"2)"threee"3)"one".0.0.1:lpushlistthreee(integer)4.0.0.1:lrangelist0-11)"threee"2)"one"3)"threee"4)"one".0.0.1:lremlist1threee#移除list列表中指定个数的值,精确匹配(integer)1.0.0.1:lrangelist0-11)"one"2)"threee"3)"one".0.0.1:lpushlistthreee(integer)4.0.0.1:lrangelist0-11)"threee"2)"one"3)"threee"4)"one".0.0.1:lremlist2threee#移除list列表中指定个数的值,精确匹配(integer)2.0.0.1:lrangelist0-11)"one"2)"one"#################################################################trim修剪。list截断.0.0.1:rpushmylisthello(integer)1.0.0.1:rpushmylisthello1(integer)2.0.0.1:rpushmylisthello2(integer)3.0.0.1:rpushmylisthello3(integer)4.0.0.1:ltrimmylist12#通过下标截取指定长度,截取的部分保留OK.0.0.1:lrangemylist0-11)"hello1"2)"hello2"#################################################################rpoplpush#移除例表中的最后一个元素,将它移动到新的列表中!.0.0.1:rpushmylisthello(integer)1.0.0.1:rpushmylisthello1(integer)2.0.0.1:rpushmylisthello2(integer)3.0.0.1:rpoplpushmylistmyotherlist#移除list列表中最后一个值到myotherlist列表中"hello2".0.0.1:lrangemylist0-1#查看mylist列表1)"hello"2)"hello1".0.0.1:keys*1)"myotherlist"2)"mylist".0.0.1:lrangemyotherlist0-1#查看目标列表myotherlist中存在该值1)"hello2"#################################################################lset#将列表中指定下标的值替换为另外一个值,相当于更新操作.0.0.1:existslist(integer)0.0.0.1:lsetlist0item#如果不存在该列表,更新就会报错(error)ERRnosuchkey.0.0.1:lpushlistvalue1(integer)1.0.0.1:lrangelist)"value1".0.0.1:lsetlist0item#如果存在,更新当前下标的值OK.0.0.1:lrangelist)"item".0.0.1:lsetlist1other#如果不存在当前下标,更新当前下标的值就会报错(error)ERRindexoutofrange#################################################################linsert#将某个具体的值插入到列表中某个元素的前面或者后面.0.0.1:lpushmylisthello(integer)1.0.0.1:lpushmylistworld(integer)2.0.0.1:linsertmylistbeforeworldother#在mylist列表中world的前面插入other(integer)3.0.0.1:lrangemylist0-11)"other"2)"world"3)"hello".0.0.1:linsertmylistafterworldnew#在mylist列表中world的后面插入other(integer)4.0.0.1:lrangemylist0-11)"other"2)"world"3)"new"4)"hello"

小结

?它实际上是一个链表,beforenodeafter,left,right都可以插入

?如果key不存在,创建新的链表

?如果key存在,新增内容

?如果移除了所有值,空链表,也代表不存在。

?在两边插入或者改动值,效率最高!中间元素,相对来说效率会低一点

消息排队!消息队列(Lpush),栈(LpushLpop)

Set(集合)

set的值是不能重读的!contans(包含)

#################################################################.0.0.1:saddmysethello#set集合中添加元素(integer)1.0.0.1:saddmysethellofinch(integer)1.0.0.1:saddmysetlovefinch(integer)1.0.0.1:smembersmyset#查看指定set的所有值1)"hellofinch"2)"hello"3)"lovefinch".0.0.1:sismembermysethello#判断某个值是不是在set集合中!(integer)1.0.0.1:sismembermysetworld(integer)0#################################################################.0.0.1:scardmyset#获取set集合中的元素个数(integer)4################################################################srem.0.0.1:clear.0.0.1:sremmysethello#移除set集合中的指定元素(integer)1.0.0.1:scardmysqt(integer)0.0.0.1:scardmyset(integer)3.0.0.1:smembersmyset1)"lovefinch1"2)"hellofinch"3)"lovefinch"#################################################################set是无序不重复集合.0.0.1:smembersmyset1)"lovefinch1"2)"hellofinch"3)"lovefinch".0.0.1:SRANDMEMBERmyset#随机抽出一个元素"lovefinch".0.0.1:SRANDMEMBERmyset"lovefinch1".0.0.1:SRANDMEMBERmyset"lovefinch".0.0.1:SRANDMEMBERmyset"lovefinch".0.0.1:SRANDMEMBERmyset#随机抽出一个元素"lovefinch1".0.0.1:SRANDMEMBERmyset"hellofinch".0.0.1:SRANDMEMBERmyset"lovefinch".0.0.1:SRANDMEMBERmyset"hellofinch".0.0.1:SRANDMEMBERmyset"lovefinch".0.0.1:SRANDMEMBERmyset"hellofinch".0.0.1:SRANDMEMBERmyset2#随机抽出指定个数的元素1)"hellofinch"2)"lovefinch"#################################################################删除指定的key,随机删除key.0.0.1:SMEMBERSmyset1)"lovefinch1"2)"hellofinch"3)"lovefinch".0.0.1:spopmyset#随机删除一些set集合中的元素!"hellofinch".0.0.1:spopmyset"lovefinch1".0.0.1:SMEMBERSmyset1)"lovefinch"#################################################################将一个指定的值,移动到另外一个set集合中,移动值的时候如果set不存在则会自动创建set.0.0.1:saddmysethello(integer)1.0.0.1:saddmysetworld(integer)1.0.0.1:saddmysetfinch(integer)1.0.0.1:saddmyset2se2(integer)1.0.0.1:SMOVEmysetmyset2finch#将一个指定的值,移动到另外一个set集合中(integer)1.0.0.1:SMEMBERSmyset1)"hello"2)"world".0.0.1:SMEMBERSmyset21)"finch"2)"se2"#################################################################-差集sdiff-交集sinter-并集sunion.0.0.1:saddkey1a(integer)1.0.0.1:saddkey1b(integer)1.0.0.1:saddkey1c(integer)1.0.0.1:saddkey2c(integer)1.0.0.1:saddkey2d(integer)1.0.0.1:saddkey2e(integer)1.0.0.1:sdiffkey1key2#差集1)"a"2)"b".0.0.1:sinterkey1key2#交集共同好友可以这样实现1)"c".0.0.1:sunionkey1key2#并集1)"c"2)"a"3)"b"4)"d"5)"e"

Hash(哈希,Map集合,key-map)

Map集合,key-map,这个值是map集合。Hash本质和String类型没有太大区别,还是一个简单的key-value。

setmyhashfieldhellofinch.0.0.1:hsetmyhashfield1hellofinch#set一个具体key-value(integer)1.0.0.1:hgetmyhashfield1#获取一个字段"hellofinch".0.0.1:hmsetmyhashfield1hellofield2world#同时set多个key-valueOK.0.0.1:hmgetmyhashfield1field2#同时获取多个字段值1)"hello"2)"world".0.0.1:hgetallmyhash#获取全部的数据1)"field1"2)"hello"3)"field2"4)"world".0.0.1:hdelmyhashfield1#删除hash指定key字段,对应的value值也没有了(integer)1.0.0.1:hgetallmyhash1)"field2"2)"world"#################################################################hlen获取hash的内容长度.0.0.1:hmsetmyhashfield1hellofield2world#同时set多个值OK.0.0.1:hgetallmyhash#获取全部数据1)"field2"2)"world"3)"field1"4)"hello".0.0.1:hlenmyhash#获取hash表的字段数量(integer)2#################################################################.0.0.1:hlenmyhash(integer)2.0.0.1:hexistsmyhashfield1#判断hash中的指定字段是否存在(integer)1.0.0.1:hexistsmyhashfield3#不存在(integer)0##################################################################只获得所有field#只获得所有的value.0.0.1:hkeysmyhash#只获得所有field1)"field2"2)"field1".0.0.1:hvalsmyhash#只获得所有的value1)"world"2)"hello"#################################################################hincrbyhdecrby.0.0.1:hsetmyhashfield35(integer)1.0.0.1:HINCRBYmyhashfield31#指定增量(integer)6.0.0.1:hincrbymyhashfield3-1(integer)5.0.0.1:hsetnxmyhashfield4hello#如果不存在则可以设置(integer)1.0.0.1:hsetnxmyhashfield4world#如果不存在则不可以设置(integer)0

hash适合存储经常变动的信息,更适合于对象的存储,String类型更加适合字符串存储。

Zset(有序集合)

在set的基础上增加了一个值,setk1v1,zsetk1scorev1

.0.0.1:zaddmyset1one#添加一个值(integer)1.0.0.1:zaddmyset2two3there#添加多个值(integer)2.0.0.1:zrangemyset0-1#获取所有zset1)"one"2)"two"3)"there"#################################################################-inf#代表无穷小+inf#代表无穷大.0.0.1:zaddsalaryxiaohong#添加三个用户(integer)1.0.0.1:zaddsalaryzhangsan(integer)1.0.0.1:zaddsalaryfinch(integer)1zrangebyscrekeyminmax.0.0.1:zrangebyscoresalary-inf+inf#将用户以salary从小到大进行排序1)"finch"2)"xiaohong"3)"zhangsan".0.0.1:zrangebyscoresalary-inf+infwithscores#将用户以salary从小到大进行排序并带上salary1)"finch"2)""3)"xiaohong"4)""5)"zhangsan"6)"".0.0.1:zrangebyscoresalary-infwithscores#显示工资以下的用户并进行从小到大的排序1)"finch"2)""3)"xiaohong"4)"".0.0.1:zrevrangesalary0-1#从大到小进行排序1)"zhangsan"2)"finch".0.0.1:zrevrangesalary0-1withscores1)"zhangsan"2)""3)"finch"4)""#################################################################zrem移除元素.0.0.1:zrangesalary0-11)"finch"2)"xiaohong"3)"zhangsan".0.0.1:zremsalaryxiaohong#移除有序集合中指定的元素,移除salary中的xiaohong(integer)1.0.0.1:zrangesalary0-11)"finch"2)"zhangsan".0.0.1:zcardsalary#获取有序集合中的个数(integer)2#################################################################.0.0.1:zaddmyset1hello2world3finch(integer)3.0.0.1:zcountmyset13#获取指定区间的成员数量(integer)3.0.0.1:zcountmyset12(integer)2预览时标签不可点收录于话题#个上一篇下一篇



转载请注明地址:http://www.sanbaicaoasb.com/scgx/8343.html
  • 上一篇文章:
  • 下一篇文章:
  • 热点文章

    • 没有热点文章

    推荐文章

    • 没有推荐文章