Home >Backend Development >PHP Tutorial >PHP-redis Chinese documentation
phpredis is an extension of php. It is very efficient and has a linked list sorting function, which is useful for creating memory-level module business relationships
Very useful; the following are the command usage tips officially provided by redis:
The download address is as follows:
https://github.com/owlient/phpredis (supports redis 2.0.4)
Redis::__construct constructor
$redis = new Redis();
connect, open link redis service
Parameters
host: string, service address
port: int, port number
timeout: float, link duration (optional, default is 0, no limit to link time)
Note: There is also time in redis.conf, the default is 300
pconnect, popen will not actively close the link
Refer to the above
setOption to set the redis mode
getOption to view the mode set by redis
ping to view the connection status
get to get the value of a certain key (string value)
If the key does not exist, return false
set writes key and value (string value)
If the writing is successful, return true
setex The written value with survival time
$redis->setex('key', 3600, 'value'); // sets key → value, with 1h TTL.
setnx determines whether it is repeated and writes the value
delete Delete the value of the specified key
Returns the number of deleted keys (long integer)
ttl
Get the survival time of a key
persist
Remove keys whose lifetime has expired
true if the key expires, false if it does not expire
mset (only available in redis version 1.1 or above)
Assign values to multiple keys at the same time
$redis->mset(array(‘key0’ => ‘value0’, ‘key1’ => ‘value1’));
multi, exec, discard
Enter or exit transaction mode
The parameter can be optional Redis::MULTI or Redis::PIPELINE. The default is Redis::MULTI
Redis::MULTI: execute multiple operations as one transaction
Redis::PIPELINE: allows (multiple) execution commands to be sent to the server simply and faster, but without any atomicity guarantee
discard: delete a transaction
Return value
multi(), returns a redis object and enters multi-mode mode. Once it enters multi-mode mode, all methods called in the future will return the same object until the exec() method is called.
watch, unwatch (after testing the code, the effect mentioned cannot be achieved)
Monitor whether the value of a key has been changed by other programs. If this key is modified between watch and exec (method), the execution of this MULTI/EXEC transaction will fail (return false)
unwatch cancels all keys monitored by this program
Parameters, a list of a pair of keys
$redis->watch('x');
subscribe *
Method callback. Note that this method may change in the future
publish *
Post content to a certain channel. Note that this method may change in the future
exists
Determine whether the key exists. exists true and is not false
incr, incrBy
key中的值进行自增1,如果填写了第二个参数,者自增第二个参数所填的值
decr, decrBy
做减法,使用方法同incr
getMultiple
传参
由key组成的数组
返回参数
如果key存在返回value,不存在返回false
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<0从尾到头删除|count|个值为value的元素
lInsert
在名称为为key的list中,找到值为pivot 的value,并根据参数Redis::BEFORE | Redis::AFTER,来确定,newvalue 是放在 pivot 的前面,或者后面。如果key不存在,不会插入,如果 pivot不存在,return -1
rpoplpush
返回并删除名称为srckey的list的尾元素,并将该元素添加到名称为dstkey的list的头部
var_dump(
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
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’
例子
var_dump(
var_dump($redis->sort(’s’, array(‘sort’ => ‘desc’, ‘store’ => ‘out’))); // (int)5
string命令
getSet
返回原来key中的值,并将value写入key
append
string,名称为key的string的值在后面加上value
$redis->get(‘key’);
getRange (方法不存在)
返回名称为key的string中start至end之间的字符
$redis->getRange(‘key’, -5, -1);
setRange (方法不存在)
改变key的string中start至end之间的字符为value
$redis->get(‘key’);
strlen
得到key的string的长度
$redis->strlen(‘key’);
getBit/setBit
返回2进制信息
zset(sorted set)操作相关
zAdd(key, score, member):向名称为key的zset中添加元素member,score用于排序。如果该元素已经存在,则根据score更新该元素的顺序。
zRange(key, start, end,withscores):返回名称为key的zset(元素已按score从小到大排序)中的index从start到end的所有元素
zDelete, zRem
zRem(key, member) :删除名称为key的zset中的元素member
$redis->zRange(‘key’, 0, -1);
zRevRange(key, start, end,withscores):返回名称为key的zset(元素已按score从大到小排序)中的index从start到end的所有元素.withscores: 是否输出socre的值,默认false,不输出
zRangeByScore, zRevRangeByScore
$redis->zRangeByScore(key, star, end, array(withscores, limit ));
返回名称为key的zset中score >= star且score <= end的所有元素
zCount
$redis->zCount(key, star, end);
返回名称为key的zset中score >= star且score <= end的所有元素的个数
zRemRangeByScore, zDeleteRangeByScore
$redis->zRemRangeByScore(‘key’, star, end);
删除名称为key的zset中score >= star且score <= end的所有元素,返回删除个数
zSize, zCard
返回名称为key的zset的所有元素的个数
zScore
$redis->zScore(key, val2);
返回名称为key的zset中元素val2的score
zRank, zRevRank
$redis->zRevRank(key, val);
返回名称为key的zset(元素已按score从小到大排序)中val元素的rank(即index,从0开始),若没有val元素,返回“null”。zRevRank 是从大到小排序
zIncrBy
$redis->zIncrBy(‘key’, increment, ‘member’);
如果在名称为key的zset中已经存在元素member,则该元素的score增加increment;否则向集合中添加该元素,其score的值为increment
zUnion/zInter
参数
keyOutput
arrayZSetKeys
arrayWeights
aggregateFunction Either “SUM”, “MIN”, or “MAX”: defines the behaviour to use on duplicate entries during the zUnion.
对N个zset求并集和交集,并将最后的集合保存在dstkeyN中。对于集合中每一个元素的score,在进行AGGREGATE运算前,都要乘以对于的WEIGHT参数。如果没有提供WEIGHT,默认为1。默认的AGGREGATE是SUM,即结果集合中元素的score是所有集合对应元素进行SUM运算的值,而MIN和MAX是指,结果集合中元素的score是所有集合对应元素中最小值和最大值。
Hash操作
hSet
$redis->hSet(‘h’, ‘key1’, ‘hello’);
向名称为h的hash中添加元素key1—>hello
hGet
$redis->hGet(‘h’, ‘key1’);
返回名称为h的hash中key1对应的value(hello)
hLen
$redis->hLen(‘h’);
返回名称为h的hash中元素个数
hDel
$redis->hDel(‘h’, ‘key1’);
删除名称为h的hash中键为key1的域
hKeys
$redis->hKeys(‘h’);
返回名称为key的hash中所有键
hVals
$redis->hVals(‘h’)
返回名称为h的hash中所有键对应的value
hGetAll
$redis->hGetAll(‘h’);
返回名称为h的hash中所有的键(field)及其对应的value
hExists
$redis->hExists(‘h’, ‘a’);
名称为h的hash中是否存在键名字为a的域
hIncrBy
$redis->hIncrBy(‘h’, ‘x’, 2);
将名称为h的hash中x的value增加2
hMset
$redis->hMset(‘user:1’, array(‘name’ => ‘Joe’, ‘salary’ => 2000));
向名称为key的hash中批量添加元素
hMGet
$redis->hmGet(‘h’, array(‘field1’, ‘field2’));
返回名称为h的hash中field1,field2对应的value
redis 操作相关
flushDB
清空当前数据库
flushAll
清空所有数据库
randomKey
随机返回key空间的一个key
select
选择一个数据库
move
转移一个key到另外一个数据库
$redis->get(‘x’); // will return 42
rename, renameKey
给key重命名 FALSE
renameNx
与remane类似,但是,如果重新命名的名字已经存在,不会替换成功
setTimeout, expire
设定一个key的活动时间(s)
$redis->setTimeout(‘x’, 3);
expireAt
key存活到一个unix时间戳时间
$redis->expireAt(‘x’, time() + 3);
keys, getKeys
返回满足给定pattern的所有key
dbSize
查看现在数据库有多少key
auth
密码认证
$redis->auth(‘foobared’);
bgrewriteaof
使用aof来进行数据库持久化
$redis->bgrewriteaof();
slaveof
选择从服务器
$redis->slaveof(‘10.0.1.7’, 6379);
save
将数据同步保存到磁盘
bgsave
将数据异步保存到磁盘
lastSave
返回上次成功将数据保存到磁盘的Unix时戳
info
返回redis的版本信息等详情
type
返回key的类型值
string: Redis::REDIS_STRING
set: Redis::REDIS_SET
list: Redis::REDIS_LIST
zset: Redis::REDIS_ZSET
hash: Redis::REDIS_HASH
other: Redis::REDIS_NOT_FOUND
以上就介绍了PHP-redis中文文档,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。