`
backend-develop
  • 浏览: 24555 次
社区版块
存档分类
最新评论

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命令详解

    以上是针对 Redis 的字符串类型和散列类型的基本介绍及其常用命令。接下来将继续探讨 Redis 的其他数据类型及其相关命令。 #### 三、其他数据类型 ##### 3. 列表类型(List) **列表类型** 允许在键(key)下存储一个...

    Redis开发环境搭建及常用命令

    #### 五、Redis常用命令示例 - **字符串操作**: - `SET key value`: 设置键的值。 - `GET key`: 获取键的值。 - **列表操作**: - `LPUSH key value [value ...]`: 从列表头部插入一个或多个值。 - `RPUSH key ...

    【Redis】——常用五大数据类型之Zset,算法数据结构

    1. `ZADD key score member [score member ...]`:此命令用于向有序集合key中添加一个或多个成员,每个成员都有一个对应的分数。如果成员已经存在,那么其分数会被更新。 2. `ZRANGE key start stop [WITHSCORES]`...

    Redis 命令实践教程.docx.docx

    SET key1 "value1" SET key2 "value2" EXEC ``` #### 六、总结 通过本教程的学习,读者可以掌握Redis的基本命令和常用操作。这些命令不仅包括简单的键值操作,还包括了复杂的列表、散列等高级数据结构的操作。此外...

    最新版redis redis-6.0.7.tar.gz

    三、Redis 常用命令: - `SET key value`:设置键值对。 - `GET key`:获取键对应的值。 - `DEL key`:删除键。 - `KEYS *`:查找所有匹配模式的键。 - `EXPIRE key seconds`:为键设置过期时间。 - `PUBLISH ...

    java操作redis

    tx.set("key1", "value1"); tx.set("key2", "value2"); List<Object> results = tx.exec(); ``` 9. **发布/订阅**:Jedis还允许你实现Redis的消息发布和订阅功能: ```java JedisPubSub subscriber = new ...

    Redis安装部署文档

    - **持久化**:Redis支持两种持久化方式——RDB(快照)和AOF(Append Only File)。 - **集群**:Redis集群提供了一种分布式存储解决方案,通过将数据分散到多个节点上,实现了数据的高可用性和负载均衡。 #### 二、...

    Redis基础配置笔记

    通过对Redis的基本安装和配置方法的学习,以及对Redis常用操作命令的理解,可以更好地掌握Redis的使用技巧,从而提高应用的开发效率和数据处理能力。此外,通过对比Memcached,我们了解到Redis不仅能够作为缓存使用...

    《Redis实战》电子书

    #### 六、Redis常用命令 - **键值相关命令**: - `keys`:查找符合给定模式的所有键。 - `exists`:判断指定的键是否存在。 - `del`:删除一个或多个键。 - `expire`:为键设置过期时间。 - `move`:将键从...

    2020.9最新-Redis视频.txt

    ### Redis常用命令 1. **字符串类型** - `SET key value`:设置键key的值为value。 - `GET key`:获取键key的值。 - `INCR key`:将键key的值加一。 2. **哈希类型** - `HSET key field value`:将哈希表key中...

    Redis实战

    #### 五、Redis常用命令 **3.1 键值相关命令** - **KEYS pattern:** 查找所有符合给定模式的键。 - **EXISTS key:** 判断给定键是否存在于当前数据库中。 - **DEL key [key ...]:** 删除一个或多个键。 - **...

    redis实战中文版

    #### 六、Redis常用命令 1. **键值相关命令**:如`EXISTS`(检查键是否存在)、`DEL`(删除键)、`EXPIRE`(设置键过期时间)等。 2. **服务器相关命令**:如`PING`(测试连接是否正常)、`SELECT`(切换数据库)、...

    Redis面试题.pdf

    Redis(Remote Dictionary Server)是一款开源的、基于内存的日志型、Key-Value数据库。它支持网络通信,可提供多种编程语言的API接口。 **特性概述:** 1. **高性能:**通过内存存储实现高速读写能力,每秒能处理...

    Jedis源码-供查询redis API源码

    **Jedis源码详解——深度探索Redis Java客户端** 在Java开发中,Jedis是与Redis进行交互的常用客户端库,它提供了丰富的API用于操作Redis的数据结构。本文将深入解析Jedis的源码,帮助开发者更好地理解和使用这个...

    redis5.0.4配置文件简析

    Redis 5.0.4引入了一种新的数据结构——Stream,这是一种可持久化的消息队列,支持多播功能。与传统的列表和集合不同,Stream提供了更高级别的抽象和功能,非常适合用于构建消息系统或事件日志等应用场景。 ### ...

    Tedu五阶段Redis分布式锁

    ##### 4.2 其他常用Redis命令 - **KEYS**:用于查找匹配模式的键,但在生产环境中不建议使用,因为它可能导致Redis服务阻塞。 - **EXISTS**:检查一个或多个键是否存在。 - **EXPIRE/PERSIST**:为键设置过期时间,...

Global site tag (gtag.js) - Google Analytics