在redis里边,除了”\n”和空格 不能作为名字的组成内容外,其他内容都可以作为key的名字部分。名字长度不做要求。
不同redis数据库切换:
用select ,默认是操作数据库0,默认共有16个数据库,所以数据库Index是从0到15操作例子
[root@fengniu020 ~]# cd /usr/local/ [root@fengniu020 local]# cd redis/ [root@fengniu020 redis]# l -bash: l: command not found [root@fengniu020 redis]# ls dump.rdb redis-cli redis.conf redis-server [root@fengniu020 redis]# ./redis-cli redis 127.0.0.1:6379> key * (error) ERR unknown command 'key' redis 127.0.0.1:6379> keys * 1) "name" 2) "addr" 3) "age" redis 127.0.0.1:6379> del addr (integer) 1 redis 127.0.0.1:6379> keys * 1) "name" 2) "age" redis 127.0.0.1:6379> type age string redis 127.0.0.1:6379> set fuck 123 OK redis 127.0.0.1:6379> keys * 1) "name" 2) "fuck" 3) "age" redis 127.0.0.1:6379> type fuck string redis 127.0.0.1:6379> keys *e 1) "name" 2) "age" redis 127.0.0.1:6379> rename fuck fuckyou OK redis 127.0.0.1:6379> get fuckyou "123" redis 127.0.0.1:6379> dbsize (integer) 3 redis 127.0.0.1:6379> expire age 10 (integer) 1 redis 127.0.0.1:6379> ttl age (integer) 0 redis 127.0.0.1:6379> keys * 1) "name" 2) "fuckyou" redis 127.0.0.1:6379> set age 12 OK redis 127.0.0.1:6379> expire age 20 (integer) 1 redis 127.0.0.1:6379> ttl age (integer) 17 redis 127.0.0.1:6379> keys * 1) "name" 2) "age" 3) "fuckyou" redis 127.0.0.1:6379> ttl age (integer) 8 redis 127.0.0.1:6379> select 1 OK redis 127.0.0.1:6379[1]> select 0 OK redis 127.0.0.1:6379> move fuckyou 1 (integer) 1 redis 127.0.0.1:6379> select 1 OK redis 127.0.0.1:6379[1]> keys * 1) "fuckyou" redis 127.0.0.1:6379[1]> flushdb OK redis 127.0.0.1:6379[1]> keys * (empty list or set) redis 127.0.0.1:6379[1]> flushall OK redis 127.0.0.1:6379[1]> select 0 OK redis 127.0.0.1:6379> keys * (empty list or set) redis 127.0.0.1:6379>
string是redis最基本的类型
redis的string可以包含任何数据。包括jpg图片或者序列化的对象。
单个value值最大上限是1G字节。
如果只用string类型,redis就可以被看作加上持久化特性的memcache
incr: increament 增长
该指令可以对key进行累加1操作,默认是累加1操作,类似i++操作
该指令可以针对 新key或已有key 进行操作
新key:创建该key并累加1,其值为1
已有key:key的信息值类型要求必须为整型的
decr 的操作模式与incr一致,不过是减1操作
substr: 对内容进行截取,包括start和end标记位置内容
操作示例
redis 127.0.0.1:6379> keys * (empty list or set) redis 127.0.0.1:6379> set age 18 OK redis 127.0.0.1:6379> set name lily OK redis 127.0.0.1:6379> set addr guangzhou OK redis 127.0.0.1:6379> keys * 1) "name" 2) "addr" 3) "age" redis 127.0.0.1:6379> mset tel 10086 wife lucy OK redis 127.0.0.1:6379> keys * 1) "addr" 2) "age" 3) "name" 4) "wife" 5) "tel" redis 127.0.0.1:6379> mget wife tel name 1) "lucy" 2) "10086" 3) "lily" redis 127.0.0.1:6379> incr tel (integer) 10087 redis 127.0.0.1:6379> incr lily (integer) 1 redis 127.0.0.1:6379> get name "lily" redis 127.0.0.1:6379> get age "18" redis 127.0.0.1:6379> incr age (integer) 19 redis 127.0.0.1:6379> decr age (integer) 18 redis 127.0.0.1:6379> incrby age 2 (integer) 20 redis 127.0.0.1:6379> decrby age 2 (integer) 18 redis 127.0.0.1:6379> append name andlucy (integer) 11 redis 127.0.0.1:6379> get name "lilyandlucy" redis 127.0.0.1:6379> substr name 7 10 "lucy" redis 127.0.0.1:6379>
redis 127.0.0.1:6379> get lala "lala" redis 127.0.0.1:6379> setnx lala 'lala_new' (integer) 0 redis 127.0.0.1:6379> get lala "lala" redis 127.0.0.1:6379> setex haircolor 10 red OK redis 127.0.0.1:6379> get haircolor "red" redis 127.0.0.1:6379> get haircolor (nil) redis 127.0.0.1:6379> set email [email protected] OK redis 127.0.0.1:6379> get email "[email protected]" redis 127.0.0.1:6379> setrange email 7 gmail.com (integer) 16 redis 127.0.0.1:6379> get email "[email protected]" redis 127.0.0.1:6379> setrange email 6 gmail.com (integer) 16 redis 127.0.0.1:6379> get email "[email protected]" redis 127.0.0.1:6379>
list类型其实就是一个双向链表。通过push,pop操作从链表的头部或者尾部添加删除元素。
这使得list既可以用作栈,也可以用作队列。
上进上出 :栈
上进下出 :队列
该list链表类型应用场合:
获得最新的10个登录用户信息: select * from user order by logintime desc limit 10;
以上sql语句可以实现用户需求,但是数据多的时候,全部数据都要受到影响,对数据库的负载比较高。必要情况还需要给关键字段(id或logintime)设置索引,索引也比较耗费系统资源
如果通过list链表实现以上功能,可以在list链表中只保留最新的10个数据,每进来一个新数据就删除一个旧数据。每次就可以从链表中直接获得需要的数据。极大节省各方面资源消耗
通过list链表保存登录系统的最新5个用户信息:
jim xiaoli jack xiaoming linken mary tom
设置一个list链表key newlogin,内部有5个元素:
redis 127.0.0.1:6379> lpush newlogin tom (integer) 1 redis 127.0.0.1:6379> lpush newlogin mary (integer) 2 redis 127.0.0.1:6379> lpush newlogin linken (integer) 3 redis 127.0.0.1:6379> lpush newlogin xiaoming (integer) 4 redis 127.0.0.1:6379> lpush newlogin jack (integer) 5
该链表每增加一个新元素,就删除一个旧元素:
redis 127.0.0.1:6379> lpush newlogin xiaoli (integer) 6 redis 127.0.0.1:6379> rpop newlogin "tom"
通过范围查找链表内部的全部元素信息:
redis 127.0.0.1:6379> lrange newlogin 0 5 1) "xiaoli" 2) "jack" 3) "xiaoming" 4) "linken" 5) "mary" redis 127.0.0.1:6379> lrange newlogin 0 100 1) "xiaoli" 2) "jack" 3) "xiaoming" 4) "linken" 5) "mary"
redis的set是string类型的无序集合。
set元素最大可以包含(2的32次方-1)个元素。
关于set集合类型除了基本的添加删除操作,其他有用的操作还包含集合的取并集(union),交集(intersection),差集(difference)。通过这些操作可以很容易的实现sns中的好友推荐功能。
注意:每个集合中的各个元素不能重复。
该类型应用场合:qq好友推荐。
tom朋友圈(与某某是好友):mary jack xiaoming wang5 wang6
linken朋友圈(与某某是好友):yuehan daxiong luce wang5 wang6
创建tomSet的set集合类型数据,内部有5个元素:
redis 127.0.0.1:6379> keys * (empty list or set) redis 127.0.0.1:6379> sadd tomSet mary (integer) 1 redis 127.0.0.1:6379> sadd tomSet jack (integer) 1 redis 127.0.0.1:6379> sadd tomSet xiaoming (integer) 1 redis 127.0.0.1:6379> sadd tomSet wang5 (integer) 1 redis 127.0.0.1:6379> sadd tomSet wang6 (integer) 1 redis 127.0.0.1:6379> keys * 1) "tomSet"
再创建一个linkenSet的set集合key:
redis 127.0.0.1:6379> keys * 1) "tomSet" redis 127.0.0.1:6379> sadd linkenSet yuehan (integer) 1 redis 127.0.0.1:6379> sadd linkenSet daxiong (integer) 1 redis 127.0.0.1:6379> sadd linkenSet lucy (integer) 1 redis 127.0.0.1:6379> sadd linkenSet wang5 (integer) 1 redis 127.0.0.1:6379> sadd linkenSet wang6 (integer) 1 redis 127.0.0.1:6379> keys * 1) "tomSet" 2) "linkenSet"
两个集合取交集、并集:
redis 127.0.0.1:6379> keys * 1) "tomSet" 2) "linkenSet" redis 127.0.0.1:6379> sinter tomSet linkenSet 1) "wang5" 2) "wang6" redis 127.0.0.1:6379> sunion tomSet linkenSet 1) "xiaoming" 2) "mary" 3) "wang5" 4) "lucy" 5) "wang6" 6) "daxiong" 7) "yuehan" 8) "jack"
两个集合互取差集:
redis 127.0.0.1:6379> sdiff tomSet linkenSet 1) "xiaoming" 2) "mary" 3) "jack" redis 127.0.0.1:6379> sdiff linkenSet tomSet 1) "lucy" 2) "yuehan" 3) "daxiong"
和set一样sorted set也是string类型元素的集合,
不同的是每个元素都会关联一个权。
通过权值可以有序的获取集合中的元素
该Sort set类型适合场合:
获得热门帖子(回复量)信息:select * from message order by backnum desc limit 5;
(以上需求可以通过简单sql语句实现,但是sql语句比较耗费mysql数据库资源)
案例:利用sort set实现获取最热门的前5帖子信息
(以回复量决定是否为热门帖子)
排序集合中的每个元素都是值、权的组合
(之前的set集合类型每个元素就只是一个 值)
我们做一个sort set排序集合,里边只保留5个元素信息,该5个元素是回复量最高的贴,
每个帖子被回复的时候,都有机会进入该集合里边,但是只有回复量最高的前5个帖子会存在于在集合,回复量低的就被删除。
关于重复元素:
set类型:没有
list链表类型:有
Sort set类型:没有
zremrangebyrank 删除按权值从小到大排序后,给定范围的元素,先删除小的
创建一个sort set 排序集合hotmessage的key,内部有5个元素:
redis 127.0.0.1:6379> zadd hotmessage 102 11 (integer) 1 redis 127.0.0.1:6379> zadd hotmessage 141 12 (integer) 1 redis 127.0.0.1:6379> zadd hotmessage 159 13 (integer) 1 redis 127.0.0.1:6379> zadd hotmessage 72 14 (integer) 1 redis 127.0.0.1:6379> zadd hotmessage 203 15 (integer) 1 redis 127.0.0.1:6379> keys * 1) "tomSet" 2) "linkenSet" 3) "hotmessage"
按照权值由高到低的顺序获得具体元素值的信息:
redis 127.0.0.1:6379> zrevrange hotmessage 0 100 1) "15" 2) "13" 3) "12" 4) "11" 5) "14" redis 127.0.0.1:6379> zadd hotmessage 189 16 (integer) 1 redis 127.0.0.1:6379> zrevrange hotmessage 0 100 1) "15" 2) "16" 3) "13" 4) "12" 5) "11" 6) "14"
增加一个新元素,就删除一个旧元素(回复量最低的)
redis 127.0.0.1:6379> zremrangebyrank hotmessage 0 0 (integer) 1 redis 127.0.0.1:6379> zrevrange hotmessage 0 100 1) "15" 2) "16" 3) "13" 4) "12" 5) "11"
redis 127.0.0.1:6379> zrank hotmessage 15 (integer) 4 redis 127.0.0.1:6379> zrank hotmessage 11 (integer) 0 redis 127.0.0.1:6379> zrank hotmessage 12 (integer) 1 redis 127.0.0.1:6379> zrevrank hotmessage 11 (integer) 4 redis 127.0.0.1:6379> zcard hotmessage (integer) 5 redis 127.0.0.1:6379> zscore 11 (error) ERR wrong number of arguments for 'zscore' command redis 127.0.0.1:6379> zscore hotmessage 11 "102" redis 127.0.0.1:6379> zrem 11 (error) ERR wrong number of arguments for 'zrem' command redis 127.0.0.1:6379> zrem hotmessage 11 (integer) 1 redis 127.0.0.1:6379> zrevrange hotmessage 0 1000 1) "15" 2) "16" 3) "13" 4) "12" redis 127.0.0.1:6379> zincrby hotmessage 200 12 "341" redis 127.0.0.1:6379> zrevrange hotmessage 0 100 1) "12" 2) "15" 3) "16" 4) "13" redis 127.0.0.1:6379>
hash类型操作示例:
redis 127.0.0.1:6379> hmset goods goods_id 9 goods_name iphone6 goods_weight 120 goods_price 4 OK redis 127.0.0.1:6379> hkeys goods 1) "goods_id" 2) "goods_name" 3) "goods_weight" 4) "goods_price" 5) "goods_number" redis 127.0.0.1:6379> hvals goods 1) "9" 2) "iphone6" 3) "120" 4) "4500" 5) "4" redis 127.0.0.1:6379> hset lucy age 10 (integer) 1 redis 127.0.0.1:6379> keys * 1) "lucy" 2) "goods" redis 127.0.0.1:6379> hset lucy name lucy (integer) 1 redis 127.0.0.1:6379> hset lucy addr beijing (integer) 1 redis 127.0.0.1:6379> hget lucy (error) ERR wrong number of arguments for 'hget' command redis 127.0.0.1:6379> hget lucy name "lucy" redis 127.0.0.1:6379> hmget name age addr 1) (nil) 2) (nil) redis 127.0.0.1:6379> hmget lucy name age addr 1) "lucy" 2) "10" 3) "beijing" redis 127.0.0.1:6379> hincrby lucy age 10 (integer) 20 redis 127.0.0.1:6379> hexists lucy age (integer) 1 redis 127.0.0.1:6379> hexists lucy age0 (integer) 0 redis 127.0.0.1:6379> hdel lucy age (integer) 1 redis 127.0.0.1:6379> hexists lucy age (integer) 0 redis 127.0.0.1:6379> hlen lucy (integer) 2 redis 127.0.0.1:6379> hkeys lucy 1) "name" 2) "addr" redis 127.0.0.1:6379> hvals lucy 1) "lucy" 2) "beijing" redis 127.0.0.1:6379> hgetall goods 1) "goods_id" 2) "9" 3) "goods_name" 4) "iphone6" 5) "goods_weight" 6) "120" 7) "goods_price" 8) "4500" 9) "goods_number" 10) "4" redis 127.0.0.1:6379>
参考学习网站: Redis 命令参考