# phpredis **Repository Path**: siushin/phpredis ## Basic Information - **Project Name**: phpredis - **Description**: PHP操作Redis - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-09-20 - **Last Updated**: 2025-12-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: phpredis ## README - 笔记来源:[redis的入门与应用](https://www.imooc.com/learn/809) - 源码地址: - Redis官方手册: ## 什么是Redis - Redis是远程的 - Redis是基于内存的 - Redis是非关系型数据库 ## Redis的应用场景 - 缓存 - 队列 - 数据存储 ## Redis安装 - [macOS安装Redis](https://redis.io/docs/getting-started/installation/install-redis-on-mac-os/) ```shell brew --version # 安装Redis服务端 brew install redis # 启动redis服务【在命令行中启动】 redis-server # Ctrl-C退出 # 查找redis-server安装路径 which redis-server # /opt/homebrew/bin/redis-server # 查看redis.conf位置 find /opt/homebrew -name 'redis.conf' # /opt/homebrew/etc/redis.conf # 启动redis服务 brew services start redis # 查看redis brew services info redis # 关闭redis服务 brew services stop redis ``` ## Redis数据类型 | 数据类型 | 存储的值 | 读写能力 | |:--------:|:---------------------------------------:|:-----------------------:| | String | 可以是字符串、整数或浮点,统称为元素 | 对字符串操作 对整数类型加减 | | List | —个序列集合且每个节点都包好了一个元素 | 序列两端推入、或弹出元素 修剪、查找或移除元素 | | Set | 各不相同的元素 | 从集合中插入或者删除元素 | | Hash | 有key value的散列组,其中key是字符串,value是元素 | 按照key进行增加删除 | | Sort Set | 带分数的score-value有序集合,其中score为浮点,value为元素 | 集合插入,按照分数范围查找 | ## Redis客户端使用 ```shell redis-cli # 查看redis基本信息(包括:版本号、端口号、安装路径等信息) info ``` ## Redis数据类型 ### 字符串 ```shell 127.0.0.1:6379> set string1 "hello world" OK 127.0.0.1:6379> get string1 "hello world" ``` ### 整型 > 自增:incr 自减:decrby ```shell 127.0.0.1:6379> set string2 4 OK 127.0.0.1:6379> get string2 "4" 127.0.0.1:6379> incr string2 (integer) 5 127.0.0.1:6379> get string2 "5" 127.0.0.1:6379> decrby string2 2 (integer) 3 127.0.0.1:6379> get string2 "3" ``` ### list列表 > list不要求列表中的值唯一 > 可实现堆栈(FILO先进后出)和队列(FIFO先进先出)操作 ```shell 127.0.0.1:6379> lpush list1 12 (integer) 1 127.0.0.1:6379> lpush list1 13 (integer) 2 127.0.0.1:6379> lpush list1 13 (integer) 3 127.0.0.1:6379> llen list1 (integer) 3 127.0.0.1:6379> rpop list1 "12" 127.0.0.1:6379> llen list1 (integer) 2 ``` ### set集合 > Set 是 String 类型的无序集合。集合成员是唯一的,这就意味着集合中不能出现重复的数据。 | 命令 | 说明 | |-----------|-----------------------| | sadd | 向集合添加一个或多个成员 | | scard | 获取集合的成员数 | | sismember | 判断member元素是否是集合key的成员 | | smembers | 返回集合中的所有成员 | | srem | 移除集合中一个或多个成员 | ```shell 127.0.0.1:6379> sadd set1 12 (integer) 1 127.0.0.1:6379> scard set1 (integer) 1 127.0.0.1:6379> sadd set1 13 (integer) 1 127.0.0.1:6379> sadd set1 13 (integer) 0 127.0.0.1:6379> smembers set1 1) "12" 2) "13" 127.0.0.1:6379> scard set1 (integer) 2 127.0.0.1:6379> sismember set1 13 (integer) 1 127.0.0.1:6379> srem set1 13 (integer) 1 127.0.0.1:6379> sismember set1 13 (integer) 0 ``` ### hash散列 | 命令 | 说明 | |-------|---------------------------| | hset | 将哈希表key中的字段field的值设为value | | hget | 获取存储在哈希表中指定字段的值 | | hlen | 获取哈希表中字段的数量 | | hmget | 获取所有给定字段的值 | ```shell 127.0.0.1:6379> hset hash1 key1 12 (integer) 1 127.0.0.1:6379> hget hash1 key1 "12" 127.0.0.1:6379> hset hash1 key2 13 (integer) 1 127.0.0.1:6379> hset hash1 key3 13 (integer) 1 127.0.0.1:6379> hlen hash1 (integer) 3 127.0.0.1:6379> hset hash1 key3 14 (integer) 0 127.0.0.1:6379> hget hash1 key3 "14" 127.0.0.1:6379> hmget hash1 key1 key2 1) "12" 2) "13" ``` ### sorted set有序集合 > 有序集合和集合一样也是string类型元素的集合,且不允许重复的成员 > 不同的是每个元素都会关联一个double类型的分数。redis正是通过分数来为集合中的成员进行从小到大的排序 ```shell 127.0.0.1:6379> zadd zset1 10.1 val1 (integer) 1 127.0.0.1:6379> zadd zset1 11.2 val2 (integer) 1 127.0.0.1:6379> zadd zset1 9.2 val3 (integer) 1 127.0.0.1:6379> zcard zset1 (integer) 3 127.0.0.1:6379> zrange zset1 0 2 withscores 1) "val3" 2) "9.1999999999999993" 3) "val1" 4) "10.1" 5) "val2" 6) "11.199999999999999" 127.0.0.1:6379> zrank zset1 val2 (integer) 2 127.0.0.1:6379> zadd zset1 12.2 val3 (integer) 0 127.0.0.1:6379> zrange zset1 0 2 withscores 1) "val1" 2) "10.1" 3) "val2" 4) "11.199999999999999" 5) "val3" 6) "12.199999999999999" 127.0.0.1:6379> zadd zset1 12.2 val2 (integer) 0 127.0.0.1:6379> zrange zset1 0 2 withscores 1) "val1" 2) "10.1" 3) "val2" 4) "12.199999999999999" 5) "val3" 6) "12.199999999999999" ``` ## PHP安装Redis扩展 > 须提前安装phpize以及php-config - PHP安装Redis扩展 ```shell which phpize /opt/homebrew/bin/phpize which php-config /opt/homebrew/bin/php-config pecl install redis # 得到安装路径:/opt/homebrew/Cellar/php/8.2.10/pecl/20220829/redis.so ``` - 查找php.ini路径 ```shell php --ini Configuration File (php.ini) Path: /opt/homebrew/etc/php/8.2 Loaded Configuration File: /opt/homebrew/etc/php/8.2/php.ini Scan for additional .ini files in: /opt/homebrew/etc/php/8.2/conf.d Additional .ini files parsed: /opt/homebrew/etc/php/8.2/conf.d/ext-opcache.ini ``` - php.ini开启Redis扩展 ```ini extension = /opt/homebrew/Cellar/php/8.2.10/pecl/20220829/redis.so ``` ## PHP操作Redis 文档地址: