Redis's PHP client supports a wide variety of clients, including C, Java, PHP, and Python. This article mainly introduces the installation and use of PHP clients.
The Redis client is actually responsible for the communication process with the Redis Server through the network protocol. the communication process must follow the protocol specifications, so that the customer's call is more in line with the usage habits of specific languages. Existing PHP clients: rediska, phpredis, Predis, and Redisent. Among these benefits, there are pure PHP implementation solutions and binary implementation solutions.
The official PHP client list is as follows:
Feature comparison
|
Predis |
Phpredis |
Rediska |
Easy to expand |
? |
|
? |
Client Policy and Hash algorithm definition |
? |
|
? |
Implementation method |
Pure PHP |
PHP extension (C language) |
Pure PHP |
Test data (in local environment) |
18900 SET/sec using 12 bytes for both key and value 18300 GET/sec while retrieving the very same values 0.210 seconds to fetch 30000 keys using _ KEYS *_. |
29000 SET/sec using 12 bytes for both key and value 30000 GET/sec while retrieving the very same values 0.037 seconds to fetch 30000 keys using "KEYS *"". |
|
Test result (in network environment) |
3150 SET/sec using 12 bytes for both key and 3150 GET/sec while retrieving the very same 0.212 seconds to fetch 30000 keys using "KEYS *". |
3300 SET/sec using 12 bytes for both key and 3300 GET/sec while retrieving the very same 0.088 seconds to fetch 30000 keys using "KEYS *". |
|
|
|
|
|
/* ---------------------- Predis ----------------------------*/
Predis
Predis is a PHP client that supports Redis with flexibility and complete features (PHP> 5.3. The current version is 0.6.3. PHP5.2 is not supported by default.
The main features are as follows:
Fully supports Redis from 1.2 to 2.4, and supports the currently developing version;
Provides consistent hash algorithms implemented by clients and supports customization;
Supports Command pipelines in a single or aggregate connection. (Command pipelining on single and aggregated connections)
You can use TCP/IP or Unix domain sockets to connect to redis and support persistent connections;
Automatically connects to the apsaradb for Redis instance. the connection is executed only when the first command is sent;
A set of client commands can be flexibly defined;
Predis installation
/* ----------------------- Phpredis ---------------------------*/
Phpredis
This is a binary PHP client, which is more efficient than Predis. This version supports Session Handler. This extension is easy to use without loading any external files. The disadvantage is that it is difficult to expand. generally, PHP programmers cannot extend it. Considering that Redis is developing rapidly and the lack of extended features still has some impact, you need to upgrade and update during the maintenance process.
Install XAMPP under Max (XAMPP Dev environment is required)
Sudo/Applications/XAMPP/xamppfiles/bin/phpize
Sudo MACOSX_DEPLOYMENT_TARGET = 10.6 CFLAGS = "-arch i386-arch x86_64-g-OS-pipe-no-cpp-precomp" CCFLAGS = "-arch i386-arch x86_64-g-OS- pipe "CXXFLAGS ="-arch i386-arch x86_64-g-OS-pipe "LDFLAGS ="-arch i386-arch x86_64-bind_at_load ". /configure -- with-apxs =/Applications/XAMPP/xamppfiles/bin/apxs -- with-php-config =/Applications/XAMPP/xamppfiles/bin/php-config
Sudo make
Sudo make install
Modify php. ini and restart the service.
Sample code
Echo 'phpredis sample:
';
Error_reporting (E_ALL );
Ini_set ('display _ errors ', 'on ');
$ Redis = new Redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> set ('First _ key_phpredis ', 'Hello World ');
);
/* ------------------- Rediska -------------------------------*/
Rediska
Rediska is currently in beta stage. His code is hosted on github for easy access. The PEAR version is also provided, so it is very convenient to obtain and install it. You can use the PEAR package for installation.
This article uses the PEAR provided by XAMPP on Mac for installation. First, add the channel address:
Localhost: bin shiqiang $ sudo./pear channel-discover pear.geometria-lab.net
Password:
Adding Channel "pear.geometria-lab.net" succeeded
Discovery of channel "pear.geometria-lab.net" succeeded
Then install
Localhost: bin shiqiang $ sudo./pear channel-discover pear.geometria-lab.net
Password:
Adding Channel "pear.geometria-lab.net" succeeded
Discovery of channel "pear.geometria-lab.net" succeeded
Localhost: bin shiqiang $ sudo./pear install geometria-lab/Rediska-beta
Unknown remote channel: pear.zfcampus.org
Did not download optional dependencies: channel: // pear.zfcampus.org/zf, use -- alldeps to download automatically
Geometria-lab/Rediska can optionally use package "channel: // pear.zfcampus.org/zf" (recommended version 1)
Downloading Rediska-0.5.6.tgz...
Starting to download Rediska-0.5.6.tgz (116,203 bytes)
................ Done: 116,203 bytes
Install OK: channel: // pear.geometria-lab.net/Rediska-0.5.6
After installation, you can see that the Rediska PEAR package already exists in the local Directory environment.
Localhost: XAMPP shiqiang $ sudo find.-name 'redis *'
./Xamppfiles/lib/php/pear/data/Rediska
./Xamppfiles/lib/php/pear/doc/Rediska
./Xamppfiles/lib/php/pear/Rediska
./Xamppfiles/lib/php/pear/Rediska/Options/RediskaInstance. php
./Xamppfiles/lib/php/pear/Rediska/Zend/Application/Resource/Rediska. php
./Xamppfiles/lib/php/pear/Rediska/Zend/Auth/Adapter/Redis. php
./Xamppfiles/lib/php/pear/Rediska/Zend/Cache/Backend/Redis. php
./Xamppfiles/lib/php/pear/Rediska/Zend/Log/Writer/Redis. php
./Xamppfiles/lib/php/pear/Rediska/Zend/Queue/Adapter/Redis. php
./Xamppfiles/lib/php/pear/Rediska/Zend/Session/SaveHandler/Redis. php
./Xamppfiles/lib/php/pear/Rediska. php
./Xamppfiles/lib/php/pear/test/Rediska
./Xamppfiles/lib/php/pear/test/Rediska/tests/library/Rediska
./Xamppfiles/lib/php/pear/test/Rediska/tests/library/Rediska/Options/RediskaInstanceTest. php
./Xamppfiles/lib/php/pear/test/Rediska/tests/library/RediskaTest. php
./Xamppfiles/temp/pear/download/Rediska-0.5.6
./Xamppfiles/temp/pear/download/Rediska-0.5.6/library/Rediska
./Xamppfiles/temp/pear/download/Rediska-0.5.6/library/Rediska/Options/RediskaInstance. php
./Xamppfiles/temp/pear/download/Rediska-0.5.6/library/Rediska/Zend/Application/Resource/Rediska. php
./Xamppfiles/temp/pear/download/Rediska-0.5.6/library/Rediska/Zend/Auth/Adapter/Redis. php
./Xamppfiles/temp/pear/download/Rediska-0.5.6/library/Rediska/Zend/Cache/Backend/Redis. php
./Xamppfiles/temp/pear/download/Rediska-0.5.6/library/Rediska/Zend/Log/Writer/Redis. php
./Xamppfiles/temp/pear/download/Rediska-0.5.6/library/Rediska/Zend/Queue/Adapter/Redis. php
./Xamppfiles/temp/pear/download/Rediska-0.5.6/library/Rediska/Zend/Session/SaveHandler/Redis. php
./Xamppfiles/temp/pear/download/Rediska-0.5.6/library/Rediska. php
./Xamppfiles/temp/pear/download/Rediska-0.5.6/tests/library/Rediska
./Xamppfiles/temp/pear/download/Rediska-0.5.6/tests/library/Rediska/Options/RediskaInstanceTest. php
./Xamppfiles/temp/pear/download/Rediska-0.5.6/tests/library/RediskaTest. php
./Xamppfiles/temp/pear/download/Rediska-0.5.6.tgz
Simple example
Error_reporting (E_ALL );
Ini_set ('display _ errors ', 'on ');
$ Options = array (
'Namespace' => 'application _',
'Servers' => array (
Array ('host' => '127. 0.0.1 ', 'port' => 127 ),
Array ('host' => '127. 0.0.1 ', 'port' => 127)
)
);
Require_once 'rediska. php ';
$ Rediska = new Rediska ($ options); // Create a Rediska object and connect to the server
$ Key = new Rediska_Key ('First _ key ');
$ Key-> setValue ('First _ value ');
$ Get_value = new Rediska_Key ('First _ key ');
Print $ get_value; // retrieves the stored value
Differences from Memcache
1. diversified data types are supported. Data types such as string, list, set, ordered set, and Hash array are supported.
2. you can flexibly specify the server on which the key is stored.
References:
1. http://rediska.geometria-lab.net/
2. Reids Which PHP Module to use
3. PHP Redis
4. worship Redis
5. Comparison between redis client predis and phpredis