client.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. The `Predis\\Client` class
  2. --------------------------
  3. .. php:namespace:: Predis
  4. The `Client` class is the main class users interact with in Predis. The first
  5. thing you'll do to start communicating with Redis is instantiate a `Client`.
  6. Constructing a Client
  7. =====================
  8. The `Client` constructor takes two arguments. The first (``$parameters``) is a
  9. set of information about the Redis connection you'd like to make. The second
  10. (``$options``) is a set of options to configure the client.
  11. .. php:class:: Client
  12. .. php:method:: __construct($parameters = null, $options = null)
  13. Creates a new `Client` instance.
  14. :param $parameters: Connection parameters
  15. :param $options: Client options
  16. Connection Parameters
  17. '''''''''''''''''''''
  18. These parameters are used to control the behaviour of the underlying connection
  19. to Redis. They can be specified using a URI string::
  20. $client = new Predis\Client('tcp://127.0.0.1:6379?database=2')
  21. Or, using an associative array::
  22. $client = new Predis\Client(array(
  23. 'scheme' => 'tcp',
  24. 'host' => '127.0.0.1',
  25. 'port' => 6379,
  26. 'database' => 3
  27. ));
  28. By default, Predis supports a lengthy list of connection parameters.
  29. .. note::
  30. Since the client can use different :doc:`connection backends`, actual support
  31. for certain parameters depends on the value of ``scheme`` and the connection
  32. backends registered using the ``connections`` client option.
  33. ================== ============================================= ========= =========================================
  34. parameter description default supported by
  35. ================== ============================================= ========= =========================================
  36. scheme Instructs the client how to connect to Redis. tcp `Predis\\Connection\\StreamConnection`
  37. Supported values are: ``tcp``, ``unix``, `Predis\\Connection\\PhpiredisConnection`
  38. ``http``. Certain values such as ``http`` `Predis\\Connection\\WebdisConnection`
  39. change the underlying connection backend.
  40. ------------------ --------------------------------------------- --------- -----------------------------------------
  41. host IP address or hostname of the server. 127.0.0.1 `Predis\\Connection\\StreamConnection`
  42. Ignored when using the ``unix`` scheme. `Predis\\Connection\\PhpiredisConnection`
  43. `Predis\\Connection\\WebdisConnection`
  44. ------------------ --------------------------------------------- --------- -----------------------------------------
  45. port TCP port the server listens on. 6379 `Predis\\Connection\\StreamConnection`
  46. Ignored when using the ``unix`` scheme. `Predis\\Connection\\PhpiredisConnection`
  47. `Predis\\Connection\\WebdisConnection`
  48. ------------------ --------------------------------------------- --------- -----------------------------------------
  49. path Path of the UNIX domain socket the server is not set `Predis\\Connection\\StreamConnection`
  50. listening on, used only in combination with `Predis\\Connection\\PhpiredisConnection`
  51. the ``unix`` scheme.
  52. Example: ``/tmp/redis.sock``.
  53. ------------------ --------------------------------------------- --------- -----------------------------------------
  54. database Redis database to select when connecting. not set `Predis\\Connection\\StreamConnection`
  55. Its effect is the same of using `SELECT`_. `Predis\\Connection\\PhpiredisConnection`
  56. ------------------ --------------------------------------------- --------- -----------------------------------------
  57. timeout Timeout to perform the connection to Redis. 5.0 `Predis\\Connection\\StreamConnection`
  58. Its value is expressed in seconds as a float `Predis\\Connection\\PhpiredisConnection`
  59. allowing sub-second resolution. `Predis\\Connection\\WebdisConnection`
  60. ------------------ --------------------------------------------- --------- -----------------------------------------
  61. read_write_timeout Timeout for read and write operations. platform `Predis\\Connection\\StreamConnection`
  62. Its value is expressed in seconds as a float dependent `Predis\\Connection\\PhpiredisConnection`
  63. allowing sub-second resolution.
  64. ------------------ --------------------------------------------- --------- -----------------------------------------
  65. async_connect Tells the client to perform the connection not set `Predis\\Connection\\StreamConnection`
  66. asynchronously without waiting for it to be (false)
  67. fully estabilished.
  68. ------------------ --------------------------------------------- --------- -----------------------------------------
  69. persistent The underlying socket is left intact after a not set `Predis\\Connection\\StreamConnection`
  70. GC collection or when the script terminates (false)
  71. (only when using FastCGI or php-fpm).
  72. ------------------ --------------------------------------------- --------- -----------------------------------------
  73. iterable_multibulk `Multi-bulk replies`_ are returned as PHP false `Predis\\Connection\\StreamConnection`
  74. iterable objects, making them streamable.
  75. ------------------ --------------------------------------------- --------- -----------------------------------------
  76. alias String used to identify a connection by name. not set Backend independent.
  77. This is useful with :doc:`clustering` and
  78. :doc:`replication`.
  79. ------------------ --------------------------------------------- --------- -----------------------------------------
  80. weight This is only used with :doc:`clustering` and not set Backend independent.
  81. determines the proportion of the load the
  82. corresponding server will bear relative to
  83. other nodes in the cluster.
  84. ------------------ --------------------------------------------- --------- -----------------------------------------
  85. user Username for HTTP authentication (`Webdis`_). not set `Predis\\Connection\\WebdisConnection`
  86. ------------------ --------------------------------------------- --------- -----------------------------------------
  87. pass Password for HTTP authentication (`Webdis`_). not set `Predis\\Connection\\WebdisConnection`
  88. ================== ============================================= ========= =========================================
  89. .. _SELECT: http://redis.io/commands/select
  90. .. _Multi-bulk replies: http://redis.io/topics/protocol#multi-bulk-reply
  91. .. _Webdis: http://webd.is/
  92. Users can also specify their own parameters, they will simply be ignored by the
  93. client but can be used later to pass additional information for custom purposes.
  94. Client Options
  95. ''''''''''''''
  96. Several behaviours of `Client` can be controlled via client options with values
  97. that vary depending on the nature of each option: some of them accept primitive
  98. types while others can also take instances of classes implementing some specific
  99. interfaces defined by Predis, which can be useful to completely override the
  100. standard ones used by `Client`::
  101. $client = new Predis\Client($parameters, array(
  102. 'prefix' => 'predis:'
  103. 'profile' => '2.6',
  104. 'connections' => array(
  105. 'tcp' => 'Predis\Connection\PhpiredisConnection',
  106. 'unix' => 'Predis\Connection\PhpiredisConnection',
  107. ),
  108. ));
  109. To achieve an even higher level of customizability, certain options also accept
  110. callables acting as initializers that can be leveraged to gain full control over
  111. the initialization of option values (e.g. instances of classes) before returning
  112. them to `Client`::
  113. $client = new Predis\Client('tcp://127.0.0.1', array(
  114. 'prefix' => 'predis:',
  115. 'profile' => function ($options, $option) {
  116. // Callable initializers have access to the whole set of options
  117. // (1st argument) and to the current option instance (2nd argument).
  118. return new Predis\Profile\ServerVersion26();
  119. },
  120. ));
  121. Users can also specify their own custom options to pass additional information.
  122. Just like standard options, they are accessible from callable initializers::
  123. $client = new Predis\Client('tcp://127.0.0.1', array(
  124. // 'commands' is a custom option, actually unknown to Predis.
  125. 'commands' => array(
  126. 'set' => Predis\Command\StringSet,
  127. 'get' => Predis\Command\StringGet,
  128. ),
  129. 'profile' => function ($options, $option) {
  130. $profile = $option->getDefault($options);
  131. if (is_array($options->commands)) {
  132. foreach ($options->commands as $command => $class) {
  133. $profile->defineCommand($command, $class);
  134. }
  135. }
  136. return $profile
  137. },
  138. ));
  139. This is the full list of client options supported by `Client`:
  140. ============== ====================================================== ================================================
  141. option description default
  142. ============== ====================================================== ================================================
  143. exceptions Changes how `Client` treats `error replies`_: true
  144. - when ``true``, it throws `Predis\\ServerException`.
  145. - when ``false``, it returns `Predis\\ResponseError`.
  146. -------------- ------------------------------------------------------ ------------------------------------------------
  147. prefix When set, the passed string is transparently applied not set
  148. as a prefix to each key present in command arguments.
  149. .. note::
  150. Keys are prefixed using rules defined by each
  151. command in order to be able to support even complex
  152. cases such as `SORT`_, `EVAL`_ and `EVALSHA`_.
  153. -------------- ------------------------------------------------------ ------------------------------------------------
  154. profile Changes the Redis version `Client` is expected to 2.6
  155. connect to, among a list of :doc:`server profiles`
  156. predefined by Predis. Supported versions are: ``1.2``,
  157. ``2.0``, ``2.2``, ``2.4``, ``2.6``, ``dev`` (unstable
  158. branch in the Redis repository).
  159. This option accepts also the fully-qualified name of
  160. a `Predis\\Profile\\ServerProfileInterface`
  161. or its instance passed either directly or returned by
  162. a callable initializer.
  163. -------------- ------------------------------------------------------ ------------------------------------------------
  164. connections Overrides :doc:`connection backends` by scheme using - tcp: `Predis\\Connection\\StreamConnection`
  165. a named array, with keys being the connection schemes - unix: `Predis\\Connection\\StreamConnection`
  166. subject to change and values being the fully-qualified - http: `Predis\\Connection\\WebdisConnection`
  167. name of classes implementing
  168. `Predis\\Connection\\SingleConnectionInterface`.
  169. This option accepts also the fully-qualified name of
  170. a `Predis\\Connection\\ConnectionFactoryInterface`
  171. or its instance passed either directly or returned by
  172. a callable initializer.
  173. -------------- ------------------------------------------------------ ------------------------------------------------
  174. cluster Changes how `Client` handles :doc:`clustering`: predis
  175. - ``predis`` indicates the use of client-side
  176. sharding.
  177. - ``redis`` indicates the use `redis cluster`_.
  178. This option accepts also the fully-qualified name of
  179. a `Predis\\Connection\\ClusterConnectionInterface`
  180. or its instance passed either directly or returned by
  181. a callable initializer.
  182. -------------- ------------------------------------------------------ ------------------------------------------------
  183. replication When ``true``, the array of connection parameters is not set
  184. used in a master and slaves :doc:`replication` setup
  185. instead of treating the servers as a cluster of nodes.
  186. This option accepts also the fully-qualified name of
  187. a `Predis\\Connection\\ReplicationConnectionInterface`
  188. or its instance passed either directly or returned by
  189. a callable initializer.
  190. ============== ====================================================== ================================================
  191. .. _error replies: http://redis.io/topics/protocol#status-reply
  192. .. _redis cluster: http://redis.io/topics/cluster-spec
  193. .. _SORT: http://redis.io/commands/eval
  194. .. _EVAL: http://redis.io/commands/eval
  195. .. _EVALSHA: http://redis.io/commands/evalsha