This article is the content of the Windows system PHP using Redis, now share to everyone, the need for friends can also refer to
Reference Link: https://www.cnblogs.com/lhat/p/6402472.html
Environment: Windows 10 64-bit operating system
PHP 5.4
Redis 3.0
1.redis Download, install
At first it was using https://github.com/MicrosoftArchive/redis/releases to download Redis under Windows, but the speed was too slow (no ladder is useless). Later, I found the following download address on GitHub:
Https://github.com/ServiceStack/redis-windows/raw/master/downloads/redis-latest.zip
Windows run (shortcut key: Windows key +r), enter "cmd" command, enter the DOC operating system window;
Accessing the Redis installation directory using commands
Redis-server redis. Windows. conf
Start Redis Service, this window does not need to close, close the window to stop the Redis service, start successfully such as:
Open a new Doc window, go to the installation directory to execute the client startup command "REDIS-CLI" or simply double-click Redis-cli.exe to test the command, create and get the name value for TAO data:
2. Download the Phpredis extension
Execute the Phpinfo () function and select the corresponding compression package according to "NTS" and "VCn" below, https://github.com/phpredis/phpredis/downloads. Also note that the PHP version should correspond well.
2. PHP Configuration Installation Extension
First, put the Php_igbinary.dll and Php_redis.dll files in the ZIP package into the ext directory of the PHP installation directory.
Then add the following configuration in php.ini
Extension=php_igbinary.dllextension=php_redis.dll
3. Restart Apache, execute the phpinfo () function, and you will find that the Redis extension is more.
4. Turn on Redis service, test
$redis = new Redis (); Connect Redis server $redis->connect (' 127.0.0.1 ', ' 6379 '); echo "Connection to server sucessfully <br/>"; Check to see if the service is running echo "Server is running:". $redis->ping ();
As a result, connecting to a Redis server succeeds
Connection to server sucessfully server is running: +pong
At this point, we can make use of Redis in PHP.
1 $redis = new Redis (); 2//Connect Redis server 3 $redis->connect (' 127.0.0.1 ', ' 6379 '); 4 5 6 $key = "key"; 7 $val = "Val"; 8 9//redis key Operation $redis->exists ($key); Determine if the key value exists one by one $redis->expire ($key, 10); Set key expires in 10 seconds//redis string string $redis->set ($key, $val); $redis->incr ($key); A key value of +1, except that Val is an integer, otherwise the function fails to execute $redis->decr ($key); Key value-1, ibid. $redis->append ($key, "UE"); Append key value content $redis->strlen ($key); Returns the length of the key value 19 20//When the key value is set for the first time, the data type of the key value cannot be changed. $redis->del ($key); Delete key value//redis hash hash $redis->hset ($key, ' field1 ', ' val1 '); Set a Key-value key value of $redis->hmset ($key, Array (' field2 ' = ' val2 ', ' field3 ' = ' val3 ')); Set multiple K-v key values to $redis->hget ($key, ' field2 '); Get a hash of one of the key values in $redis->hmget ($key, Array (' Field2 ', ' field1 ')); Get multiple key values for hash $redis->hgetall ($key); Gets all the key-value pairs in the hash 29 $redis->hlen ($key); Gets the number of key-value pairs in the hash $redis->hkeys ($key); Get all the keys in the hash $redis->hvals ($key); Get all the values in the hash $redis->del ($key); Delete key value//redis List $index = $start = 0;36 $redis->lpush ($key, ' val1 ', ' val2 '); Add multiple values at the beginning of the list $redis->lpop ($key); Remove and get the first element of list $redis->rpop ($key); Remove and get the last element of list $stop = $redis->llen ($key)-1; Gets the length of list $redis->lindex ($key, $index); Get list elements by index $redis->lrange ($key, $start, $stop); Gets the element within the specified range $redis->del ($key);//redis set unordered set $redis->sadd ($key, ' val1 ', ' val2 '); Add multiple elements to the collection $redis->scard ($key); Gets the number of collection elements $redis->spop ($key); Remove and get a random element within the collection $redis->srem ($key, ' val1 ', ' val2 '); Removes multiple elements from a collection $redis->sismember ($key, ' val1 '); Determines whether an element exists in the set $redis->del ($key);//redis Sorted set ordered set 54//The elements in an ordered set are associated with a fractional score, depending on the score score the element Sort 55 $redis->zadd ($key, $score 1, $val 1, $score 2, $val 2); Add multiple elements to the collection $redis->zcard ($key); Gets the total number of elements in the collection $redis->zcount ($key, $minScore, $maxScore); Gets the elements within the category range of the collection. $redis->zrem ($key, $member 1, $member 2); Remove multiple elements within a collection
Related recommendations:
PHP using Redis instance to explain
Windows under redis+php use
Example of installing Redis extensions in PHP under Windows