Home Database Mysql Tutorial Redis基本数据类型和相关操作

Redis基本数据类型和相关操作

Jun 07, 2016 pm 03:29 PM
redis Basic operate data Related type material

Redis资料汇总 http://blog.nosqlfan.com/html/3537.html Redis命令参考 http://www.redisdoc.com/en/latest/ 在线尝试Redis网站 http://try.redis.io/ Redis: Re mote Di ctionary S erver Redis支持的健数据类型 字符串类型 string 散列表类型 hash 列表类

Redis资料汇总 http://blog.nosqlfan.com/html/3537.html

Redis命令参考 http://www.redisdoc.com/en/latest/

在线尝试Redis网站 http://try.redis.io/

Redis: Remote Dictionary Server

Redis支持的健值数据类型

字符串类型 string

散列表类型 hash

列表类型 list

集合类型 set

有序集合类型 zset

Redis安装

redis-server Redis服务器

redis-cli Redis命令行客户端

redis-benchmark 性能测试工具

redis-check-aof AOF文件修复工具

redis-check-dump RDB文件检查工具

Redis命令

KEYS pattern 获得符合规则的键名列表

EXISTS key 判断一个键是否存在

DEL key 删除一个键

TYPE key 获得键值的数据类型

字符串数据类型 STRING

字符串类型是Redis基本数据类型,能存储任何形式的字符串,包括二进制数据。

SET key value 赋值

GET key 取值

INCR key 递增数字(所有Redis命令都是原子操作)

Redis键命名实践 “对象类型:对象ID:对象属性”,对于多个单词推荐用.分割。如键user:1:friends表示ID为1的用户的好友列表。

INCRBY key increment 增加指定整数

DECRBY key decrement 减少指定的整数

INCRBYFLOAT key increment 增加指定浮点数

APPEND key value 向尾部追加值,返回追加后字符串的长度

STRLEN key 返回键值的长度

MGET key [key ...] 获取多个健值

MSET key value [key value ...] 设置多个键值

位操作

GETBIT key offset 获得一个字符串指定位置的二进制位的值(0或1)

SETBIT key offset value 设置字符串类型键指定位置的二进制值,返回该位置的旧值

BITCOUNT key [start] [end] 统计字符串类型中值为1的二进制位个数,可以指定字节的范围

BITOP operation destkey key [key...] (AND, OR, XOR, NOT)

散列类型 HASH

散列类型的键值也是一种字典结构,其存储了字段和字段值的映射,但字段值只能是字符串,不支持其他类型。(集合类型也不支持数据类型嵌套)

HSET key field value 赋值(插入时返回1,更新时返回0)

HGET key field 取值

HMSET key field value [field value ...]

HMGET key field [feild...]

HGETALL key

HEXISTS key field 判断字段是否存在

HSETNX key field value 当字段不存在时赋值

HINCRBY key field increment 增加数字

HDEL key field [field...] 删除字段

HKEYS key 只获取字段名

HVALS key 只获取字段值

HLEN key 获得字段数量

列表类型 LIST

列表类型可以存储一个有序的字符串列表,常用的操作是向列表两端添加元素或者获得某个列表的某一个片段。

内部使用双向链表实现,获取越接近两端的元素速度就越快,不过索引访问元素比较慢。列表类型能非常快速地完成关系数据库难以应付的场景,如社交网络新鲜事。

LPUSH key value [value...] 从列表左边增加元素

RPUSH key value [value...] 从列表右边增加元素

LPOP key 从列表左边弹出元素

RPOP key 从列表右边弹出元素

LLEN key 获取列表中元素的个数

LRANGE key start stop 获取列表片段(包括stop,支持负数表示从最右边开始计数)

LREM key count value 删除前count个值为value的元素(count>0时从左边开始删除,count

LINDEX key index 获取指定索引的元素值

LSET key index value 设置指定索引的元素值

LTRIM key start end 只保留指定片段

LINSERT key FEFORE|AFTER pivot value 首先从左到右查找pivot元素,再根据第二个参赛将value插入该元素前面或后面。

RPOPLPUSH source destination 将一个元素转移到另一个列表 ,原子操作。当source和destination相同时会不断将对尾元素移到队首,实现网站监控系统。

集合类型 SET

集合类型每个元素不同,且无序。

SADD key member [member...] 增加元素,返回成功加入元素的个数

SREM key member [member...] 删除元素

SMEMBERS key 获得集合中所有元素

SISMEMBER key member 判断是否存在集合中

集合间运算

SDIFF [destination] key [key ...] 多个集合求差运算A-B,并存储到destination中

SINTER [destination] key [key ...] 多个集合执行交运算

SUNION [destination] key [key...] 多个集合求并运算

SCARD key 集合中元素个数

SRANDMEMEBER key [count] 随机获得集合中元素(当count>0时随机获取count个不重复元素,count

SPOP key 从集合中随机弹出一个元素

有序集合类型 ZSET

列表类型通过链表实现,获取靠近两端的数据速度极快,当元素增加后中间元素比较慢,更适合"新鲜事"或“日志”这样很少访问中间元素的应用。

有序集合类型通过散列表和跳跃表实现的,所以即使读取位于中间位置也很快O(NlgN)。

列表中不能简单调整某个元素位置,有序集合可以。有序集合更耗费内存。

ZADD key score member [score member...] 加入一个元素和该元素的分数(分数可以是整数或小数,+inf和-inf表示正负无穷)

ZSCORE key member 获得元素的分数

ZRANGE key start stop [WITHSCORE] 按照从从小到大的顺序返回start和stop之间所有元素(WITHSCORE表示带上分数)复杂度O(logn+m)

ZREVRANGE key start stop [WITHSCORE] 从大到小的顺序

ZRANGEBYSCORE key min max [WITHSCORE] [LIMIT offset count] 按照从小到大返回分数在min和max之间的元素

ZINCRBY key increment member 增加某个元素的分数

ZCARD key 获得集合中元素个数

ZCOUNT key min max 获得指定范围内的元素个数

ZREM key member [member ...] 删除一个或多个元素

ZREMRANGEBYRANK key start stop 按照元素分数从小到大的顺序删除指定排名范围内的所有元素,并返回删除元素的个数

ZREMRANGEBYSCORE key min max 删除指定分数范围内的所有元素

ZRANK key member 获得元素的排名

ZREVRANK key member

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to build the redis cluster mode How to build the redis cluster mode Apr 10, 2025 pm 10:15 PM

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to clear redis data How to clear redis data Apr 10, 2025 pm 10:06 PM

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

How to use the redis command How to use the redis command Apr 10, 2025 pm 08:45 PM

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

How to read redis queue How to read redis queue Apr 10, 2025 pm 10:12 PM

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

How to use redis lock How to use redis lock Apr 10, 2025 pm 08:39 PM

Using Redis to lock operations requires obtaining the lock through the SETNX command, and then using the EXPIRE command to set the expiration time. The specific steps are: (1) Use the SETNX command to try to set a key-value pair; (2) Use the EXPIRE command to set the expiration time for the lock; (3) Use the DEL command to delete the lock when the lock is no longer needed.

How to implement the underlying redis How to implement the underlying redis Apr 10, 2025 pm 07:21 PM

Redis uses hash tables to store data and supports data structures such as strings, lists, hash tables, collections and ordered collections. Redis persists data through snapshots (RDB) and append write-only (AOF) mechanisms. Redis uses master-slave replication to improve data availability. Redis uses a single-threaded event loop to handle connections and commands to ensure data atomicity and consistency. Redis sets the expiration time for the key and uses the lazy delete mechanism to delete the expiration key.

How to read the source code of redis How to read the source code of redis Apr 10, 2025 pm 08:27 PM

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.

How to make message middleware for redis How to make message middleware for redis Apr 10, 2025 pm 07:51 PM

Redis, as a message middleware, supports production-consumption models, can persist messages and ensure reliable delivery. Using Redis as the message middleware enables low latency, reliable and scalable messaging.

See all articles