This article mainly introduces 30 code examples of common methods for operating redis in PHP. This article actually has more than 30 methods, which can operate string type, list type and set type data. Friends in need can refer to it
redis There are a lot of operations. I used to see a relatively comprehensive blog, but I can’t find it now. After searching for a long time, I will summarize some examples of PHP processing redis. I personally think some examples are commonly used. The following examples are all based on the php-redis extension.
1, connect
Description: The instance is connected to a Redis.
Parameters: host: string, port: int
Return value: BOOL Successful return: TRUE; Failure return: FALSE
Example:
$redis = new redis();
$result = $redis->connect('127.0.0.1', 6379);
var_dump($result); //Result: bool(true)
?>
2, set
Description: Set the value of key and value
Parameter: Key Value
Return value: BOOL Successful return: TRUE; Failure return: FALSE
Example:
$ redis = new redis();
$redis->connect('127.0.0.1', 6379);
$result = $redis->set('test',"11111111111");
var_dump($result) ; //Result: bool(true)
?>
3, get
Description: Get the value of the specified key
Parameter: key
Return value: string or BOOL If the key does not exist, then Return FALSE. Otherwise, return the value corresponding to the specified key.
Example:
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$result = $redis->get(' test');
var_dump($result); //Result: string(11) "11111111111"
?>
4, delete
Description: Delete the specified key
Parameters: a key, Or an uncertain number of parameters, an array for each key: key1 key2 key3 ... keyN
Return value: number of items deleted
Example:
$redis = new redis();
$redis-> ;connect('127.0.0.1', 6379);
$redis->set('test',"1111111111111");
echo $redis->get('test'); //Result: 1111111111111
$ redis->delete('test');
var_dump($redis->get('test')); //Result: bool(false)
?>
5, setnx
Description: If the key does not exist in the database, set the key value parameter
Parameter: key value
Return value: BOOL Successful return: TRUE; Failure return: FALSE
Example:
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->set('test',"1111111111111");
$redis->setnx( 'test',"22222222");
echo $redis->get('test'); //Result: 1111111111111
$redis->delete('test');
$redis->setnx(' test',"22222222");
echo $redis->get('test'); //Result: 22222222
?>
6, exists
Description: Verify whether the specified key exists
Parameter key
Return value: Bool Successful return: TRUE; Failure return: FALSE
Example:
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->set('test',"1111111111111");
var_dump($redis->exists('test')); //Result: bool(true)
?>
7, incr
Description: Numeric increment to store the key value key.
Parameters: key value: The value that will be added to the key
Return value: INT the new value
Example:
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->set('test',"123");
var_dump($redis-> ;incr("test")); //Result: int(124)
var_dump($redis->incr("test")); //Result: int(125)
?>
8, decr
Description: Store key value in numerical decrement.
Parameters: key value: the value that will be added to the key
Return value: INT the new value
Example:
$redis = new redis();
$redis->connect('127.0 .0.1', 6379);
$redis->set('test',"123");
var_dump($redis->decr("test")); //Result: int(122)
var_dump ($redis->decr("test")); //Result: int(121)
?>
9, getMultiple
Description: Get the values of all specified keys. If one or more keys do not exist, the value of that key in the array is false
Parameters: Array of lists containing the values of the keys
Return value: Returns an array containing the values of all keys
Example:
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->set('test1',"1");
$ redis->set('test2',"2");
$result = $redis->getMultiple(array('test1','test2'));
print_r($result); //Result: Array ( [0] => 1 [1] => 2 )
?>
10, lpush
Description: Add a string value from the head of the list. Create the list if the key does not exist. If the key exists and is not a list, return FALSE.
Parameters: key, value
Return value: Returns the array length on success, false on failure
Example:
$redis = new redis();
$redis->connect('127.0.0.1 ', 6379);
$redis->delete('test');
var_dump($redis->lpush("test","111")); //Result: int(1)
var_dump($ redis->lpush("test","222")); //Result: int(2)
?>
11, rpush
Description: Add a string value from the end of the list. Create the list if the key does not exist. If the key exists and is not a list, return FALSE.
Parameters: key, value
Return value: Returns the array length on success, false on failure
Example:
$redis = new redis();
$redis->connect('127.0.0.1 ', 6379);
$redis->delete('test');
var_dump($redis->lpush("test","111")); //Result: int(1)
var_dump($ redis->lpush("test","222")); //Result: int(2)
var_dump($redis->rpush("test","333")); //Result: int( 3)
var_dump($redis->rpush("test","444")); //Result: int(4)
?>
12, lpop
Description: Return and move Remove the first element of the list
Parameters: key
Return value: Return the value of the first element if successful, false if failed
Example:
$redis = new redis();
$ redis->connect('127.0.0.1', 6379);
$redis->delete('test');
$redis->lpush("test","111");
$redis-> ;lpush("test","222");
$redis->rpush("test","333");
$redis->rpush("test","444");
var_dump($ redis->lpop("test")); //Result: string(3) "222"
?>
13, lsize,llen
Description: The length of the returned list. If the list does not exist or is empty, the command returns 0. If the key is not a list, this command returns FALSE.
Parameter: Key
Return value: Returns the array length on success, false on failure
Example:
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
$redis->lpush("test","111");
$redis->lpush("test","222");
$redis->rpush("test","333");
$redis->rpush("test","444");
var_dump($redis->lsize("test")); //Result: int(4)
?>
14, lget
Description: Returns the element specified by the specified key stored in the list. 0 first element, 1 second... -1 last element, -2 second last... Returns FALSE if the wrong index or key does not point to the list.
Parameter: key index
Return value: Return the value of the specified element if successful, false on failure
Example:
$redis = new redis();
$redis->connect('127.0 .0.1', 6379);
$redis->delete('test');
$redis->lpush("test","111");
$redis->lpush("test"," 222");
$redis->rpush("test","333");
$redis->rpush("test","444");
var_dump($redis->lget("test ",3)); //Result: string(3) "444"
?>
15, lset
Description: Assign a new value to the index specified in the list, if the index does not exist Return false.
Parameter: key index value
Return value: Return true if successful, false if failed
Example:
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
$redis-> lpush("test","111");
$redis->lpush("test","222");
var_dump($redis->lget("test",1)); //Result: string(3) "111"
var_dump($redis->lset("test",1,"333")); //Result: bool(true)
var_dump($redis->lget("test" ,1)); //Result: string(3) "333"
?>
16, lgetrange
Description:
Returns the specified storage from start to end in the specified key list in this area Element, lGetRange(key, start, end). 0 for the first element, 1 for the second element... -1 for the last element, -2 for the penultimate element...
Parameters: key start end
Return value: Successfully returns the value found, failure is false
Example:
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
$redis-> lpush("test","111");
$redis->lpush("test","222");
print_r($redis->lgetrange("test",0,-1)); / /Result: Array ( [0] => 222 [1] => 111 )
?>
17,lremove
Description: Remove count matches from the head of the list value. If count is zero, all matching elements are removed. If count is negative, the content is deleted from the end.
Parameter: key count value
Return value: Return the number of deleted items successfully, false on failure
Example:
$redis = new redis();
$redis->connect(' 127.0.0.1', 6379);
$redis->delete('test');
$redis->lpush('test','a');
$redis->lpush('test', 'b');
$redis->lpush('test','c');
$redis->rpush('test','a');
print_r($redis->lgetrange(' test', 0, -1)); //Result: Array ( [0] => c [1] => b [2] => a [3] => a )
var_dump($redis ->lremove('test','a',2)); //Result: int(2)
print_r($redis->lgetrange('test', 0, -1)); //Result: Array ( [0] => c [1] => b )
?>
18, sadd
Description: Add a value to a Key. If this value is already in this Key, return FALSE.
Parameter: key value
Return value: Return true on success, false on failure
Example:
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
var_dump($redis->sadd('test','111')); //Result: bool(true)
var_dump($redis- >sadd('test','333')); //Result: bool(true)
print_r($redis->sort('test')); //Result: Array ( [0] => 111 [1] => 333 )
?>
19, sremove
Description: Delete the value specified in Key
Parameter: key member
Return value: true or false
Example:
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
$redis- >sadd('test','111');
$redis->sadd('test','333');
$redis->sremove('test','111');
print_r( $redis->sort('test')); //Result: Array ([0] => 333)
?>
20,smove
Description: Move the value in Key1 Go to Key2
Parameter: srcKey dstKey member
Return value: true or false
Example
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
$redis->delete('test1');
$redis->sadd('test','111');
$redis- >sadd('test','333');
$redis->sadd('test1','222');
$redis->sadd('test1','444');
$redis ->smove('test',"test1",'111');
print_r($redis->sort('test1')); //Result: Array ( [0] => 111 [1] => 222 [2] => 444 )
?>
21, scontains
Description: Checks whether the specified value exists in the collection.
Parameter: key value
Return value: true or false
Example:
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
$redis-> sadd('test','111');
$redis->sadd('test','112');
$redis->sadd('test','113');
var_dump($redis ->scontains('test', '111')); //Result: bool(true)
?>
22,ssize
Description: Returns the number of stored values in the collection
Parameters: key
Return value: The number of arrays is returned on success, 0 on failure
Example:
$redis = new redis();
$redis->connect('127.0.0.1', 6379 );
$redis->delete('test');
$redis->sadd('test','111');
$redis->sadd('test','112');
echo $redis->ssize('test'); //Result: 2
?>
23, spop
Description: Randomly remove and return a value in key
Parameter: key
Return value: Return the deleted value on success, false on failure
Example:
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
$redis->sadd("test","111");
$redis->sadd("test","222");
$redis- >sadd("test","333");
var_dump($redis->spop("test")); //Result: string(3) "333"
?>
24,sinter
Description: Returns the intersection of all specified keys. If only a key is specified, this command generates members of the set. If a key does not exist, returns FALSE.
Parameters: key1, key2, keyN
Return value: Return array intersection on success, false on failure
Example:
$redis = new redis();
$redis->connect('127.0 .0.1', 6379);
$redis->delete('test');
$redis->sadd("test","111");
$redis->sadd("test"," 222");
$redis->sadd("test","333");
$redis->sadd("test1","111");
$redis->sadd("test1", "444");
var_dump($redis->sinter("test","test1")); //Result: array(1) { [0]=> string(3) "111" }
? >
25,sinterstore
Description: Execute the sInter command and store the result in the newly created variable.
Parameters:
Key: dstkey, the key to store the diff into.
Keys: key1, key2… keyN. key1..keyN are intersected as in sInter.
Return value: Return successfully, the number of intersections, false on failure
Example:
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
$redis->sadd("test","111");
$redis->sadd("test","222");
$redis->sadd("test","333") ;
$redis->sadd("test1","111");
$redis->sadd("test1","444");
var_dump($redis->sinterstore('new'," test","test1")); //Result: int(1)
var_dump($redis->smembers('new')); //Result: array(1) { [0]=> string( 3) "111" }
?> Return the merged set, false on failure
Example:
$redis->connect('127.0.0.1', 6379);
$redis ->delete('test');
$redis->sadd("test","111");
$redis->sadd("test","222");
$redis-> sadd("test","333");
$redis->sadd("test1","111"); $redis->sadd("test1","444");
print_r($redis ->sunion("test","test1")); //Result: Array ( [0] => 111 [1] => 222 [2] => 333 [3] => 444 )
?>
27, sunionstore
Description: Execute the sunion command and store the results in the newly created variable.
Parameters:
Key: dstkey, the key to store the diff into.
Keys: key1, key2… keyN. key1..keyN are intersected as in sInter.
Return value: Return successfully, the number of intersections, false on failure
Example:
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
$redis-> sadd("test","111");
$redis->sadd("test","222");
$redis->sadd("test","333");
$redis-> ;sadd("test1","111");
$redis->sadd("test1","444");
var_dump($redis->sinterstore('new',"test","test1" )); //Result: int(4)
print_r($redis->smembers('new')); //Result: Array ( [0] => 111 [1] => 222 [2] => 333 [3] => 444 )
?>
28,sdiff
Description: Returns results that exist in the first set and do not exist in all other sets
Parameters: Keys : key1, key2, …, keyN: Any number of keys corresponding to sets in redis.
Return value: Return array on success, false on failure
Example:
$redis = new redis() ;
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
$redis->sadd("test","111");
$ redis->sadd("test","222");
$redis->sadd("test","333");
$redis->sadd("test1","111");
$redis->sadd("test1","444");
print_r($redis->sdiff("test","test1")); //Result: Array ( [0] => 222 [ 1] => 333 )
?>
29,sdiffstore
Description: Execute the sdiff command and store the results in the newly created variable.
Parameters:
Key: dstkey, the key to store the diff into.
Keys: key1, key2, … , keyN: Any number of keys corresponding to sets in redis
Return value: Return a number on success, false on failure
Example:
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
$redis ->sadd("test","111");
$redis->sadd("test","222");
$redis->sadd("test","333");
$ redis->sadd("test1","111");
$redis->sadd("test1","444");
var_dump($redis->sdiffstore('new',"test", "test1")); //Result: int(2)
print_r($redis->smembers('new')); //Result: Array ( [0] => 222 [1] => 333 )
?>
30,smembers, sgetmembers
Description:
Return the contents of the set
Parameters: Key: key
Return value: An array of elements, the contents of the set.
Example:
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
$redis-> ;sadd("test","111");
$redis->sadd("test","222");
print_r($redis->smembers('test')); //Result: Array ( [0] => 111 [1] => 222 )
?>

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
