client.rst 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. ''''''''''''''