PHP-redis命令文档
PHP-redis命令文档
Redis本质上一个Key/Value数据库,与Memcached类似的NoSQL型数据库,但是他的数据可以持久化的保存在磁盘上,解决了服务重启后数据不丢失的问题,它的值可以是string(字符串)、list(列表)、sets(集合)或者是ordered sets(被排序的集合),所有的数据类型都具有push/pop、add/remove、执行服务端的并集、交集、两个sets集中的差别等等操作,这些操作都是具有原子性的,Redis还支持各种不同的排序能力
Redis 2.0更是增加了很多新特性,如:提升了性能、增加了新的数据类型、更少的利用内存(AOF和VM)
phpredis是php的一个扩展,效率是相当高有链表排序功能,对创建内存级的模块业务关系
以下是redis官方提供的命令使用技巧:
Redis::__construct构造函数
$redis = new Redis();
connect, open 链接redis服务
参数
host: string,服务地址
port: int,端口号
timeout: float,链接时长 (可选, 默认为 0 ,不限链接时间)
注: 在redis.conf中也有时间,默认为300
ping 查看连接状态
get 得到某个key的值(string值)
如果该key不存在,return 特殊值 nil;如果 key 不是字符串类型,那么返回一个错误
set 写入key 和 value(string值)
在 Redis 2.6.12 版本以前, set命令总是返回 OK 。
从 Redis 2.6.12 版本开始, set 在设置操作成功完成时,才返回 OK 。
如果设置了 NX 或者 XX ,但因为条件没达到而造成设置操作未执行,那么命令返回空批量回复(NULL Bulk Reply)。
setex 带生存时间的写入值
将值 value 关联到 key ,并将 key 的生存时间设为 seconds (以秒为单位)。
如果 key 已经存在, SETEX 命令将覆写旧值。
这个命令类似于以下两个命令:
SET key value
EXPIRE key seconds # 设置生存时间
不同之处是, SETEX 是一个原子性(atomic)操作,关联值和设置生存时间两个动作会在同一时间内完成,
该命令在 Redis 用作缓存时,非常实用。
#粒子
$redis->setex(‘key’, 3600, ‘value’); // sets key → value, with 1h TTL.
setnx 判断是否重复的,写入值
#将 key 的值设为 value ,当且仅当 key 不存在。 若给定的 key 已经存在,则 SETNX 不做任何动作
$redis->setnx(‘key’, ‘value’);
$redis->setnx(‘key’, ‘value’);
delete 删除指定key的值
返回已经删除key的个数(长整数)
$redis->delete(‘key1′, ‘key2′);
$redis->delete(array(‘key3′, ‘key4′, ‘key5′));
ttl
得到一个key的生存时间
persist
移除生存时间到期的key
如果key到期 true 如果不到期 false
mset (redis版本1.1以上才可以用)
同时给多个key赋值
$redis->mset(array(‘key0′ => ‘value0′, ‘key1′ => ‘value1′));
exists
判断key是否存在。存在 true 不在 false
$redis->exists(‘test’);
incr, incrBy
key中的值进行自增1,如果填写了第二个参数,者自增第二个参数所填的值
$redis->incr(‘key1′);
$redis->incrBy(‘key1′, 10);
decr, decrBy
做减法,使用方法同incr
getMultiple
传参
由key组成的数组
返回参数
如果key存在返回value,不存在返回false
$redis->set(‘key1′, ‘value1′);
$redis->set(‘key2′, ‘value2′);
$redis->set(‘key3′, ‘value3′);
$redis->getMultiple(array(‘key1′, ‘key2′, ‘key3′));
$redis->lRem(‘key1′, ‘A’, 2);
$redis->lRange(‘key1′, 0, -1);#获取所有的列表
list相关操作
lPush
$redis->lPush(key, value);
在名称为key的list左边(头)添加一个值为value的 元素
rPush
$redis->rPush(key, value);
在名称为key的list右边(尾)添加一个值为value的 元素
lPushx/rPushx
$redis->lPushx(key, value);
在名称为key的list左边(头)/右边(尾)添加一个值为value的元素,如果value已经存在,则不添加
lPop/rPop
$redis->lPop(‘key’);
输出名称为key的list左(头)起/右(尾)起的第一个元素,删除该元素
blPop/brPop
$redis->blPop(‘key1′, ‘key2′, 10);
lpop命令的block版本。即当timeout为0时,若遇到名称为key i的list不存在或该list为空,则命令结束。如果timeout>0,则遇到上述情况时,等待timeout秒,如果问题没有解决,则对keyi+1开始的list执行pop操作
lSize
$redis->lSize(‘key’);
返回名称为key的list有多少个元素
lIndex, lGet
$redis->lGet(‘key’, 0);
返回名称为key的list中index位置的元素
lSet
$redis->lSet(‘key’, 0, ‘X’);
给名称为key的list中index位置的元素赋值为value
lRange, lGetRange
$redis->lRange(‘key1′, 0, -1);
返回名称为key的list中start至end之间的元素(end为 -1 ,返回所有)
lTrim, listTrim
$redis->lTrim(‘key’, start, end);
截取名称为key的list,保留start至end之间的元素
lRem, lRemove
$redis->lRem(‘key’, ‘A’, 2);
删除count个名称为key的list中值为value的元素。count为0,删除所有值为value的元素,count>0从头至尾删除count个值为value的元素,count
lInsert
在名称为为key的list中,找到值为pivot 的value,并根据参数Redis::BEFORE | Redis::AFTER,来确定,newvalue 是放在 pivot 的前面,或者后面。如果key不存在,不会插入,如果 pivot不存在,return -1
$redis->delete(‘key1′); $redis->lInsert(‘key1′, Redis::AFTER, ‘A’, ‘X’); $redis->lPush(‘key1′, ‘A’); $redis->lPush(‘key1′, ‘B’); $redis->lPush(‘key1′, ‘C’); $redis->lInsert(‘key1′, Redis::BEFORE, ‘C’, ‘X’);
$redis->lRange(‘key1′, 0, -1);
$redis->lInsert(‘key1′, Redis::AFTER, ‘C’, ‘Y’);
$redis->lRange(‘key1′, 0, -1);
$redis->lInsert(‘key1′, Redis::AFTER, ‘W’, ‘value’);
rpoplpush
返回并删除名称为srckey的list的尾元素,并将该元素添加到名称为dstkey的list的头部
$redis->delete(‘x’, ‘y’);
$redis->lPush(‘x’, ‘abc’); $redis->lPush(‘x’, ‘def’); $redis->lPush(‘y’, ’123′); $redis->lPush(‘y’, ’456′); // move the last of x to the front of y. var_dump($redis->rpoplpush(‘x’, ‘y’));
var_dump($redis->lRange(‘x’, 0, -1));
var_dump($redis->lRange(‘y’, 0, -1));
string(3) “abc”
array(1) { [0]=> string(3) “def” }
array(3) { [0]=> string(3) “abc” [1]=> string(3) “456″ [2]=> string(3) “123″ }
SET操作相关
sAdd
向名称为key的set中添加元素value,如果value存在,不写入,return false
$redis->sAdd(key , value);
sRem, sRemove
删除名称为key的set中的元素value
$redis->sAdd(‘key1′ , ‘set1′);
$redis->sAdd(‘key1′ , ‘set2′);
$redis->sAdd(‘key1′ , ‘set3′);
$redis->sRem(‘key1′, ‘set2′);
sMove
将value元素从名称为srckey的集合移到名称为dstkey的集合
$redis->sMove(seckey, dstkey, value);
sIsMember, sContains
名称为key的集合中查找是否有value元素,有ture 没有 false
$redis->sIsMember(key, value);
sCard, sSize
返回名称为key的set的元素个数
sPop
随机返回并删除名称为key的set中一个元素
sRandMember
随机返回名称为key的set中一个元素,不删除
sInter
求交集
sInterStore
求交集并将交集保存到output的集合
$redis->sInterStore(‘output’, ‘key1′, ‘key2′, ‘key3′)
sUnion
求并集
$redis->sUnion(‘s0′, ‘s1′, ‘s2′);
s0,s1,s2 同时求并集
sUnionStore
求并集并将并集保存到output的集合
$redis->sUnionStore(‘output’, ‘key1′, ‘key2′, ‘key3′);
sDiff
求差集
sDiffStore
求差集并将差集保存到output的集合
sMembers, sGetMembers
返回名称为key的set的所有元素
sort
排序,分页等
参数
‘by’ => ‘some_pattern_*’,
‘limit’ => array(0, 1),
‘get’ => ‘some_other_pattern_*’ or an array of patterns,
‘sort’ => ‘asc’ or ‘desc’,
‘alpha’ => TRUE,
‘store’ => ‘external-key’
例子
$redis->delete(‘s’); $redis->sadd(‘s’, 5); $redis->sadd(‘s’, 4); $redis->sadd(‘s’, 2); $redis->sadd(‘s’, 1); $redis->sadd(‘s’, 3);
var_dump($redis->sort(‘s’)); // 1,2,3,4,5
var_dump($redis->sort(‘s’, array(‘sort’ => ‘desc’))); // 5,4,3,2,1
var_dump($redis->sort(‘s’, array(‘sort’ => ‘desc’, ‘store’ => ‘out’))); // (int)5
string命令
getSet
返回原来key中的值,并将value写入key
$redis->set(‘x’, ’42′);
$exValue = $redis->getSet(‘x’, ‘lol’); // return ’42′, replaces x by ‘lol’
$newValue = $redis->get(‘x’)’ // return ‘lol’
append
string,名称为key的string的值在后面加上value
$redis->set(‘key’, ‘value1′);
$redis->append(‘key’, ‘value2′);
$redis->get(‘key’);
getRange (方法不存在)
返回名称为key的string中start至end之间的字符
$redis->set(‘key’, ‘string value’);
$redis->getRange(‘key’, 0, 5);
$redis->getRange(‘key’, -5, -1);
setRange (方法不存在)
改变key的string中start至end之间的字符为value
$redis->set(‘key’, ‘Hello world’);
$redis->setRange(‘key’, 6, “redis”);
$redis->get(‘key’);
strlen
得到key的string的长度
$redis->strlen(‘key’);
getBit/setBit
返回2进制信息

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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: 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.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

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).

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.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.

There are two types of Redis data expiration strategies: periodic deletion: periodic scan to delete the expired key, which can be set through expired-time-cap-remove-count and expired-time-cap-remove-delay parameters. Lazy Deletion: Check for deletion expired keys only when keys are read or written. They can be set through lazyfree-lazy-eviction, lazyfree-lazy-expire, lazyfree-lazy-user-del parameters.
