Redis常用命令(1)——Key
DEL
格式:DEL key [key ...]
作用:删除一个或多个 key。不存在的 key 会被忽略。
返回值:被删除 key 的数量。
示例:
192.168.1.100:6379> set testkey 1
OK
# 删除成功返回删除key的个数
192.168.1.100:6379> del testkey
(integer) 1
# key不存在返回0
192.168.1.100:6379> del testkey
(integer) 0
EXISTS
格式:EXISTS key
作用:检查 key 是否存在。
返回值:存在返回 1,否则返回 0 。
示例:
192.168.1.100:6379> set testkey 1
OK
192.168.1.100:6379> exists testkey
(integer) 1
192.168.1.100:6379> del testkey
(integer) 1
192.168.1.100:6379> exists testkey
(integer) 0
EXPIRE
格式:EXPIRE key seconds
作用:为给定 key 设置生存时间,单位为秒,当 key 过期时,被自动删除。对一个已经带有生存时间的 key 执行 EXPIRE 命令,新指定的生存时间会取代旧的生存时间。PEXPIRE命令单位为毫秒。
返回值:设置成功返回 1 。key 不存在或者不能为 key 设置生存时间时,返回 0 。
示例:
192.168.1.100:6379> set testkey 1
OK
192.168.1.100:6379> expire testkey 5
(integer) 1
192.168.1.100:6379> exists testkey
(integer) 1
192.168.1.100:6379> exists testkey
(integer) 0
EXPIREAT
格式:EXPIREAT key timestamp
作用:为 key 设置生存时间,时间为UNIX 时间戳,单位为秒。PEXPIREAT命令单位为毫秒。
返回值:设置成功返回 1 。key 不存在或者不能为 key 设置生存时间时,返回 0 。
示例:
192.168.1.100:6379> set testkey 1
OK
192.168.1.100:6379> expireat testkey 1486456903
(integer) 1
192.168.1.100:6379> exists testkey
(integer) 1
192.168.1.100:6379> exists testkey
(integer) 0
KEYS
格式:KEYS pattern
作用:查找符合pattern格式要求的key列表。pattern可使用如下通配符
? 仅与一个任意字符匹配。
* 与任意字符匹配。
[] 与可选的字符匹配。
\x 对x进行转义。
返回值:符合pattern格式要求的key列表。
示例:
192.168.1.100:6379> set testkey 1
OK
192.168.1.100:6379> keys test*
1) "testkey"
(1.92s)
192.168.1.100:6379> keys test?ey
1) "testkey"
(1.72s)
192.168.1.100:6379> del 'testkey'
(integer) 1
192.168.1.100:6379> keys test*
(empty list or set)
(1.71s)
PERSIST
格式:PERSIST key
作用:移除给定 key 的生存时间。
返回值:成功返回1,失败或key不存在返回0。
示例:
192.168.1.100:6379> persist testkey
(integer) 0
192.168.1.100:6379> set testkey 1
OK
192.168.1.100:6379> expire testkey 15
(integer) 1
192.168.1.100:6379> persist testkey
(integer) 1
192.168.1.100:6379> exists testkey
(integer) 1
SORT
格式:SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC | DESC] [ALPHA] [STORE destination]
作用:对列表、集合、有序集合进行排序,返回排序的结果或保存到destination中。默认按数字进行排序。
返回值:排序结果或destination元素的个数。
示例:
# 初始化数据
192.168.1.100:6379> sadd testkey 4
(integer) 1
192.168.1.100:6379> sadd testkey 3
(integer) 1
192.168.1.100:6379> sadd testkey 8
(integer) 1
# 对集合进行排序,默认为升序
192.168.1.100:6379> sort testkey
1) "3"
2) "4"
3) "8"
# 对集合进行降序排序
192.168.1.100:6379> sort testkey DESC
1) "8"
2) "4"
3) "3"
# 使用limit限制范围
192.168.1.100:6379> sort testkey DESC limit 1 1
1) "4"
# 初始化数据
192.168.1.100:6379> set testkey_4 100
OK
192.168.1.100:6379> set testkey_3 50
OK
192.168.1.100:6379> set testkey_8 10
OK
# 使用by指定排序的参考key,用*匹配testkey的值
192.168.1.100:6379> sort testkey by testkey_* DESC
1) "4"
2) "3"
3) "8"
# 使用get返回关联数据
192.168.1.100:6379> sort testkey by testkey_* DESC get testkey_*
1) "100"
2) "50"
3) "10"
# sort默认按照数字进行排序,会转换为双精度数字,对于字符串直接使用sort会报错,添加alpha可要求sort按照字典顺序排序
192.168.1.100:6379> sadd testkey a
(integer) 1
192.168.1.100:6379> sadd testkey b
(integer) 1
192.168.1.100:6379> sadd testkey c
(integer) 1
192.168.1.100:6379> sort testkey
(error) ERR One or more scores can't be converted into double
192.168.1.100:6379> sort testkey alpha
1) "a"
2) "b"
3) "c"
TTL
格式:TTL key
作用:查询设置了生存时间的key的剩余时间,单位为秒。
返回值:正常情况下返回剩余时间,如果key没有设置生存时间返回-1,如果key不存在返回-2。
示例:
192.168.1.100:6379> set testkey 1
OK
192.168.1.100:6379> expire testkey 50
(integer) 1
192.168.1.100:6379> ttl testkey
(integer) 47
192.168.1.100:6379> ttl testkey
(integer) 29
192.168.1.100:6379> ttl testkey
(integer) -2
TYPE
格式:TYPE key
作用:返回key的类型
返回值:key的类型
示例:
# 字符串类型
192.168.1.100:6379> set testkey1 2
OK
192.168.1.100:6379> type testkey1
string
#集合类型
192.168.1.100:6379> sadd testkey2 d
(integer) 1
192.168.1.100:6379> type testkey2
set
#列表类型
192.168.1.100:6379> lpush testkey3 d
(integer) 1
192.168.1.100:6379> type testkey3
list
# 散列类型
192.168.1.100:6379> hset testkey4 t t
(integer) 1
192.168.1.100:6379> type testkey4
hash
# 有序集合类型
192.168.1.100:6379> zadd testkey5 100 d
(integer) 1
192.168.1.100:6379> type testkey5
zset
原文地址:http://caiguoqing.org/post/103
Redis常用命令(1)——Key的更多相关文章
- Redis常用命令(key、string、List)
1.Key 1.keys * 查询所有数据 2.exists key名 判断key名是否存在 3.move key名 数据库号(0-15) 移动数据key名到相应的数据库 4.expire ...
- Redis常用命令
Redis常用命令Redis提供了丰富的命令对数据库和各种数据类型进行操作,这些命令可以再Linux终端使用.1.键值相关命令2.服务器相关命令 一.键值相关命令 1.get get 键值 当 key ...
- 第2讲 Redis常用命令与高级应用
目录 一.redis数据类型 5. sorted sets类型和操作 二.Redis常用命令 1.键值相关命令 2.服务器相关命令 三. redis高级应用 1. 给redis服务器设置密码 2.持久 ...
- Redis常用命令手册:服务器相关命令
Redis提供了丰富的命令(command)对数据库和各种数据类型进行操作,这些command可以在Linux终端使用.在编程时,比如各类语言包,这些命令都有对应的方法.下面将Redis提供的命令做一 ...
- redis redis常用命令及内存分析总结(附RedisClient工具简介
redis常用命令及内存分析总结(附RedisClient工具简介 by:授客 QQ:1033553122 redis-cli工具 查看帮助 连接redis数据库 常用命令 exists key se ...
- Redis常用命令与高级应用
附: 127.0.0.1:6379> set xiaofei 小飞 OK 127.0.0.1:6379> get xiaofei "\xe5\xb0\x8f\xe9\xa3\x9 ...
- Redis快速起步及Redis常用命令大全
本系列教程内容提要 Java工程师之Redis实战系列教程教程是一个学习教程,是关于Java工程师的Redis知识的实战系列教程,本系列教程均以解决特定问题为目标,使用Redis快速解决在实际生产中的 ...
- redis常用命令及持久化机制
redis 常用命令 查找redis服务文件 find / -name redis-server 查找配置文件 find / -name redis.conf 启动服务时候,要指定配置文件 启动r ...
- No-sql之redis常用命令
转自:http://blog.csdn.net/nicewuranran/article/details/51793760 No-SQL之Redis 介绍 Redis是一种基于内存存储的key-val ...
随机推荐
- react 中发布订阅模式使用
react 中发布订阅模式使用 场景 怎么能将设计模式应用到我们的 React 项目中?以前一直在思考这个问题. 场景一 模块 A 模块 B 需要用到同一个数据 data,A 和 B 都会修改这份数据 ...
- Springer editorial manager上传latex文件
Springer的投稿系统editorial manager在初次投稿时只需要上传pdf文件,修改后要求上传Latex源文件.上传过程遇到好多问题,花了快两天才搞定,整理如下: 1. 主要上传的文件包 ...
- 01 C语言基本介绍
C语言特点 容易上手学习 结构化语言 执行效率高 处理的工作和活动偏底层 可以在多种计算机平台上编译(类似Java的跨平台) C语言历史 目前,C 语言是最广泛使用的系统程序设计语言之一 C 语言是最 ...
- 【题解】[USACO19DEC]Milk Visits G
题目戳我 \(\text{Solution:}\) 这题不要把思想局限到线段树上--这题大意就是求路径经过的值中\(x\)的出现性问题. 最开始的想法是值域线段树--看了题解发现直接\(vector\ ...
- HTTPS证书知识扫盲
1. 前言 现在搞网站域名不加个HTTPS就显得不专业,特别在使用JWT进行认证的接口一定要加HTTPS为你的接口增加一层安全屏障.今天就来聊聊配置HTTPS的关键SSL证书,也被称为CA证书. 2. ...
- 晋城6397.7539(薇)xiaojie:晋城哪里有xiaomei
晋城哪里有小姐服务大保健[微信:6397.7539倩儿小妹[晋城叫小姐服务√o服务微信:6397.7539倩儿小妹[晋城叫小姐服务][十微信:6397.7539倩儿小妹][晋城叫小姐包夜服务][十微信 ...
- C语言-入门级编程语言--编程小白首选
我们都知道计算机很厉害,利用计算机可以高效地处理和加工信息,随着计算机技术的发展,计算机的功能越来越强大,不但能够处理数值信息,而且还能处理各种文字.图形.图像.动画.声音等非数值信息. 在199 ...
- 【数论】HAOI2012 容易题
题目大意 洛谷链接 有一个数列A已知对于所有的\(A[i]\)都是\(1~n\)的自然数,并且知道对于一些\(A[i]\)不能取哪些值,我们定义一个数列的积为该数列所有元素的乘积,要求你求出所有可能的 ...
- 非科班8k,靠这套知识体系收入暴涨100%!
我是18年毕业,非科班,毕业即进入互联网行业.坐标深圳,java程序员,当时到手薪资8k左右. bat等大厂月薪薪资动辄20k,25k,还不包括"签字费",福利和奖金.当然,薪资也 ...
- beego log
package main import ( "github.com/astaxie/beego/logs" _ "xcms/routers" _ "x ...