Redis's PHP Client

Source: Internet
Author: User
Tags pear php redis redis server
Redis supports a wide variety of clients, including C, Java, PHP, Python, etc., this article mainly introduces the installation and use of PHP client.


Redis's clients actually assume the process of communicating with Redis server over a network protocol, and the process of communication must follow the protocol specification to make the customer's call more consistent with the language-specific usage habits. Existing PHP client: Rediska, Phpredis, Predis, Redisent. These can be justifying, there is a pure PHP implementation, there is a binary version of the implementation of the scheme.


A list of the official PHP clients is as follows:



Feature comparison



Predis

Phpredis

Rediska

Easy to expand

?


?

Client policy and hash algorithm definitions

?


?

Implementation method

Pure PHP

PHP extensions (C language)

Pure PHP

Test data (Local environment)

18900 set/sec using 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 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 results (under network environment)

3150 set/sec using 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 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 flexible and feature-complete (php>5.3), Redis-enabled PHP client. The current version is 0.6.3, PHP5.2 is not supported by default.

The main features are as follows:

Complete support for Redis from 1.2 to 2.4, and support the currently developing version;

Provides a consistent hashing algorithm for client implementations, supporting customization;

Support for command pipelines in a single or aggregated connection; (command pipelining on aggregated connections)

Ability to connect to Redis via TCP/IP or UNIX domain sockets, supporting persistent connections;

Automatically connect to Redis instances, using lazy mode, and only perform the connection when the first command is issued;

Can flexibly define the client's command set;


Predis Installation


/*-----------------------Phpredis---------------------------*/


Phpredis


This is a binary version of the PHP client, according to the argument, the efficiency is higher than the Predis. This version supports the handler as a session. This extension is a bit more convenient to use without having to load any external files. The disadvantage is that it is difficult to extend, and general PHP programmers cannot extend it. In view of the rapid development of Redis, the lack of extended features still have some impact, need to pay attention to the maintenance process updates.


Installation of integrated XAMPP under MAX (XAMPP Dev environment 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 "./co Nfigure--with-apxs=/applications/xampp/xamppfiles/bin/apxs--with-php-config=/applications/xampp/xamppfiles/bin /php-config

sudo make

sudo make install


Then modify the 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 (' 127.0.0.1 ', 6379);

$redis->set (' First_key_phpredis ', ' Hello World ');


);


/*-------------------Rediska-------------------------------*/


Rediska


Rediska is still in the beta phase. His code is hosted on GitHub and can be easily retrieved. The pear version is also available, so it is easy to get and install. You can use the Pear package to install.


This article uses the pear provided by XAMPP under 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 the

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 the installation is complete, you can see that the local directory environment already has the Rediska pear package

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


Examples of simple use



Error_reporting (E_all);

Ini_set (' display_errors ', ' on ');


$options = Array (

' Namespace ' = ' application_ ',

' Servers ' = Array (

Array (' host ' = ' 127.0.0.1 ', ' port ' = 6379),

Array (' host ' = ' 127.0.0.1 ', ' port ' = 6380)

)

);


Require_once ' rediska.php ';

$rediska = new Rediska ($options);//Create Rediska object, connect to server


$key = new Rediska_key (' First_key ');

$key->setvalue (' First_value ');


$get _value = new Rediska_key (' First_key ');

Print $get _value;//retrieve the stored value


The difference from Memcache

1. The supported data types are diversified. Supports data types such as strings, lists, collections, ordered collections, and hash arrays.

2, you can flexibly specify which server the key is stored on.


Resources:

1, http://rediska.geometria-lab.net/

2. Reids which PHP Module to use

3. PHP Redis

4. Worship Redis

5, Redis client Predis and Phpredis comparison

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.