Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。 它支持多种类型的数据结构,如 字符串(strings), 散列(hashes), 列表(lists), 集合(sets), 有序集合(sorted sets) 与范围查询, bitmaps, hyperloglogs 和 地理空间(geospatial) 索引半径查询。 Redis 内置了 复制(replication),LUA脚本(Lua scripting), LRU驱动事件(LRU eviction),事务(transactions) 和不同级别的 磁盘持久化(persistence), 并通过 Redis哨兵(Sentinel)和自动 分区(Cluster)提供高可用性(high availability)。
[root@node1 bin]# redis-cli -p 6379 127.0.0.1:6379> 127.0.0.1:6379> FLUSHALL //清空所有数据库 OK 127.0.0.1:6379> keys * (empty array) 127.0.0.1:6379> 127.0.0.1:6379> set name hello OK 127.0.0.1:6379> keys * 1) "name" 127.0.0.1:6379> set age 1 OK 127.0.0.1:6379> keys * 1) "name" 2) "age" 127.0.0.1:6379> EXISTS name //判断当前的key是否存在 (integer) 1 127.0.0.1:6379> EXISTS age (integer) 1 127.0.0.1:6379> EXISTS name1 (integer) 0 127.0.0.1:6379> move name 1 //移除当前key到1库 (integer) 1 127.0.0.1:6379> keys * 1) "age" 127.0.0.1:6379> SELECT 1 OK 127.0.0.1:6379[1]> keys * 1) "name" 127.0.0.1:6379[1]> SELECT 0 OK 127.0.0.1:6379> 127.0.0.1:6379> keys * 1) "age" 127.0.0.1:6379> get age "1" 127.0.0.1:6379> EXPIRE age 5 //设置key的过期时间,单位秒 (integer) 1 127.0.0.1:6379> ttl age (integer) -2 127.0.0.1:6379> get age (nil) 127.0.0.1:6379> KEYS * (empty array) 127.0.0.1:6379> set age 2 OK 127.0.0.1:6379> set name hello OK 127.0.0.1:6379> KEYS * 1) "name" 2) "age" 127.0.0.1:6379> type name //查看key的类型 string 127.0.0.1:6379> type age string
更多命令,点击链接查看
127.0.0.1:6379> set mei 1 //设置值 OK 127.0.0.1:6379> get mei //获得 "1" 127.0.0.1:6379> EXISTS mei (integer) 1 127.0.0.1:6379> APPEND mei "hello" //追加字符串,如果当前key不存在,就相当于set key 127.0.0.1:6379> keys * 1) "mei" 127.0.0.1:6379> APPEND hehe ",wrold" (integer) 6 127.0.0.1:6379> keys * 1) "hehe" 2) "mei" (integer) 6 127.0.0.1:6379> get mei "1hello" 127.0.0.1:6379> APPEND mei ",wrold" (integer) 12 127.0.0.1:6379> STRLEN mei //获取字符串的长度 (integer) 12 127.0.0.1:6379> get mei "1hello,wrold" ######################################## 127.0.0.1:6379> set views 0 //设置初始浏览量为0 OK 127.0.0.1:6379> get views "0" 127.0.0.1:6379> INCR views //自增1 (integer) 1 127.0.0.1:6379> INCR views (integer) 2 127.0.0.1:6379> INCR views (integer) 3 127.0.0.1:6379> get views "3" 127.0.0.1:6379> decr views //自减1 (integer) 2 127.0.0.1:6379> decr views (integer) 1 127.0.0.1:6379> get views "1" 127.0.0.1:6379> INCRBY views 10 //可以设置步长,指定增量 (integer) 11 127.0.0.1:6379> DECRBY views 6 (integer) 5 127.0.0.1:6379> ######################################## 127.0.0.1:6379> FLUSHdb //清空数据库 OK 127.0.0.1:6379> 127.0.0.1:6379> keys * (empty array) 127.0.0.1:6379> set key1 "hello,word" OK 127.0.0.1:6379> get key1 "hello,word" 127.0.0.1:6379> GETRANGE key1 0 3 //截取字符串[0,3] "hell" 127.0.0.1:6379> GETRANGE key1 0 4 "hello" 127.0.0.1:6379> GETRANGE key1 0 -1 //获取全部的字符串 "hello,word" #替换 127.0.0.1:6379> set key2 hahaha OK 127.0.0.1:6379> get key2 "hahaha" 127.0.0.1:6379> SETRANGE key2 1 ello //替换指定位置开始的字符串 (integer) 6 127.0.0.1:6379> get key2 "helloa" ######################################### #setex (set with expire) 设置过期时间 #setnx (set if not exist) 不存在再设置 (在分布式锁中会常常使用) 127.0.0.1:6379> SETEX key3 15 "hello" // 设置key3值为hello,15秒过期 OK 127.0.0.1:6379> TTL key3 (integer) 10 127.0.0.1:6379> get key3 "hello" 127.0.0.1:6379> TTL key3 (integer) -2 127.0.0.1:6379> get key3 (nil) 127.0.0.1:6379> keys * 1) "key1" 2) "key2" 127.0.0.1:6379> setnx mykey "redis" //mykey之前不存在,就创建 (integer) 1 127.0.0.1:6379> keys * 1) "mykey" 2) "key1" 3) "key2" 127.0.0.1:6379> setnx mykey "hehe" //mykey已存在,创建失败 (integer) 0 127.0.0.1:6379> get mykey "redis" ######################################### 127.0.0.1:6379> keys * (empty array) 127.0.0.1:6379> MSET k1 v1 k2 v2 k3 v3 //同时设置多个值 OK 127.0.0.1:6379> 127.0.0.1:6379> keys * 1) "k2" 2) "k1" 3) "k3" 127.0.0.1:6379> MGET k1 k2 k3 //同时获取多个值 1) "v1" 2) "v2" 3) "v3" 127.0.0.1:6379> MSETNX k1 v1 k4 v4 //是一个原子性的操作,要么一起成功,要么一起失败。 (integer) 0 127.0.0.1:6379> get k4 (nil) #对象 127.0.0.1:6379> MSET user:1:name tc user:1:age 20 //设置一个user:1 对象 值为json字符来保存一个对象 OK 127.0.0.1:6379> mget user:1:name user:1:age 1) "tc" 2) "20" 127.0.0.1:6379> keys * 1) "k2" 2) "k1" 3) "user:1:age" 4) "k3" 5) "user:1:name"
127.0.0.1:6379> GETSET db redis //如果不存在值,则返回nil (nil) 127.0.0.1:6379> get db "redis" 127.0.0.1:6379> GETSET db hello //如果存在值,获取原来值,并设置新的值 "redis" 127.0.0.1:6379> get db "hello"
基本的数据类型,列表
//所有的list命令都是用l开头的,redis不区分大小写命令 127.0.0.1:6379> keys * (empty array) 127.0.0.1:6379> LPUSH list one //将一个值或者多个值,插入列表头部 (integer) 1 127.0.0.1:6379> LPUSH list two (integer) 2 127.0.0.1:6379> LPUSH list three (integer) 3 127.0.0.1:6379> LRANGE list 0 -1 1) "three" 2) "two" 3) "one" 127.0.0.1:6379> RPUSH list redis //将一个值或者多个值,插入列表尾部 (integer) 4 127.0.0.1:6379> LRANGE list 0 -1 1) "three" 2) "two" 3) "one" 4) "redis" 127.0.0.1:6379> LPOP list //移除列表的第一个元素 "three" 127.0.0.1:6379> RPOP list //移除列表的最后一个元素 "redis" 127.0.0.1:6379> LRANGE list 0 -1 1) "two" 2) "one" ######################################### //lindex 127.0.0.1:6379> LRANGE list 0 -1 1) "two" 2) "one" 127.0.0.1:6379> LINDEX list 1 //通过下标获得list 中某一个值 "one" 127.0.0.1:6379> LINDEX list 0 "two" ######################################### //LLEN,可以存在重复值 127.0.0.1:6379> LPUSH list one (integer) 3 127.0.0.1:6379> LRANGE list 0 -1 1) "one" 2) "two" 3) "one" 127.0.0.1:6379> LPUSH list two (integer) 4 127.0.0.1:6379> LRANGE list 0 -1 1) "two" 2) "one" 3) "two" 4) "one" 127.0.0.1:6379> LPUSH list three (integer) 5 127.0.0.1:6379> LLEN list //返回列表的长度 (integer) 5 ######################################### //lrem 移除指定的值: 取关 uid 127.0.0.1:6379> LRANGE list 0 -1 1) "three" 2) "two" 3) "one" 4) "two" 5) "one" 127.0.0.1:6379> LREM list 2 one //移除list集合中指定个数的值,精准匹配 (integer) 2 127.0.0.1:6379> LRANGE list 0 -1 1) "three" 2) "two" 3) "two" 127.0.0.1:6379> LREM list 1 two (integer) 1 127.0.0.1:6379> LRANGE list 0 -1 1) "three" 2) "two" ######################################### //trim 修剪 list 截断 127.0.0.1:6379> RPUSH list "hehe" (integer) 1 127.0.0.1:6379> RPUSH list "hehe1" (integer) 2 127.0.0.1:6379> RPUSH list "hehe2" (integer) 3 127.0.0.1:6379> RPUSH list "hehe3" (integer) 4 127.0.0.1:6379> LRANGE list 0 -1 1) "hehe" 2) "hehe1" 3) "hehe2" 4) "hehe3" 127.0.0.1:6379> LTRIM list 0 1 //通过下标截取指定的长度,这个list已经被改变了,截断了只剩下截取的元素 OK 127.0.0.1:6379> LRANGE list 0 -1 1) "hehe" 2) "hehe1" ######################################### //rpoplpush 移除列表的最后一个元素,并移动到新的列表中 127.0.0.1:6379> RPUSH list "hehe" (integer) 1 127.0.0.1:6379> RPUSH list "hehe1" (integer) 2 127.0.0.1:6379> RPUSH list "hehe2" (integer) 3 127.0.0.1:6379> RPUSH list "hehe3" (integer) 4 127.0.0.1:6379> LRANGE list 0 -1 1) "hehe" 2) "hehe1" 3) "hehe2" 4) "hehe3" 127.0.0.1:6379> RPOPLPUSH list mylist "hehe3" 127.0.0.1:6379> LRANGE list 0 -1 1) "hehe" 2) "hehe1" 3) "hehe2" 127.0.0.1:6379> LRANGE mylist 0 -1 1) "hehe3" ######################################### lset //将列表中指定下标的值替换为另外一个值,更新操作 127.0.0.1:6379> EXISTS list //判断列表是否存在 (integer) 0 127.0.0.1:6379> LSET list 0 item //如果不存在列表,我们去更新就会报错 (error) ERR no such key 127.0.0.1:6379> LPUSH list "hello1" (integer) 1 127.0.0.1:6379> LRANGE list 0 -1 1) "hello1" 127.0.0.1:6379> LSET list 0 item //如果存在,我们就去更新当前下标的值 OK 127.0.0.1:6379> LRANGE list 0 -1 1) "item" 127.0.0.1:6379> LSET list 1 item //如果不存在指标的值,就会报错 (error) ERR index out of range ######################################### //insert 插入值 127.0.0.1:6379> RPUSH list "hello" (integer) 1 127.0.0.1:6379> RPUSH list "word" (integer) 2 127.0.0.1:6379> LINSERT list before "word" "other" //在list表中,”word“ 之前插入值 (integer) 3 127.0.0.1:6379> LRANGE list 0 -1 1) "hello" 2) "other" 3) "word" 127.0.0.1:6379> LINSERT list after "word" "haha" //在list表中,”word“ 之后插入值 (integer) 4 127.0.0.1:6379> LRANGE list 0 -1 1) "hello" 2) "other" 3) "word" 4) "haha" #########################################
它实际上是一个链表,before node after ,left,right 都可以插入值
如果key不存在,创建新的链表
如果key存在,新增内容
如果移除了所有值,空列表,也代表不存在
在两边插入或改动值,效率最高。针对中间元素,相对来说效率会低一点。
。