Redis client predis Introduction

Source: Internet
Author: User
Tags autoloader redis server value store

Readme. markdown official introduction

 

# Predis #

# About ##

Predis is a flexible and feature-complete PHP (> = 5.3) client library for the redis key-Value Store.

For a list of frequently asked questions about predis, see the _ FAQ _ file in the root of the repository.
For a version compatible with PHP 5.2 you must use the backported version from the latest release in the 0.6.x series.

# Main features ##

-Full support for redis 1.2, 2.0 and 2.2. Different Versions of redis are supported via server profiles.
-Client-side sharding with support for consistent hashing and custom distribution strategies.
-Command pipelining on single and aggregated connections.
-Transaction action for redis transactions (redis> = 2.0) with support for CAS operations (redis> = 2.2 ).
-Lazy connections to redis instances are automatically estabilished upon the first call to a command.
-Ability to connect to redis using TCP/IP or Unix domain sockets by default.
-Flexible system to define and register your own set of commands to a client instance.

# Quick examples ##

See the [Official wiki] (http://wiki.github.com/nrk/predis) of the project for a more
Complete coverage of all the features available in predis.

### Loading predis

Predis relies on the autoloading features of PHP and complies with
[PSR-0 standard] (http://groups.google.com/group/php-standards/web/psr-0-final-proposal)
For interoperability with most of the major frameworks and libraries.
When used in simple projects or scripts you might need to define an autoloader function:

Spl_autoload_register (function ($ class ){
$ File = predis_base_path. strtr ($ class, '//', '/'). '. php ';
If (file_exists ($ file )){
Require $ file;
Return true;
}
});

You can also create a single phar archive from the repository just by launching the _ createphar. php _
Script located in the _ bin _ directory. The generated phar ships with a stub that defines an autoloader
Function for predis, so you just need to require the phar archive in order to be able to use the library.

Alternatively you can generate a single PHP file that holds every class, just like older versions of predis,
Using the _ createsinglefile. php _ script located in the _ bin _ directory. In this way you can load predis in
Your scripts simply by using functions such as _ require _ and _ include _, but this practice is not encouraged.

### Connecting to a local instance of redis ###

You don't have to specify a TCP host and port when connecting to redis instances running on
Localhost on the default port:

$ Redis = new predis/client ();
$ Redis-> set ('library ', 'predis ');
$ Value = $ redis-> get ('library ');

You can also use an URI string or an array-based dictionary to specify the connection parameters:

$ Redis = new predis/client ('tcp: // 10.0.0.1: 8080 ');

// Is equivalent:

$ Redis = new predis/client (Array (
'Scheme '=> 'tcp ',
'Host' => '10. 0.0.1 ',
'Port' => 6379,
));

### Pipelining multiple commands to multiple instances of redis with client-side sharding ###

Pipelining helps with CES when there is the need to issue has commands to a server
In one go. Furthermore, pipelining works transparently even on aggregated connections. predis,
In fact, supports client-side sharding of data using consistent-hashing on keys and clustered
Connections are supported natively by the client class.

$ Redis = new predis/client (Array (
Array ('host' => '10. 0.0.1 ', 'Port' => 6379 ),
Array ('host' = & gt; '10. 0.0.2 ', 'Port' = & gt; 6379)
));

$ Replies = $ redis-> pipeline (function ($ pipe ){
For ($ I = 0; I I <1000; $ I ++ ){
$ Pipe-> set ("key: $ I", str_pad ($ I, 4, '0', 0 ));
$ Pipe-> get ("key: $ I ");
}
});

### Overriding standard connection classes with M ones ###

Predis allows developers to create new connection classes to add support for new protocols
Or override the existing ones to provide a different implementation compared to the default
Classes. This can be obtained by subclassing the predis/Network/iconnectionsingle interface.

Class myconnectionclass implements predis/Network/iconnectionsingle {
// Implementation goes here
}

// Let predis automatically use your own class to handle the default TCP Connection

Predis/connectionschemes: Define ('tcp ', 'myononclass ');

You can have a look at the predis/network namespace for some actual code that gives a better
Insight about how to create new connection classes.

### Definition and runtime registration of new commands on the client ###

Let's suppose redis just added the support for a brand new feature associated
With a new command. If you want to start using the above mentioned new feature
Right away without messing with predis source code or waiting for it to find
Its way into a stable predis release, then you can start off by creating a new
Class that matches the command type and its behaviour and then bind it to
Client instance at runtime. Actually, it is easier done than said:

Class brandnewrediscommand extends predis/commands/command {
Public Function GETID () {return 'newcmd ';}
}

$ Redis = new predis/client ();
$ Redis-> getprofile ()-> definecommand ('brandnewrediscommand', 'newcmd ');
$ Redis-> newcmd ();

# Development ##

Predis is fully backed up by a test suite which tries to cover all the aspects of
Client Library and the interaction of every single command with a redis server. If you
Want to work on predis, it is highly recommended that you first run the test suite
Be sure that everything is OK, and report strange behaviours or bugs.

When modifying predis please be sure that no warnings or notices are emitted by PHP
By running the interpreter in your development environment with the "error_reporting"
Variable set to e_all | e_strict.

The recommended way to contribute to predis is to fork the project on GitHub, create
New topic branches on your newly created repository to fix or add features and then
Open a new pull request with a description of the applied changes. obviusly, you can
Use any other Git hosting provider of your preference. Diff patches will be accepted
Too, even though they are not the preferred way to contriugh to predis.

# Dependencies ##

-Php> = 5.3.0
-Phpunit> = 3.5.0 (needed to run the test suite)

# Links ##

### Project ###
-[Source Code] (http://github.com/nrk/predis)
-[Wiki] (http://wiki.github.com/nrk/predis)
-[Issue tracker] (http://github.com/nrk/predis/issues)

### Related ###
-[Redis] (http://code.google.com/p/redis)
-[PHP] (http://php.net /)
-[Phpunit] (http://www.phpunit.de /)
-[Git] (http://git-scm.com /)

# Author ##

-[Daniele Alessandri] (mailto: suppakilla@gmail.com) ([Twitter] (http://twitter.com/JoL1hAHN ))

# Contributors ##

-[Lorenz Castelli] (http://github.com/lcastelli)
-[Jordi Boggiano] (http://github.com/Seldaek) ([Twitter] (http://twitter.com/seldaek ))
-[Sebastian waisbrot] (http://github.com/seppo0010) for his work on extending [phpiredis] (http://github.com/seppo0010/phpiredis) for predis

# License ##

The code for predis is distributed under the terms of the MIT license (see license ).

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.