Home  >  Article  >  Backend Development  >  PHP operation Redis detailed case

PHP operation Redis detailed case

迷茫
迷茫Original
2017-01-23 15:39:461446browse

$redis = new Redis();

connect, open link redis service

Parameters

host: string, service address

port: int, port number

timeout: float, link duration (optional, The default is 0, no limit to the link time)

Note: There is also time in redis.conf, the default is 300

pconnect, popen will not actively close the link

Reference Above

setOption sets the redis mode

getOption checks the mode set by redis

ping checks the connection status

get gets the value of a certain key (string value)

If the key does not exist, return false

set writes the key and value (string value)

If the writing is successful, return ture

setex Write value with survival time

$redis->setex('key', 3600, 'value'); // sets key → value, with 1h TTL.

setnx Determine whether it is repeated, write value

$redis->setnx('key', 'value');
$redis->setnx('key', 'value');

delete Delete the value of the specified key

Return the number of deleted keys ( Long integer)

$redis->delete('key1', 'key2');
$redis->delete(array('key3', 'key4', 'key5'));

Get the survival time of a key

persist

Remove the key whose survival time expires

If the key expires true if not Expires false

mset (redis version 1.1 or above can only be used)

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 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 is 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 key

parameters monitored by this program, A list of a pair of keys

$redis->watch('x');
$ret = $redis->multi() ->incr('x') ->exec();
subscribe *

Method callback. Note that this method may change in the future

publish *

Publish content to a certain channel. Note that this method may change in the future

exists

Determine whether the key exists. If there is true but not false

incr, the value in incrBy

key will be incremented by 1. If the second parameter is filled in, it will be incremented by the value filled in the second parameter

$redis->incr('key1');
$redis->incrBy('key1', 10);

decr, decrBy

Do subtraction, the usage method is the same as incr

getMultiple

Pass parameters

Array composed of keys

Return parameters

If the key exists, return value, if it does not exist, return 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 related operations

lPush

$redis->lPush(key, value);

Add an element with a value of value to the left (head) of the list named key

rPush

$redis->rPush(key, value);

Add an element with a value of value to the right (tail) of the list named key

lPushx/rPushx

$redis->lPushx(key, value);

Add an element with value to the left (head)/right (tail) of the list named key. If value already exists, it will not be added

lPop/rPop

$redis->lPop('key');

Output the first element from the left (head)/right (tail) of the list named key, delete the element

blPop/brPop

$redis->blPop('key1', 'key2', 10);

The block version of the lpop command. That is, when timeout is 0, if the list named key i does not exist or the list is empty, the command ends. If timeout>0, when encountering the above situation, wait for timeout seconds. If the problem is not solved, perform pop operation on the list starting from keyi+1

lSize

$redis->lSize('key');

The return name is key How many elements does the list have?

lIndex, lGet

$redis->lGet('key', 0);

Returns the element at the index position in the list named key

lSet

$redis->lSet('key', 0, 'X');

to the name named The element at the index position in the key's list is assigned value

lRange, lGetRange

$redis->lRange('key1', 0, -1);

returns the elements between start and end in the list named key (end is -1, return all)

lTrim, listTrim

$redis->lTrim('key', start, end);

Intercept the list named key and keep the elements between start and end

lRem, lRemove

$redis->lRem('key', 'A', 2);

Delete count The element whose value is value in the list named key. count is 0, delete all elements with value, count>0 deletes count elements with value from beginning to end, count2992b7809a7f8c726b6a088afe98dabf= star且score 67918fd90413e2ebe6d16c82a8d0dfdf= star且score dedad31280ebd53bcec48a8feb5b05b9= star且score 7dd843fc73bab33b50dba3a77fc01680hello

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

$key = $redis->randomKey();

select

选择一个数据库

move

转移一个key到另外一个数据库

$redis->select(0); // switch to DB 0
$redis->set('x', '42'); // write 42 to x
$redis->move('x', 1); // move to DB 1
$redis->select(1); // switch to DB 1
$redis->get('x'); // will return 42

rename, renameKey

给key重命名

$redis->set('x', '42');
$redis->rename('x', 'y');
$redis->get('y'); // → 42
$redis->get('x'); // → `FALSE`

renameNx

与remane类似,但是,如果重新命名的名字已经存在,不会替换成功

setTimeout, expire

设定一个key的活动时间(s)

$redis->setTimeout('x', 3);

expireAt

key存活到一个unix时间戳时间

$redis->expireAt('x', time() + 3);

keys, getKeys

返回满足给定pattern的所有key

$keyWithUserPrefix = $redis->keys('user*');

dbSize

查看现在数据库有多少key

$count = $redis->dbSize();

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