client.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. password Password for accessing a password-protected not set `Predis\\Connection\\StreamConnection`
  58. Redis instance. Its effect is the same of `Predis\\Connection\\PhpiredisConnection`
  59. using `AUTH`_.
  60. ------------------ --------------------------------------------- --------- -----------------------------------------
  61. timeout Timeout to perform the connection to Redis. 5.0 `Predis\\Connection\\StreamConnection`
  62. Its value is expressed in seconds as a float `Predis\\Connection\\PhpiredisConnection`
  63. allowing sub-second resolution. `Predis\\Connection\\WebdisConnection`
  64. ------------------ --------------------------------------------- --------- -----------------------------------------
  65. read_write_timeout Timeout for read and write operations. platform `Predis\\Connection\\StreamConnection`
  66. Its value is expressed in seconds as a float dependent `Predis\\Connection\\PhpiredisConnection`
  67. allowing sub-second resolution.
  68. ------------------ --------------------------------------------- --------- -----------------------------------------
  69. async_connect Tells the client to perform the connection not set `Predis\\Connection\\StreamConnection`
  70. asynchronously without waiting for it to be (false)
  71. fully estabilished.
  72. ------------------ --------------------------------------------- --------- -----------------------------------------
  73. persistent The underlying socket is left intact after a not set `Predis\\Connection\\StreamConnection`
  74. GC collection or when the script terminates (false)
  75. (only when using FastCGI or php-fpm).
  76. ------------------ --------------------------------------------- --------- -----------------------------------------
  77. iterable_multibulk `Multi-bulk replies`_ are returned as PHP false `Predis\\Connection\\StreamConnection`
  78. iterable objects, making them streamable.
  79. ------------------ --------------------------------------------- --------- -----------------------------------------
  80. alias String used to identify a connection by name. not set Backend independent.
  81. This is useful with :doc:`clustering` and
  82. :doc:`replication`.
  83. ------------------ --------------------------------------------- --------- -----------------------------------------
  84. weight This is only used with :doc:`clustering` and not set Backend independent.
  85. determines the proportion of the load the
  86. corresponding server will bear relative to
  87. other nodes in the cluster.
  88. ------------------ --------------------------------------------- --------- -----------------------------------------
  89. user Username for HTTP authentication (`Webdis`_). not set `Predis\\Connection\\WebdisConnection`
  90. ------------------ --------------------------------------------- --------- -----------------------------------------
  91. pass Password for HTTP authentication (`Webdis`_). not set `Predis\\Connection\\WebdisConnection`
  92. ================== ============================================= ========= =========================================
  93. .. _SELECT: http://redis.io/commands/select
  94. .. _AUTH: http://redis.io/commands/auth
  95. .. _Multi-bulk replies: http://redis.io/topics/protocol#multi-bulk-reply
  96. .. _Webdis: http://webd.is/
  97. Users can also specify their own parameters, they will simply be ignored by the
  98. client but can be used later to pass additional information for custom purposes.
  99. Client Options
  100. ''''''''''''''
  101. Several behaviours of `Client` can be controlled via client options with values
  102. that vary depending on the nature of each option: some of them accept primitive
  103. types while others can also take instances of classes implementing some specific
  104. interfaces defined by Predis, which can be useful to completely override the
  105. standard ones used by `Client`::
  106. $client = new Predis\Client($parameters, array(
  107. 'prefix' => 'predis:'
  108. 'profile' => '2.6',
  109. 'connections' => array(
  110. 'tcp' => 'Predis\Connection\PhpiredisConnection',
  111. 'unix' => 'Predis\Connection\PhpiredisConnection',
  112. ),
  113. ));
  114. To achieve an even higher level of customizability, certain options also accept
  115. callables acting as initializers that can be leveraged to gain full control over
  116. the initialization of option values (e.g. instances of classes) before returning
  117. them to `Client`::
  118. $client = new Predis\Client('tcp://127.0.0.1', array(
  119. 'prefix' => 'predis:',
  120. 'profile' => function ($options, $option) {
  121. // Callable initializers have access to the whole set of options
  122. // (1st argument) and to the current option instance (2nd argument).
  123. return new Predis\Profile\ServerVersion26();
  124. },
  125. ));
  126. .. note::
  127. When using callables, the configuration of the returned value must be fully
  128. handled by the user's code since Predis will not attempt to use its standard
  129. configuration path for that option.
  130. Users can also specify their own custom options to pass additional information.
  131. Just like standard options, they are accessible from callable initializers::
  132. $client = new Predis\Client('tcp://127.0.0.1', array(
  133. // 'commands' is a custom option, actually unknown to Predis.
  134. 'commands' => array(
  135. 'set' => Predis\Command\StringSet,
  136. 'get' => Predis\Command\StringGet,
  137. ),
  138. 'profile' => function ($options, $option) {
  139. $profile = $option->getDefault($options);
  140. if (is_array($options->commands)) {
  141. foreach ($options->commands as $command => $class) {
  142. $profile->defineCommand($command, $class);
  143. }
  144. }
  145. return $profile
  146. },
  147. ));
  148. This is the full list of client options supported by `Client`:
  149. ============== ====================================================== ================================================
  150. option description default
  151. ============== ====================================================== ================================================
  152. exceptions Changes how `Client` treats `error replies`_: true
  153. - when ``true``, it throws `Predis\\ServerException`.
  154. - when ``false``, it returns `Predis\\ResponseError`.
  155. -------------- ------------------------------------------------------ ------------------------------------------------
  156. prefix When set, the passed string is transparently applied not set
  157. as a prefix to each key present in command arguments.
  158. .. note::
  159. Keys are prefixed using rules defined by each
  160. command in order to be able to support even complex
  161. cases such as `SORT`_, `EVAL`_ and `EVALSHA`_.
  162. -------------- ------------------------------------------------------ ------------------------------------------------
  163. profile Changes the Redis version `Client` is expected to 2.6
  164. connect to, among a list of :doc:`server profiles`
  165. predefined by Predis. Supported versions are: ``1.2``,
  166. ``2.0``, ``2.2``, ``2.4``, ``2.6``, ``dev`` (unstable
  167. branch in the Redis repository).
  168. This option accepts also the fully-qualified name of
  169. a `Predis\\Profile\\ServerProfileInterface`
  170. or its instance passed either directly or returned by
  171. a callable initializer.
  172. .. note::
  173. In the latter case, Predis will not automatically
  174. apply the specified key prefix to the profile so
  175. it must be manually set by the user's code.
  176. -------------- ------------------------------------------------------ ------------------------------------------------
  177. connections Overrides :doc:`connection backends` by scheme using - tcp: `Predis\\Connection\\StreamConnection`
  178. a named array, with keys being the connection schemes - unix: `Predis\\Connection\\StreamConnection`
  179. subject to change and values being the fully-qualified - http: `Predis\\Connection\\WebdisConnection`
  180. name of classes implementing
  181. `Predis\\Connection\\SingleConnectionInterface`.
  182. This option accepts also the fully-qualified name of
  183. a `Predis\\Connection\\ConnectionFactoryInterface`
  184. or its instance passed either directly or returned by
  185. a callable initializer.
  186. -------------- ------------------------------------------------------ ------------------------------------------------
  187. cluster Changes how `Client` handles :doc:`clustering`: predis
  188. - ``predis`` indicates the use of client-side
  189. sharding.
  190. - ``redis`` indicates the use `redis cluster`_.
  191. This option accepts also the fully-qualified name of
  192. a `Predis\\Connection\\ClusterConnectionInterface`
  193. or its instance passed either directly or returned by
  194. a callable initializer.
  195. -------------- ------------------------------------------------------ ------------------------------------------------
  196. replication When ``true``, the array of connection parameters is not set
  197. used in a master and slaves :doc:`replication` setup
  198. instead of treating the servers as a cluster of nodes.
  199. This option accepts also the fully-qualified name of
  200. a `Predis\\Connection\\ReplicationConnectionInterface`
  201. or its instance passed either directly or returned by
  202. a callable initializer.
  203. ============== ====================================================== ================================================
  204. .. _error replies: http://redis.io/topics/protocol#status-reply
  205. .. _redis cluster: http://redis.io/topics/cluster-spec
  206. .. _SORT: http://redis.io/commands/eval
  207. .. _EVAL: http://redis.io/commands/eval
  208. .. _EVALSHA: http://redis.io/commands/evalsha