十二、数据类型

1.String字符串
字符串操作
• set key value [ex seconds] [px milliseconds] [nx|xx]
– 设置key及值,过期时间可以设置为秒或毫秒为单位
– nx只有key不存在,才对key进行操作
– xx只有key已存在,才对key进行操作
• setrange key offset value
– 从偏移量开始复写key的特定位的值
>set first "hello world"
>setrange first 6 “Redis” //改写为hello Redis
• strlen key
– 统计字串长度
>strlen first
• append key value
– 字符存在则追加,不存在则创建key及value
– 返回值为key的长度
>append myname jacob
• setbit key offset value
– 对key所存储字串,设置或清除特定偏移量上的位(bi
t)
– Value值可以为1或0,offset为0~2^32之间
– key不存在,则创建新key
>setbit bit 0 1
>setbit bit 1 0
bit:第0位为1,第一位为0
bitcount key
– 统计字串中被设置为1的比特位数量
>setbit bits 0 1 //0001
>setbit bits 3 1 //1001
>bitcount bits //结果为2
记录网站用户上线频率,如用户A上线了多少天等类似的数据如用户在某天上线,则使用setbit,以用户名为key,将网站上线日为offset,并在该offset上设置1,最后计算用户总上线次数时,使用bitcount用户名即可这样,即使网站运行10年,每个用户仅占用10*365比特位即456字节即可
>setbit peter 100 1 //网站上线100天用户登录了一次
>setbit peter 105 1 //网站上线105天用户登录了一次
>bitcount peter
• decr key
– 将key中的值减1,key不存在则先初始化为0,再减1
>set test 10
>decr test
• decrby key decrement
– 将key中的值,减去decrement
>set count 100
>decrby count 20
• get key
– 返回key所存储的字符串值
– 如果key不存在则返回特殊值nil
– 如果key的值不是字串,则返回错误,get只能处理字串
• getrange key start end
– 返回字串值中的子字串,截取范围为start和end
– 负数偏移量表述从末尾计数,-1表示最后一个字符,-2 表示倒数第二个字符
>set first “hello,the world”
>getrange first -5 -1
>getrange first 0 4
• incr key
– 将key的值加1,如果key不存在,则初始为0后再加1
– 主要应用为计数器
>set page 20
>incr page
• incrby key increment
– 将key的值增加increment
• incrbyfloat key increment
– 为key中所储存的值加上浮点数增量 increment
>set num 16.1
>incrbyfloat num 1.1
• mget key [key…]
– 一次获取一个或多个key的值,空格分隔,<具有原子性
>
• mset key value [key value …]
– 一次设置多个key及值,空格分隔,<具有原子性>
2. Hash表
2.1 Hash表简介
• Redis hash是一个string类型的field和value的映射表
• 一个key可对应多个field,一个field对应一个value
• 将一个对象存储为hash类型,较于每个字段都存储成
string类型更能节省内存
2.2 Hash表操作
• hset key field value
– 将hash表中field值设置为value
>hset site google 'www.g.cn‘
>hset site baidu 'www.baidu.com'
• hget key filed
– 获取hash表中field的值
>hget site google
• hmset key field value [field value…]
– 同时给hash表中的多个field赋值
>hmset site google www.g.cn baidu www.baidu.com
• hmget key field [field…]
– 返回hash表中多个field的值
>hmget site google baidu
• hkeys key
– 返回hash表中所有field名称
>hmset site google www.g.cn baidu www.baidu.com
>hkeys site
• hgetall key
– 返回hash表中所有field的值
• hvals key
– 返回hash表中所有filed的值
>hvals key
• hdel key field [field…]
– 删除hash表中多个field的值,不存在则忽略
>hdel site google baidu
3. List列表
3.1 List列表简介
• Redis的list是一个字符队列
• 先进后出
• 一个key可以有多个值
3.2 List列表操作
• lpush key value [value…]
– 将一个或多个值value插入到列表key的表头
– Key不存在,则创建key
>lpush list a b c //list1值依次为c b a
等同于lpush list a; lpush list b; lpush list c
• lrange key start stop
– 从开始位置读取key的值到stop结束
>lrange list 0 2 //从0位开始,读到2位为止
>lrange list 0 -1 //从开始读到结束为止
>lrange list 0 -2 //从开始读到倒数第2位值
知识讲解
知识讲解
List列表操作(续1)
• lpop key
– 移除并返回列表头元素数据,key不存在则返回nil
>lpop list //删除表头元素,可以多次执行
• llen key
– 返回列表key的长度
• lindex key index
– 返回列表中第index个值
如lindex key 0 ; lindex key 2; lindex key -2
• lset key index value
– 将key中index位置的值修改为value
>lset list 3 test //将list中第3个值修改为test
• rpush key value [value…]
– 将value插入到key的末尾
>rpush list3 a b c //list3值为a b c
>rpush list3 d //末尾插入d
• rpop key
– 删除并返回key末尾的值
– >rpush list3 a b c //list3值为a b c
>rpush list3 d //末尾插入d
3.3 其他操作指令
• del key [key…]
– 删除一个或多个key
• exists key
– 测试一个key是否存在
• expire key seconds
– 设置key的生存周期
• persist key
– 设置key永不过期
• ttl key
– 查看key的生存周期
• keys 匹配
– 找符合匹配条件的key,特殊符号用\屏蔽
>keys * //显示所有key
>keys h?llo //匹配hello,hallo,hxllo等
>keys h*llo //匹配hllo或heeello等
>keys h[ae]lo //匹配hello和hallo
• flushall
– 清空所有数据
• select id
– 选择数据库,id用数字指定,默认数据库为0
>select 0
>select 2
• move key db_id
– 将当前数据库的key移动到db_id数据库中
>move key 1 //将key移动到1数据库中
• rename key newkey
– 给key改名为newkey,newkey已存在时,则覆盖
其值
• renamenx key newkey
– 仅当newkey不存在时,才将key改名为newkey
• sort key
– 对key进行排序
>lpush cost 1 8 7 2 5
>sort cost //默认对数字排序,升序
>sort cost desc //降序
>lpush test “about” “site” “rename”
>sort test alpha //对字符排序
>sort cost alpha limit 0 3 //排序后提取0-3位数据
>sort cost alpha limit 0 3 desc
>sort cost STORE cost2 //对cost排序并保存为cost2
• type key
– 返回key的数据类型
4. 管理命令
• del key [key…]
– 删除一个或多个key
• exists key
– 测试一个key是否存在
• expire key seconds
– 设置key的生存周期
• persist key
– 设置key永不过期
• ttl key
– 查看key的生存周期

posted @ 2021-07-07 11:15  落樰兂痕  阅读(32)  评论(0编辑  收藏  举报