Recommend this page to a friend! |
Classes of Duong Huynh Nghia | PHP Slim Framework 3 Modular Application | libraries/vendor/predis/predis/README.md | Download |
|
DownloadPredis[![Software license][ico-license]](LICENSE) [![Latest stable][ico-version-stable]][link-packagist] [![Latest development][ico-version-dev]][link-packagist] [![Monthly installs][ico-downloads-monthly]][link-downloads] [![Build status][ico-travis]][link-travis] [![HHVM support][ico-hhvm]][link-hhvm] [![Gitter room][ico-gitter]][link-gitter] Flexible and feature-complete Redis client for PHP >= 5.3 and HHVM >= 2.3.0. Predis does not require any additional C extension by default, but it can be optionally paired with phpiredis to lower the overhead of the serialization and parsing of the Redis RESP Protocol. For an __experimental__ asynchronous implementation of the client you can refer to Predis\Async. More details about this project can be found on the frequently asked questions. Main features
How to _install_ and use PredisThis library can be found on Packagist for an easier management of projects dependencies using Composer or on our own PEAR channel for a more traditional installation using PEAR. Ultimately, compressed archives of each release are available on GitHub. Loading the libraryPredis relies on the autoloading features of PHP to load its files when needed and complies with the PSR-4 standard. Autoloading is handled automatically when dependencies are managed through Composer, but it is also possible to leverage its own autoloader in projects or scripts lacking any autoload facility:
It is also possible to create a phar archive directly
from the repository by launching the Connecting to RedisWhen creating a client instance without passing any connection parameter, Predis assumes
Connection parameters can be supplied either in the form of URI strings or named arrays. The latter is the preferred way to supply parameters, but URI strings can be useful when parameters are read from non-structured or partially-structured sources:
It is also possible to connect to local instances of Redis using UNIX domain sockets, in this case
the parameters must use the
The client can leverage TLS/SSL encryption to connect to secured remote Redis instances without the
need to configure an SSL proxy like stunnel. This can be useful when connecting to nodes running on
various cloud hosting providers. Encryption can be enabled with using the
The connection schemes The actual list of supported connection parameters can vary depending on each connection backend so it is recommended to refer to their specific documentation or implementation for details. When an array of connection parameters is provided, Predis automatically works in cluster mode using client-side sharding. Both named arrays and URI strings can be mixed when providing configurations for each node:
See the aggregate connections section of this document for more details. Connections to Redis are lazy meaning that the client connects to a server only if and when needed.
While it is recommended to let the client do its own stuff under the hood, there may be times when
it is still desired to have control of when the connection is opened or closed: this can easily be
achieved by invoking Client configurationMany aspects and behaviors of the client can be configured by passing specific client options to the
second argument of
Options are managed using a mini DI-alike container and their values can be lazily initialized only when needed. The client options supported by default in Predis are: - Users can also provide custom options with values or callable objects (for lazy initialization) that are stored in the options container for later use through the library. Aggregate connectionsAggregate connections are the foundation upon which Predis implements clustering and replication and they are used to group multiple connections to single Redis nodes and hide the specific logic needed to handle them properly depending on the context. Aggregate connections usually require an array of connection parameters when creating a new client instance. ClusterBy default, when no specific client options are set and an array of connection parameters is passed to the client's constructor, Predis configures itself to work in clustering mode using a traditional client-side sharding approach to create a cluster of independent nodes and distribute the keyspace among them. This approach needs some form of external health monitoring of nodes and requires manual operations to rebalance the keyspace when changing its configuration by adding or removing nodes:
Along with Redis 3.0, a new supervised and coordinated type of clustering was introduced in the form
of redis-cluster. This kind of approach uses a different
algorithm to distribute the keyspaces, with Redis nodes coordinating themselves by communicating via
a gossip protocol to handle health status, rebalancing, nodes discovery and request redirection. In
order to connect to a cluster managed by redis-cluster, the client requires a list of its nodes (not
necessarily complete since it will automatically discover new nodes if necessary) and the
ReplicationThe client can be configured to operate in a single master / multiple slaves setup to provide better service availability. When using replication, Predis recognizes read-only commands and sends them to a random slave in order to provide some sort of load-balancing and switches to the master as soon as it detects a command that performs any kind of operation that would end up modifying the keyspace or the value of a key. Instead of raising a connection error when a slave fails, the client attempts to fall back to a different slave among the ones provided in the configuration. The basic configuration needed to use the client in replication mode requires one Redis server to be
identified as the master (this can be done via connection parameters using the
The above configuration has a static list of servers and relies entirely on the client's logic, but
it is possible to rely on
If the master and slave nodes are configured to require an authentication from clients, a password
must be provided via the global
While Predis is able to distinguish commands performing write and read-only operations,
The Command pipelinesPipelining can help with performances when many commands need to be sent to a server by reducing the latency introduced by network round-trip timings. Pipelining also works with aggregate connections. The client can execute the pipeline inside a callable block or return a pipeline instance with the ability to chain commands thanks to its fluent interface:
TransactionsThe client provides an abstraction for Redis transactions based on
This abstraction can perform check-and-set operations thanks to Adding new commandsWhile we try to update Predis to stay up to date with all the commands available in Redis, you might prefer to stick with an old version of the library or provide a different way to filter arguments or parse responses for specific commands. To achieve that, Predis provides the ability to implement new command classes to define or override commands in the default server profiles used by the client:
There is also a method to send raw commands without filtering their arguments or parsing responses. Users must provide the list of arguments for the command as an array, following the signatures as defined by the Redis documentation for commands:
Script commandsWhile it is possible to leverage Lua scripting on Redis 2.6+ using
directly
Customizable connection backendsPredis can use different connection backends to connect to Redis. Two of them leverage a third party
extension such as phpiredis resulting in major performance gains
especially when dealing with big multibulk responses. While one is based on PHP streams, the other
is based on socket resources provided by
Developers can create their own connection classes to support whole new network backends, extend
existing classes or provide completely different implementations. Connection classes must implement
For a more in-depth insight on how to create new connection backends you can refer to the actual
implementation of the standard connection classes available in the DevelopmentReporting bugs and contributing codeContributions to Predis are highly appreciated either in the form of pull requests for new features, bug fixes, or just bug reports. We only ask you to adhere to a basic set of rules before submitting your changes or filing bugs on the issue tracker to make it easier for everyone to stay consistent while working on the project. Test suite__ATTENTION__: Do not ever run the test suite shipped with Predis against instances of Redis running in production environments or containing data you are interested in! Predis has a comprehensive test suite covering every aspect of the library. This test suite performs
integration tests against a running instance of Redis (>= 2.4.0 is required) to verify the correct
behavior of the implementation of each command and automatically skips commands not defined in the
specified Redis profile. If you do not have Redis up and running, integration tests can be disabled.
By default the test suite is configured to execute integration tests using the profile for Redis 3.2
(which is the current stable version of Redis) but can optionally target a Redis instance built from
the Predis uses Travis CI for continuous integration and the history for past and current builds can be found on its project page. OtherProject related linksAuthorLicenseThe code for Predis is distributed under the terms of the MIT license (see LICENSE). [ico-license]: https://img.shields.io/github/license/nrk/predis.svg?style=flat-square [ico-version-stable]: https://img.shields.io/packagist/v/predis/predis.svg?style=flat-square [ico-version-dev]: https://img.shields.io/packagist/vpre/predis/predis.svg?style=flat-square [ico-downloads-monthly]: https://img.shields.io/packagist/dm/predis/predis.svg?style=flat-square [ico-travis]: https://img.shields.io/travis/nrk/predis.svg?style=flat-square [ico-hhvm]: https://img.shields.io/hhvm/predis/predis.svg?style=flat-square [ico-gitter]: https://img.shields.io/gitter/room/nrk/predis.svg?style=flat-square [link-packagist]: https://packagist.org/packages/predis/predis [link-travis]: https://travis-ci.org/nrk/predis [link-downloads]: https://packagist.org/packages/predis/predis/stats [link-hhvm]: http://hhvm.h4cc.de/package/predis/predis [link-gitter]: https://gitter.im/nrk/predis |