Connection.php 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\redis;
  8. use yii\base\Component;
  9. use yii\db\Exception;
  10. use yii\helpers\Inflector;
  11. /**
  12. * The redis connection class is used to establish a connection to a [redis](http://redis.io/) server.
  13. *
  14. * By default it assumes there is a redis server running on localhost at port 6379 and uses the database number 0.
  15. *
  16. * It is possible to connect to a redis server using [[hostname]] and [[port]] or using a [[unixSocket]].
  17. *
  18. * It also supports [the AUTH command](http://redis.io/commands/auth) of redis.
  19. * When the server needs authentication, you can set the [[password]] property to
  20. * authenticate with the server after connect.
  21. *
  22. * The execution of [redis commands](http://redis.io/commands) is possible with via [[executeCommand()]].
  23. *
  24. * @method mixed append($key, $value) Append a value to a key. <https://redis.io/commands/append>
  25. * @method mixed auth($password) Authenticate to the server. <https://redis.io/commands/auth>
  26. * @method mixed bgrewriteaof() Asynchronously rewrite the append-only file. <https://redis.io/commands/bgrewriteaof>
  27. * @method mixed bgsave() Asynchronously save the dataset to disk. <https://redis.io/commands/bgsave>
  28. * @method mixed bitcount($key, $start = null, $end = null) Count set bits in a string. <https://redis.io/commands/bitcount>
  29. * @method mixed bitfield($key, ...$operations) Perform arbitrary bitfield integer operations on strings. <https://redis.io/commands/bitfield>
  30. * @method mixed bitop($operation, $destkey, ...$keys) Perform bitwise operations between strings. <https://redis.io/commands/bitop>
  31. * @method mixed bitpos($key, $bit, $start = null, $end = null) Find first bit set or clear in a string. <https://redis.io/commands/bitpos>
  32. * @method mixed blpop(...$keys, $timeout) Remove and get the first element in a list, or block until one is available. <https://redis.io/commands/blpop>
  33. * @method mixed brpop(...$keys, $timeout) Remove and get the last element in a list, or block until one is available. <https://redis.io/commands/brpop>
  34. * @method mixed brpoplpush($source, $destination, $timeout) Pop a value from a list, push it to another list and return it; or block until one is available. <https://redis.io/commands/brpoplpush>
  35. * @method mixed clientKill(...$filters) Kill the connection of a client. <https://redis.io/commands/client-kill>
  36. * @method mixed clientList() Get the list of client connections. <https://redis.io/commands/client-list>
  37. * @method mixed clientGetname() Get the current connection name. <https://redis.io/commands/client-getname>
  38. * @method mixed clientPause($timeout) Stop processing commands from clients for some time. <https://redis.io/commands/client-pause>
  39. * @method mixed clientReply($option) Instruct the server whether to reply to commands. <https://redis.io/commands/client-reply>
  40. * @method mixed clientSetname($connectionName) Set the current connection name. <https://redis.io/commands/client-setname>
  41. * @method mixed clusterAddslots(...$slots) Assign new hash slots to receiving node. <https://redis.io/commands/cluster-addslots>
  42. * @method mixed clusterCountkeysinslot($slot) Return the number of local keys in the specified hash slot. <https://redis.io/commands/cluster-countkeysinslot>
  43. * @method mixed clusterDelslots(...$slots) Set hash slots as unbound in receiving node. <https://redis.io/commands/cluster-delslots>
  44. * @method mixed clusterFailover($option = null) Forces a slave to perform a manual failover of its master.. <https://redis.io/commands/cluster-failover>
  45. * @method mixed clusterForget($nodeId) Remove a node from the nodes table. <https://redis.io/commands/cluster-forget>
  46. * @method mixed clusterGetkeysinslot($slot, $count) Return local key names in the specified hash slot. <https://redis.io/commands/cluster-getkeysinslot>
  47. * @method mixed clusterInfo() Provides info about Redis Cluster node state. <https://redis.io/commands/cluster-info>
  48. * @method mixed clusterKeyslot($key) Returns the hash slot of the specified key. <https://redis.io/commands/cluster-keyslot>
  49. * @method mixed clusterMeet($ip, $port) Force a node cluster to handshake with another node. <https://redis.io/commands/cluster-meet>
  50. * @method mixed clusterNodes() Get Cluster config for the node. <https://redis.io/commands/cluster-nodes>
  51. * @method mixed clusterReplicate($nodeId) Reconfigure a node as a slave of the specified master node. <https://redis.io/commands/cluster-replicate>
  52. * @method mixed clusterReset($resetType = "SOFT") Reset a Redis Cluster node. <https://redis.io/commands/cluster-reset>
  53. * @method mixed clusterSaveconfig() Forces the node to save cluster state on disk. <https://redis.io/commands/cluster-saveconfig>
  54. * @method mixed clusterSetslot($slot, $type, $nodeid = null) Bind a hash slot to a specific node. <https://redis.io/commands/cluster-setslot>
  55. * @method mixed clusterSlaves($nodeId) List slave nodes of the specified master node. <https://redis.io/commands/cluster-slaves>
  56. * @method mixed clusterSlots() Get array of Cluster slot to node mappings. <https://redis.io/commands/cluster-slots>
  57. * @method mixed command() Get array of Redis command details. <https://redis.io/commands/command>
  58. * @method mixed commandCount() Get total number of Redis commands. <https://redis.io/commands/command-count>
  59. * @method mixed commandGetkeys() Extract keys given a full Redis command. <https://redis.io/commands/command-getkeys>
  60. * @method mixed commandInfo(...$commandNames) Get array of specific Redis command details. <https://redis.io/commands/command-info>
  61. * @method mixed configGet($parameter) Get the value of a configuration parameter. <https://redis.io/commands/config-get>
  62. * @method mixed configRewrite() Rewrite the configuration file with the in memory configuration. <https://redis.io/commands/config-rewrite>
  63. * @method mixed configSet($parameter, $value) Set a configuration parameter to the given value. <https://redis.io/commands/config-set>
  64. * @method mixed configResetstat() Reset the stats returned by INFO. <https://redis.io/commands/config-resetstat>
  65. * @method mixed dbsize() Return the number of keys in the selected database. <https://redis.io/commands/dbsize>
  66. * @method mixed debugObject($key) Get debugging information about a key. <https://redis.io/commands/debug-object>
  67. * @method mixed debugSegfault() Make the server crash. <https://redis.io/commands/debug-segfault>
  68. * @method mixed decr($key) Decrement the integer value of a key by one. <https://redis.io/commands/decr>
  69. * @method mixed decrby($key, $decrement) Decrement the integer value of a key by the given number. <https://redis.io/commands/decrby>
  70. * @method mixed del(...$keys) Delete a key. <https://redis.io/commands/del>
  71. * @method mixed discard() Discard all commands issued after MULTI. <https://redis.io/commands/discard>
  72. * @method mixed dump($key) Return a serialized version of the value stored at the specified key.. <https://redis.io/commands/dump>
  73. * @method mixed echo($message) Echo the given string. <https://redis.io/commands/echo>
  74. * @method mixed eval($script, $numkeys, ...$keys, ...$args) Execute a Lua script server side. <https://redis.io/commands/eval>
  75. * @method mixed evalsha($sha1, $numkeys, ...$keys, ...$args) Execute a Lua script server side. <https://redis.io/commands/evalsha>
  76. * @method mixed exec() Execute all commands issued after MULTI. <https://redis.io/commands/exec>
  77. * @method mixed exists(...$keys) Determine if a key exists. <https://redis.io/commands/exists>
  78. * @method mixed expire($key, $seconds) Set a key's time to live in seconds. <https://redis.io/commands/expire>
  79. * @method mixed expireat($key, $timestamp) Set the expiration for a key as a UNIX timestamp. <https://redis.io/commands/expireat>
  80. * @method mixed flushall($ASYNC = null) Remove all keys from all databases. <https://redis.io/commands/flushall>
  81. * @method mixed flushdb($ASYNC = null) Remove all keys from the current database. <https://redis.io/commands/flushdb>
  82. * @method mixed geoadd($key, $longitude, $latitude, $member, ...$more) Add one or more geospatial items in the geospatial index represented using a sorted set. <https://redis.io/commands/geoadd>
  83. * @method mixed geohash($key, ...$members) Returns members of a geospatial index as standard geohash strings. <https://redis.io/commands/geohash>
  84. * @method mixed geopos($key, ...$members) Returns longitude and latitude of members of a geospatial index. <https://redis.io/commands/geopos>
  85. * @method mixed geodist($key, $member1, $member2, $unit = null) Returns the distance between two members of a geospatial index. <https://redis.io/commands/geodist>
  86. * @method mixed georadius($key, $longitude, $latitude, $radius, $metric, ...$options) Query a sorted set representing a geospatial index to fetch members matching a given maximum distance from a point. <https://redis.io/commands/georadius>
  87. * @method mixed georadiusbymember($key, $member, $radius, $metric, ...$options) Query a sorted set representing a geospatial index to fetch members matching a given maximum distance from a member. <https://redis.io/commands/georadiusbymember>
  88. * @method mixed get($key) Get the value of a key. <https://redis.io/commands/get>
  89. * @method mixed getbit($key, $offset) Returns the bit value at offset in the string value stored at key. <https://redis.io/commands/getbit>
  90. * @method mixed getrange($key, $start, $end) Get a substring of the string stored at a key. <https://redis.io/commands/getrange>
  91. * @method mixed getset($key, $value) Set the string value of a key and return its old value. <https://redis.io/commands/getset>
  92. * @method mixed hdel($key, ...$fields) Delete one or more hash fields. <https://redis.io/commands/hdel>
  93. * @method mixed hexists($key, $field) Determine if a hash field exists. <https://redis.io/commands/hexists>
  94. * @method mixed hget($key, $field) Get the value of a hash field. <https://redis.io/commands/hget>
  95. * @method mixed hgetall($key) Get all the fields and values in a hash. <https://redis.io/commands/hgetall>
  96. * @method mixed hincrby($key, $field, $increment) Increment the integer value of a hash field by the given number. <https://redis.io/commands/hincrby>
  97. * @method mixed hincrbyfloat($key, $field, $increment) Increment the float value of a hash field by the given amount. <https://redis.io/commands/hincrbyfloat>
  98. * @method mixed hkeys($key) Get all the fields in a hash. <https://redis.io/commands/hkeys>
  99. * @method mixed hlen($key) Get the number of fields in a hash. <https://redis.io/commands/hlen>
  100. * @method mixed hmget($key, ...$fields) Get the values of all the given hash fields. <https://redis.io/commands/hmget>
  101. * @method mixed hmset($key, $field, $value, ...$more) Set multiple hash fields to multiple values. <https://redis.io/commands/hmset>
  102. * @method mixed hset($key, $field, $value) Set the string value of a hash field. <https://redis.io/commands/hset>
  103. * @method mixed hsetnx($key, $field, $value) Set the value of a hash field, only if the field does not exist. <https://redis.io/commands/hsetnx>
  104. * @method mixed hstrlen($key, $field) Get the length of the value of a hash field. <https://redis.io/commands/hstrlen>
  105. * @method mixed hvals($key) Get all the values in a hash. <https://redis.io/commands/hvals>
  106. * @method mixed incr($key) Increment the integer value of a key by one. <https://redis.io/commands/incr>
  107. * @method mixed incrby($key, $increment) Increment the integer value of a key by the given amount. <https://redis.io/commands/incrby>
  108. * @method mixed incrbyfloat($key, $increment) Increment the float value of a key by the given amount. <https://redis.io/commands/incrbyfloat>
  109. * @method mixed info($section = null) Get information and statistics about the server. <https://redis.io/commands/info>
  110. * @method mixed keys($pattern) Find all keys matching the given pattern. <https://redis.io/commands/keys>
  111. * @method mixed lastsave() Get the UNIX time stamp of the last successful save to disk. <https://redis.io/commands/lastsave>
  112. * @method mixed lindex($key, $index) Get an element from a list by its index. <https://redis.io/commands/lindex>
  113. * @method mixed linsert($key, $where, $pivot, $value) Insert an element before or after another element in a list. <https://redis.io/commands/linsert>
  114. * @method mixed llen($key) Get the length of a list. <https://redis.io/commands/llen>
  115. * @method mixed lpop($key) Remove and get the first element in a list. <https://redis.io/commands/lpop>
  116. * @method mixed lpush($key, ...$values) Prepend one or multiple values to a list. <https://redis.io/commands/lpush>
  117. * @method mixed lpushx($key, $value) Prepend a value to a list, only if the list exists. <https://redis.io/commands/lpushx>
  118. * @method mixed lrange($key, $start, $stop) Get a range of elements from a list. <https://redis.io/commands/lrange>
  119. * @method mixed lrem($key, $count, $value) Remove elements from a list. <https://redis.io/commands/lrem>
  120. * @method mixed lset($key, $index, $value) Set the value of an element in a list by its index. <https://redis.io/commands/lset>
  121. * @method mixed ltrim($key, $start, $stop) Trim a list to the specified range. <https://redis.io/commands/ltrim>
  122. * @method mixed mget(...$keys) Get the values of all the given keys. <https://redis.io/commands/mget>
  123. * @method mixed migrate($host, $port, $key, $destinationDb, $timeout, ...$options) Atomically transfer a key from a Redis instance to another one.. <https://redis.io/commands/migrate>
  124. * @method mixed monitor() Listen for all requests received by the server in real time. <https://redis.io/commands/monitor>
  125. * @method mixed move($key, $db) Move a key to another database. <https://redis.io/commands/move>
  126. * @method mixed mset(...$keyValuePairs) Set multiple keys to multiple values. <https://redis.io/commands/mset>
  127. * @method mixed msetnx(...$keyValuePairs) Set multiple keys to multiple values, only if none of the keys exist. <https://redis.io/commands/msetnx>
  128. * @method mixed multi() Mark the start of a transaction block. <https://redis.io/commands/multi>
  129. * @method mixed object($subcommand, ...$argumentss) Inspect the internals of Redis objects. <https://redis.io/commands/object>
  130. * @method mixed persist($key) Remove the expiration from a key. <https://redis.io/commands/persist>
  131. * @method mixed pexpire($key, $milliseconds) Set a key's time to live in milliseconds. <https://redis.io/commands/pexpire>
  132. * @method mixed pexpireat($key, $millisecondsTimestamp) Set the expiration for a key as a UNIX timestamp specified in milliseconds. <https://redis.io/commands/pexpireat>
  133. * @method mixed pfadd($key, ...$elements) Adds the specified elements to the specified HyperLogLog.. <https://redis.io/commands/pfadd>
  134. * @method mixed pfcount(...$keys) Return the approximated cardinality of the set(s) observed by the HyperLogLog at key(s).. <https://redis.io/commands/pfcount>
  135. * @method mixed pfmerge($destkey, ...$sourcekeys) Merge N different HyperLogLogs into a single one.. <https://redis.io/commands/pfmerge>
  136. * @method mixed ping($message = null) Ping the server. <https://redis.io/commands/ping>
  137. * @method mixed psetex($key, $milliseconds, $value) Set the value and expiration in milliseconds of a key. <https://redis.io/commands/psetex>
  138. * @method mixed psubscribe(...$patterns) Listen for messages published to channels matching the given patterns. <https://redis.io/commands/psubscribe>
  139. * @method mixed pubsub($subcommand, ...$arguments) Inspect the state of the Pub/Sub subsystem. <https://redis.io/commands/pubsub>
  140. * @method mixed pttl($key) Get the time to live for a key in milliseconds. <https://redis.io/commands/pttl>
  141. * @method mixed publish($channel, $message) Post a message to a channel. <https://redis.io/commands/publish>
  142. * @method mixed punsubscribe(...$patterns) Stop listening for messages posted to channels matching the given patterns. <https://redis.io/commands/punsubscribe>
  143. * @method mixed quit() Close the connection. <https://redis.io/commands/quit>
  144. * @method mixed randomkey() Return a random key from the keyspace. <https://redis.io/commands/randomkey>
  145. * @method mixed readonly() Enables read queries for a connection to a cluster slave node. <https://redis.io/commands/readonly>
  146. * @method mixed readwrite() Disables read queries for a connection to a cluster slave node. <https://redis.io/commands/readwrite>
  147. * @method mixed rename($key, $newkey) Rename a key. <https://redis.io/commands/rename>
  148. * @method mixed renamenx($key, $newkey) Rename a key, only if the new key does not exist. <https://redis.io/commands/renamenx>
  149. * @method mixed restore($key, $ttl, $serializedValue, $REPLACE = null) Create a key using the provided serialized value, previously obtained using DUMP.. <https://redis.io/commands/restore>
  150. * @method mixed role() Return the role of the instance in the context of replication. <https://redis.io/commands/role>
  151. * @method mixed rpop($key) Remove and get the last element in a list. <https://redis.io/commands/rpop>
  152. * @method mixed rpoplpush($source, $destination) Remove the last element in a list, prepend it to another list and return it. <https://redis.io/commands/rpoplpush>
  153. * @method mixed rpush($key, ...$values) Append one or multiple values to a list. <https://redis.io/commands/rpush>
  154. * @method mixed rpushx($key, $value) Append a value to a list, only if the list exists. <https://redis.io/commands/rpushx>
  155. * @method mixed sadd($key, ...$members) Add one or more members to a set. <https://redis.io/commands/sadd>
  156. * @method mixed save() Synchronously save the dataset to disk. <https://redis.io/commands/save>
  157. * @method mixed scard($key) Get the number of members in a set. <https://redis.io/commands/scard>
  158. * @method mixed scriptDebug($option) Set the debug mode for executed scripts.. <https://redis.io/commands/script-debug>
  159. * @method mixed scriptExists(...$sha1s) Check existence of scripts in the script cache.. <https://redis.io/commands/script-exists>
  160. * @method mixed scriptFlush() Remove all the scripts from the script cache.. <https://redis.io/commands/script-flush>
  161. * @method mixed scriptKill() Kill the script currently in execution.. <https://redis.io/commands/script-kill>
  162. * @method mixed scriptLoad($script) Load the specified Lua script into the script cache.. <https://redis.io/commands/script-load>
  163. * @method mixed sdiff(...$keys) Subtract multiple sets. <https://redis.io/commands/sdiff>
  164. * @method mixed sdiffstore($destination, ...$keys) Subtract multiple sets and store the resulting set in a key. <https://redis.io/commands/sdiffstore>
  165. * @method mixed select($index) Change the selected database for the current connection. <https://redis.io/commands/select>
  166. * @method mixed set($key, $value, ...$options) Set the string value of a key. <https://redis.io/commands/set>
  167. * @method mixed setbit($key, $offset, $value) Sets or clears the bit at offset in the string value stored at key. <https://redis.io/commands/setbit>
  168. * @method mixed setex($key, $seconds, $value) Set the value and expiration of a key. <https://redis.io/commands/setex>
  169. * @method mixed setnx($key, $value) Set the value of a key, only if the key does not exist. <https://redis.io/commands/setnx>
  170. * @method mixed setrange($key, $offset, $value) Overwrite part of a string at key starting at the specified offset. <https://redis.io/commands/setrange>
  171. * @method mixed shutdown($saveOption = null) Synchronously save the dataset to disk and then shut down the server. <https://redis.io/commands/shutdown>
  172. * @method mixed sinter(...$keys) Intersect multiple sets. <https://redis.io/commands/sinter>
  173. * @method mixed sinterstore($destination, ...$keys) Intersect multiple sets and store the resulting set in a key. <https://redis.io/commands/sinterstore>
  174. * @method mixed sismember($key, $member) Determine if a given value is a member of a set. <https://redis.io/commands/sismember>
  175. * @method mixed slaveof($host, $port) Make the server a slave of another instance, or promote it as master. <https://redis.io/commands/slaveof>
  176. * @method mixed slowlog($subcommand, $argument = null) Manages the Redis slow queries log. <https://redis.io/commands/slowlog>
  177. * @method mixed smembers($key) Get all the members in a set. <https://redis.io/commands/smembers>
  178. * @method mixed smove($source, $destination, $member) Move a member from one set to another. <https://redis.io/commands/smove>
  179. * @method mixed sort($key, ...$options) Sort the elements in a list, set or sorted set. <https://redis.io/commands/sort>
  180. * @method mixed spop($key, $count = null) Remove and return one or multiple random members from a set. <https://redis.io/commands/spop>
  181. * @method mixed srandmember($key, $count = null) Get one or multiple random members from a set. <https://redis.io/commands/srandmember>
  182. * @method mixed srem($key, ...$members) Remove one or more members from a set. <https://redis.io/commands/srem>
  183. * @method mixed strlen($key) Get the length of the value stored in a key. <https://redis.io/commands/strlen>
  184. * @method mixed subscribe(...$channels) Listen for messages published to the given channels. <https://redis.io/commands/subscribe>
  185. * @method mixed sunion(...$keys) Add multiple sets. <https://redis.io/commands/sunion>
  186. * @method mixed sunionstore($destination, ...$keys) Add multiple sets and store the resulting set in a key. <https://redis.io/commands/sunionstore>
  187. * @method mixed swapdb($index, $index) Swaps two Redis databases. <https://redis.io/commands/swapdb>
  188. * @method mixed sync() Internal command used for replication. <https://redis.io/commands/sync>
  189. * @method mixed time() Return the current server time. <https://redis.io/commands/time>
  190. * @method mixed touch(...$keys) Alters the last access time of a key(s). Returns the number of existing keys specified.. <https://redis.io/commands/touch>
  191. * @method mixed ttl($key) Get the time to live for a key. <https://redis.io/commands/ttl>
  192. * @method mixed type($key) Determine the type stored at key. <https://redis.io/commands/type>
  193. * @method mixed unsubscribe(...$channels) Stop listening for messages posted to the given channels. <https://redis.io/commands/unsubscribe>
  194. * @method mixed unlink(...$keys) Delete a key asynchronously in another thread. Otherwise it is just as DEL, but non blocking.. <https://redis.io/commands/unlink>
  195. * @method mixed unwatch() Forget about all watched keys. <https://redis.io/commands/unwatch>
  196. * @method mixed wait($numslaves, $timeout) Wait for the synchronous replication of all the write commands sent in the context of the current connection. <https://redis.io/commands/wait>
  197. * @method mixed watch(...$keys) Watch the given keys to determine execution of the MULTI/EXEC block. <https://redis.io/commands/watch>
  198. * @method mixed zadd($key, ...$options) Add one or more members to a sorted set, or update its score if it already exists. <https://redis.io/commands/zadd>
  199. * @method mixed zcard($key) Get the number of members in a sorted set. <https://redis.io/commands/zcard>
  200. * @method mixed zcount($key, $min, $max) Count the members in a sorted set with scores within the given values. <https://redis.io/commands/zcount>
  201. * @method mixed zincrby($key, $increment, $member) Increment the score of a member in a sorted set. <https://redis.io/commands/zincrby>
  202. * @method mixed zinterstore($destination, $numkeys, $key, ...$options) Intersect multiple sorted sets and store the resulting sorted set in a new key. <https://redis.io/commands/zinterstore>
  203. * @method mixed zlexcount($key, $min, $max) Count the number of members in a sorted set between a given lexicographical range. <https://redis.io/commands/zlexcount>
  204. * @method mixed zrange($key, $start, $stop, $WITHSCORES = null) Return a range of members in a sorted set, by index. <https://redis.io/commands/zrange>
  205. * @method mixed zrangebylex($key, $min, $max, $LIMIT = null, $offset = null, $count = null) Return a range of members in a sorted set, by lexicographical range. <https://redis.io/commands/zrangebylex>
  206. * @method mixed zrevrangebylex($key, $max, $min, $LIMIT = null, $offset = null, $count = null) Return a range of members in a sorted set, by lexicographical range, ordered from higher to lower strings.. <https://redis.io/commands/zrevrangebylex>
  207. * @method mixed zrangebyscore($key, $min, $max, $WITHSCORES = null, $LIMIT = null, $offset = null, $count = null) Return a range of members in a sorted set, by score. <https://redis.io/commands/zrangebyscore>
  208. * @method mixed zrank($key, $member) Determine the index of a member in a sorted set. <https://redis.io/commands/zrank>
  209. * @method mixed zrem($key, ...$members) Remove one or more members from a sorted set. <https://redis.io/commands/zrem>
  210. * @method mixed zremrangebylex($key, $min, $max) Remove all members in a sorted set between the given lexicographical range. <https://redis.io/commands/zremrangebylex>
  211. * @method mixed zremrangebyrank($key, $start, $stop) Remove all members in a sorted set within the given indexes. <https://redis.io/commands/zremrangebyrank>
  212. * @method mixed zremrangebyscore($key, $min, $max) Remove all members in a sorted set within the given scores. <https://redis.io/commands/zremrangebyscore>
  213. * @method mixed zrevrange($key, $start, $stop, $WITHSCORES = null) Return a range of members in a sorted set, by index, with scores ordered from high to low. <https://redis.io/commands/zrevrange>
  214. * @method mixed zrevrangebyscore($key, $max, $min, $WITHSCORES = null, $LIMIT = null, $offset = null, $count = null) Return a range of members in a sorted set, by score, with scores ordered from high to low. <https://redis.io/commands/zrevrangebyscore>
  215. * @method mixed zrevrank($key, $member) Determine the index of a member in a sorted set, with scores ordered from high to low. <https://redis.io/commands/zrevrank>
  216. * @method mixed zscore($key, $member) Get the score associated with the given member in a sorted set. <https://redis.io/commands/zscore>
  217. * @method mixed zunionstore($destination, $numkeys, $key, ...$options) Add multiple sorted sets and store the resulting sorted set in a new key. <https://redis.io/commands/zunionstore>
  218. * @method mixed scan($cursor, $MATCH = null, $pattern = null, $COUNT = null, $count = null) Incrementally iterate the keys space. <https://redis.io/commands/scan>
  219. * @method mixed sscan($key, $cursor, $MATCH = null, $pattern = null, $COUNT = null, $count = null) Incrementally iterate Set elements. <https://redis.io/commands/sscan>
  220. * @method mixed hscan($key, $cursor, $MATCH = null, $pattern = null, $COUNT = null, $count = null) Incrementally iterate hash fields and associated values. <https://redis.io/commands/hscan>
  221. * @method mixed zscan($key, $cursor, $MATCH = null, $pattern = null, $COUNT = null, $count = null) Incrementally iterate sorted sets elements and associated scores. <https://redis.io/commands/zscan>
  222. *
  223. * @property string $driverName Name of the DB driver. This property is read-only.
  224. * @property bool $isActive Whether the DB connection is established. This property is read-only.
  225. * @property LuaScriptBuilder $luaScriptBuilder This property is read-only.
  226. *
  227. * @author Carsten Brandt <mail@cebe.cc>
  228. * @since 2.0
  229. */
  230. class Connection extends Component
  231. {
  232. /**
  233. * @event Event an event that is triggered after a DB connection is established
  234. */
  235. const EVENT_AFTER_OPEN = 'afterOpen';
  236. /**
  237. * @var string the hostname or ip address to use for connecting to the redis server. Defaults to 'localhost'.
  238. * If [[unixSocket]] is specified, hostname and [[port]] will be ignored.
  239. */
  240. public $hostname = 'localhost';
  241. /**
  242. * @var integer the port to use for connecting to the redis server. Default port is 6379.
  243. * If [[unixSocket]] is specified, [[hostname]] and port will be ignored.
  244. */
  245. public $port = 6379;
  246. /**
  247. * @var string the unix socket path (e.g. `/var/run/redis/redis.sock`) to use for connecting to the redis server.
  248. * This can be used instead of [[hostname]] and [[port]] to connect to the server using a unix socket.
  249. * If a unix socket path is specified, [[hostname]] and [[port]] will be ignored.
  250. * @since 2.0.1
  251. */
  252. public $unixSocket;
  253. /**
  254. * @var string the password for establishing DB connection. Defaults to null meaning no AUTH command is sent.
  255. * See http://redis.io/commands/auth
  256. */
  257. public $password;
  258. /**
  259. * @var integer the redis database to use. This is an integer value starting from 0. Defaults to 0.
  260. * Since version 2.0.6 you can disable the SELECT command sent after connection by setting this property to `null`.
  261. */
  262. public $database = 0;
  263. /**
  264. * @var float timeout to use for connection to redis. If not set the timeout set in php.ini will be used: `ini_get("default_socket_timeout")`.
  265. */
  266. public $connectionTimeout = null;
  267. /**
  268. * @var float timeout to use for redis socket when reading and writing data. If not set the php default value will be used.
  269. */
  270. public $dataTimeout = null;
  271. /**
  272. * @var integer Bitmask field which may be set to any combination of connection flags passed to [stream_socket_client()](http://php.net/manual/en/function.stream-socket-client.php).
  273. * Currently the select of connection flags is limited to `STREAM_CLIENT_CONNECT` (default), `STREAM_CLIENT_ASYNC_CONNECT` and `STREAM_CLIENT_PERSISTENT`.
  274. *
  275. * > Warning: `STREAM_CLIENT_PERSISTENT` will make PHP reuse connections to the same server. If you are using multiple
  276. * > connection objects to refer to different redis [[$database|databases]] on the same [[port]], redis commands may
  277. * > get executed on the wrong database. `STREAM_CLIENT_PERSISTENT` is only safe to use if you use only one database.
  278. * >
  279. * > You may still use persistent connections in this case when disambiguating ports as described
  280. * > in [a comment on the PHP manual](http://php.net/manual/en/function.stream-socket-client.php#105393)
  281. * > e.g. on the connection used for session storage, specify the port as:
  282. * >
  283. * > ```php
  284. * > 'port' => '6379/session'
  285. * > ```
  286. *
  287. * @see http://php.net/manual/en/function.stream-socket-client.php
  288. * @since 2.0.5
  289. */
  290. public $socketClientFlags = STREAM_CLIENT_CONNECT;
  291. /**
  292. * @var integer The number of times a command execution should be retried when a connection failure occurs.
  293. * This is used in [[executeCommand()]] when a [[SocketException]] is thrown.
  294. * Defaults to 0 meaning no retries on failure.
  295. * @since 2.0.7
  296. */
  297. public $retries = 0;
  298. /**
  299. * @var array List of available redis commands.
  300. * @see http://redis.io/commands
  301. */
  302. public $redisCommands = [
  303. 'APPEND', // Append a value to a key
  304. 'AUTH', // Authenticate to the server
  305. 'BGREWRITEAOF', // Asynchronously rewrite the append-only file
  306. 'BGSAVE', // Asynchronously save the dataset to disk
  307. 'BITCOUNT', // Count set bits in a string
  308. 'BITFIELD', // Perform arbitrary bitfield integer operations on strings
  309. 'BITOP', // Perform bitwise operations between strings
  310. 'BITPOS', // Find first bit set or clear in a string
  311. 'BLPOP', // Remove and get the first element in a list, or block until one is available
  312. 'BRPOP', // Remove and get the last element in a list, or block until one is available
  313. 'BRPOPLPUSH', // Pop a value from a list, push it to another list and return it; or block until one is available
  314. 'CLIENT KILL', // Kill the connection of a client
  315. 'CLIENT LIST', // Get the list of client connections
  316. 'CLIENT GETNAME', // Get the current connection name
  317. 'CLIENT PAUSE', // Stop processing commands from clients for some time
  318. 'CLIENT REPLY', // Instruct the server whether to reply to commands
  319. 'CLIENT SETNAME', // Set the current connection name
  320. 'CLUSTER ADDSLOTS', // Assign new hash slots to receiving node
  321. 'CLUSTER COUNTKEYSINSLOT', // Return the number of local keys in the specified hash slot
  322. 'CLUSTER DELSLOTS', // Set hash slots as unbound in receiving node
  323. 'CLUSTER FAILOVER', // Forces a slave to perform a manual failover of its master.
  324. 'CLUSTER FORGET', // Remove a node from the nodes table
  325. 'CLUSTER GETKEYSINSLOT', // Return local key names in the specified hash slot
  326. 'CLUSTER INFO', // Provides info about Redis Cluster node state
  327. 'CLUSTER KEYSLOT', // Returns the hash slot of the specified key
  328. 'CLUSTER MEET', // Force a node cluster to handshake with another node
  329. 'CLUSTER NODES', // Get Cluster config for the node
  330. 'CLUSTER REPLICATE', // Reconfigure a node as a slave of the specified master node
  331. 'CLUSTER RESET', // Reset a Redis Cluster node
  332. 'CLUSTER SAVECONFIG', // Forces the node to save cluster state on disk
  333. 'CLUSTER SETSLOT', // Bind a hash slot to a specific node
  334. 'CLUSTER SLAVES', // List slave nodes of the specified master node
  335. 'CLUSTER SLOTS', // Get array of Cluster slot to node mappings
  336. 'COMMAND', // Get array of Redis command details
  337. 'COMMAND COUNT', // Get total number of Redis commands
  338. 'COMMAND GETKEYS', // Extract keys given a full Redis command
  339. 'COMMAND INFO', // Get array of specific Redis command details
  340. 'CONFIG GET', // Get the value of a configuration parameter
  341. 'CONFIG REWRITE', // Rewrite the configuration file with the in memory configuration
  342. 'CONFIG SET', // Set a configuration parameter to the given value
  343. 'CONFIG RESETSTAT', // Reset the stats returned by INFO
  344. 'DBSIZE', // Return the number of keys in the selected database
  345. 'DEBUG OBJECT', // Get debugging information about a key
  346. 'DEBUG SEGFAULT', // Make the server crash
  347. 'DECR', // Decrement the integer value of a key by one
  348. 'DECRBY', // Decrement the integer value of a key by the given number
  349. 'DEL', // Delete a key
  350. 'DISCARD', // Discard all commands issued after MULTI
  351. 'DUMP', // Return a serialized version of the value stored at the specified key.
  352. 'ECHO', // Echo the given string
  353. 'EVAL', // Execute a Lua script server side
  354. 'EVALSHA', // Execute a Lua script server side
  355. 'EXEC', // Execute all commands issued after MULTI
  356. 'EXISTS', // Determine if a key exists
  357. 'EXPIRE', // Set a key's time to live in seconds
  358. 'EXPIREAT', // Set the expiration for a key as a UNIX timestamp
  359. 'FLUSHALL', // Remove all keys from all databases
  360. 'FLUSHDB', // Remove all keys from the current database
  361. 'GEOADD', // Add one or more geospatial items in the geospatial index represented using a sorted set
  362. 'GEOHASH', // Returns members of a geospatial index as standard geohash strings
  363. 'GEOPOS', // Returns longitude and latitude of members of a geospatial index
  364. 'GEODIST', // Returns the distance between two members of a geospatial index
  365. 'GEORADIUS', // Query a sorted set representing a geospatial index to fetch members matching a given maximum distance from a point
  366. 'GEORADIUSBYMEMBER', // Query a sorted set representing a geospatial index to fetch members matching a given maximum distance from a member
  367. 'GET', // Get the value of a key
  368. 'GETBIT', // Returns the bit value at offset in the string value stored at key
  369. 'GETRANGE', // Get a substring of the string stored at a key
  370. 'GETSET', // Set the string value of a key and return its old value
  371. 'HDEL', // Delete one or more hash fields
  372. 'HEXISTS', // Determine if a hash field exists
  373. 'HGET', // Get the value of a hash field
  374. 'HGETALL', // Get all the fields and values in a hash
  375. 'HINCRBY', // Increment the integer value of a hash field by the given number
  376. 'HINCRBYFLOAT', // Increment the float value of a hash field by the given amount
  377. 'HKEYS', // Get all the fields in a hash
  378. 'HLEN', // Get the number of fields in a hash
  379. 'HMGET', // Get the values of all the given hash fields
  380. 'HMSET', // Set multiple hash fields to multiple values
  381. 'HSET', // Set the string value of a hash field
  382. 'HSETNX', // Set the value of a hash field, only if the field does not exist
  383. 'HSTRLEN', // Get the length of the value of a hash field
  384. 'HVALS', // Get all the values in a hash
  385. 'INCR', // Increment the integer value of a key by one
  386. 'INCRBY', // Increment the integer value of a key by the given amount
  387. 'INCRBYFLOAT', // Increment the float value of a key by the given amount
  388. 'INFO', // Get information and statistics about the server
  389. 'KEYS', // Find all keys matching the given pattern
  390. 'LASTSAVE', // Get the UNIX time stamp of the last successful save to disk
  391. 'LINDEX', // Get an element from a list by its index
  392. 'LINSERT', // Insert an element before or after another element in a list
  393. 'LLEN', // Get the length of a list
  394. 'LPOP', // Remove and get the first element in a list
  395. 'LPUSH', // Prepend one or multiple values to a list
  396. 'LPUSHX', // Prepend a value to a list, only if the list exists
  397. 'LRANGE', // Get a range of elements from a list
  398. 'LREM', // Remove elements from a list
  399. 'LSET', // Set the value of an element in a list by its index
  400. 'LTRIM', // Trim a list to the specified range
  401. 'MGET', // Get the values of all the given keys
  402. 'MIGRATE', // Atomically transfer a key from a Redis instance to another one.
  403. 'MONITOR', // Listen for all requests received by the server in real time
  404. 'MOVE', // Move a key to another database
  405. 'MSET', // Set multiple keys to multiple values
  406. 'MSETNX', // Set multiple keys to multiple values, only if none of the keys exist
  407. 'MULTI', // Mark the start of a transaction block
  408. 'OBJECT', // Inspect the internals of Redis objects
  409. 'PERSIST', // Remove the expiration from a key
  410. 'PEXPIRE', // Set a key's time to live in milliseconds
  411. 'PEXPIREAT', // Set the expiration for a key as a UNIX timestamp specified in milliseconds
  412. 'PFADD', // Adds the specified elements to the specified HyperLogLog.
  413. 'PFCOUNT', // Return the approximated cardinality of the set(s) observed by the HyperLogLog at key(s).
  414. 'PFMERGE', // Merge N different HyperLogLogs into a single one.
  415. 'PING', // Ping the server
  416. 'PSETEX', // Set the value and expiration in milliseconds of a key
  417. 'PSUBSCRIBE', // Listen for messages published to channels matching the given patterns
  418. 'PUBSUB', // Inspect the state of the Pub/Sub subsystem
  419. 'PTTL', // Get the time to live for a key in milliseconds
  420. 'PUBLISH', // Post a message to a channel
  421. 'PUNSUBSCRIBE', // Stop listening for messages posted to channels matching the given patterns
  422. 'QUIT', // Close the connection
  423. 'RANDOMKEY', // Return a random key from the keyspace
  424. 'READONLY', // Enables read queries for a connection to a cluster slave node
  425. 'READWRITE', // Disables read queries for a connection to a cluster slave node
  426. 'RENAME', // Rename a key
  427. 'RENAMENX', // Rename a key, only if the new key does not exist
  428. 'RESTORE', // Create a key using the provided serialized value, previously obtained using DUMP.
  429. 'ROLE', // Return the role of the instance in the context of replication
  430. 'RPOP', // Remove and get the last element in a list
  431. 'RPOPLPUSH', // Remove the last element in a list, prepend it to another list and return it
  432. 'RPUSH', // Append one or multiple values to a list
  433. 'RPUSHX', // Append a value to a list, only if the list exists
  434. 'SADD', // Add one or more members to a set
  435. 'SAVE', // Synchronously save the dataset to disk
  436. 'SCARD', // Get the number of members in a set
  437. 'SCRIPT DEBUG', // Set the debug mode for executed scripts.
  438. 'SCRIPT EXISTS', // Check existence of scripts in the script cache.
  439. 'SCRIPT FLUSH', // Remove all the scripts from the script cache.
  440. 'SCRIPT KILL', // Kill the script currently in execution.
  441. 'SCRIPT LOAD', // Load the specified Lua script into the script cache.
  442. 'SDIFF', // Subtract multiple sets
  443. 'SDIFFSTORE', // Subtract multiple sets and store the resulting set in a key
  444. 'SELECT', // Change the selected database for the current connection
  445. 'SET', // Set the string value of a key
  446. 'SETBIT', // Sets or clears the bit at offset in the string value stored at key
  447. 'SETEX', // Set the value and expiration of a key
  448. 'SETNX', // Set the value of a key, only if the key does not exist
  449. 'SETRANGE', // Overwrite part of a string at key starting at the specified offset
  450. 'SHUTDOWN', // Synchronously save the dataset to disk and then shut down the server
  451. 'SINTER', // Intersect multiple sets
  452. 'SINTERSTORE', // Intersect multiple sets and store the resulting set in a key
  453. 'SISMEMBER', // Determine if a given value is a member of a set
  454. 'SLAVEOF', // Make the server a slave of another instance, or promote it as master
  455. 'SLOWLOG', // Manages the Redis slow queries log
  456. 'SMEMBERS', // Get all the members in a set
  457. 'SMOVE', // Move a member from one set to another
  458. 'SORT', // Sort the elements in a list, set or sorted set
  459. 'SPOP', // Remove and return one or multiple random members from a set
  460. 'SRANDMEMBER', // Get one or multiple random members from a set
  461. 'SREM', // Remove one or more members from a set
  462. 'STRLEN', // Get the length of the value stored in a key
  463. 'SUBSCRIBE', // Listen for messages published to the given channels
  464. 'SUNION', // Add multiple sets
  465. 'SUNIONSTORE', // Add multiple sets and store the resulting set in a key
  466. 'SWAPDB', // Swaps two Redis databases
  467. 'SYNC', // Internal command used for replication
  468. 'TIME', // Return the current server time
  469. 'TOUCH', // Alters the last access time of a key(s). Returns the number of existing keys specified.
  470. 'TTL', // Get the time to live for a key
  471. 'TYPE', // Determine the type stored at key
  472. 'UNSUBSCRIBE', // Stop listening for messages posted to the given channels
  473. 'UNLINK', // Delete a key asynchronously in another thread. Otherwise it is just as DEL, but non blocking.
  474. 'UNWATCH', // Forget about all watched keys
  475. 'WAIT', // Wait for the synchronous replication of all the write commands sent in the context of the current connection
  476. 'WATCH', // Watch the given keys to determine execution of the MULTI/EXEC block
  477. 'ZADD', // Add one or more members to a sorted set, or update its score if it already exists
  478. 'ZCARD', // Get the number of members in a sorted set
  479. 'ZCOUNT', // Count the members in a sorted set with scores within the given values
  480. 'ZINCRBY', // Increment the score of a member in a sorted set
  481. 'ZINTERSTORE', // Intersect multiple sorted sets and store the resulting sorted set in a new key
  482. 'ZLEXCOUNT', // Count the number of members in a sorted set between a given lexicographical range
  483. 'ZRANGE', // Return a range of members in a sorted set, by index
  484. 'ZRANGEBYLEX', // Return a range of members in a sorted set, by lexicographical range
  485. 'ZREVRANGEBYLEX', // Return a range of members in a sorted set, by lexicographical range, ordered from higher to lower strings.
  486. 'ZRANGEBYSCORE', // Return a range of members in a sorted set, by score
  487. 'ZRANK', // Determine the index of a member in a sorted set
  488. 'ZREM', // Remove one or more members from a sorted set
  489. 'ZREMRANGEBYLEX', // Remove all members in a sorted set between the given lexicographical range
  490. 'ZREMRANGEBYRANK', // Remove all members in a sorted set within the given indexes
  491. 'ZREMRANGEBYSCORE', // Remove all members in a sorted set within the given scores
  492. 'ZREVRANGE', // Return a range of members in a sorted set, by index, with scores ordered from high to low
  493. 'ZREVRANGEBYSCORE', // Return a range of members in a sorted set, by score, with scores ordered from high to low
  494. 'ZREVRANK', // Determine the index of a member in a sorted set, with scores ordered from high to low
  495. 'ZSCORE', // Get the score associated with the given member in a sorted set
  496. 'ZUNIONSTORE', // Add multiple sorted sets and store the resulting sorted set in a new key
  497. 'SCAN', // Incrementally iterate the keys space
  498. 'SSCAN', // Incrementally iterate Set elements
  499. 'HSCAN', // Incrementally iterate hash fields and associated values
  500. 'ZSCAN', // Incrementally iterate sorted sets elements and associated scores
  501. ];
  502. /**
  503. * @var resource redis socket connection
  504. */
  505. private $_socket = false;
  506. /**
  507. * Closes the connection when this component is being serialized.
  508. * @return array
  509. */
  510. public function __sleep()
  511. {
  512. $this->close();
  513. return array_keys(get_object_vars($this));
  514. }
  515. /**
  516. * Returns a value indicating whether the DB connection is established.
  517. * @return bool whether the DB connection is established
  518. */
  519. public function getIsActive()
  520. {
  521. return $this->_socket !== false;
  522. }
  523. /**
  524. * Establishes a DB connection.
  525. * It does nothing if a DB connection has already been established.
  526. * @throws Exception if connection fails
  527. */
  528. public function open()
  529. {
  530. if ($this->_socket !== false) {
  531. return;
  532. }
  533. $connection = ($this->unixSocket ?: $this->hostname . ':' . $this->port) . ', database=' . $this->database;
  534. \Yii::trace('Opening redis DB connection: ' . $connection, __METHOD__);
  535. $this->_socket = @stream_socket_client(
  536. $this->unixSocket ? 'unix://' . $this->unixSocket : 'tcp://' . $this->hostname . ':' . $this->port,
  537. $errorNumber,
  538. $errorDescription,
  539. $this->connectionTimeout ? $this->connectionTimeout : ini_get('default_socket_timeout'),
  540. $this->socketClientFlags
  541. );
  542. if ($this->_socket) {
  543. if ($this->dataTimeout !== null) {
  544. stream_set_timeout($this->_socket, $timeout = (int) $this->dataTimeout, (int) (($this->dataTimeout - $timeout) * 1000000));
  545. }
  546. if ($this->password !== null) {
  547. $this->executeCommand('AUTH', [$this->password]);
  548. }
  549. if ($this->database !== null) {
  550. $this->executeCommand('SELECT', [$this->database]);
  551. }
  552. $this->initConnection();
  553. } else {
  554. \Yii::error("Failed to open redis DB connection ($connection): $errorNumber - $errorDescription", __CLASS__);
  555. $message = YII_DEBUG ? "Failed to open redis DB connection ($connection): $errorNumber - $errorDescription" : 'Failed to open DB connection.';
  556. throw new Exception($message, $errorDescription, $errorNumber);
  557. }
  558. }
  559. /**
  560. * Closes the currently active DB connection.
  561. * It does nothing if the connection is already closed.
  562. */
  563. public function close()
  564. {
  565. if ($this->_socket !== false) {
  566. $connection = ($this->unixSocket ?: $this->hostname . ':' . $this->port) . ', database=' . $this->database;
  567. \Yii::trace('Closing DB connection: ' . $connection, __METHOD__);
  568. try {
  569. $this->executeCommand('QUIT');
  570. } catch (SocketException $e) {
  571. // ignore errors when quitting a closed connection
  572. }
  573. fclose($this->_socket);
  574. $this->_socket = false;
  575. }
  576. }
  577. /**
  578. * Initializes the DB connection.
  579. * This method is invoked right after the DB connection is established.
  580. * The default implementation triggers an [[EVENT_AFTER_OPEN]] event.
  581. */
  582. protected function initConnection()
  583. {
  584. $this->trigger(self::EVENT_AFTER_OPEN);
  585. }
  586. /**
  587. * Returns the name of the DB driver for the current [[dsn]].
  588. * @return string name of the DB driver
  589. */
  590. public function getDriverName()
  591. {
  592. return 'redis';
  593. }
  594. /**
  595. * @return LuaScriptBuilder
  596. */
  597. public function getLuaScriptBuilder()
  598. {
  599. return new LuaScriptBuilder();
  600. }
  601. /**
  602. * Allows issuing all supported commands via magic methods.
  603. *
  604. * ```php
  605. * $redis->hmset('test_collection', 'key1', 'val1', 'key2', 'val2')
  606. * ```
  607. *
  608. * @param string $name name of the missing method to execute
  609. * @param array $params method call arguments
  610. * @return mixed
  611. */
  612. public function __call($name, $params)
  613. {
  614. $redisCommand = strtoupper(Inflector::camel2words($name, false));
  615. if (in_array($redisCommand, $this->redisCommands)) {
  616. return $this->executeCommand($redisCommand, $params);
  617. } else {
  618. return parent::__call($name, $params);
  619. }
  620. }
  621. /**
  622. * Executes a redis command.
  623. * For a list of available commands and their parameters see http://redis.io/commands.
  624. *
  625. * The params array should contain the params separated by white space, e.g. to execute
  626. * `SET mykey somevalue NX` call the following:
  627. *
  628. * ```php
  629. * $redis->executeCommand('SET', ['mykey', 'somevalue', 'NX']);
  630. * ```
  631. *
  632. * @param string $name the name of the command
  633. * @param array $params list of parameters for the command
  634. * @return array|bool|null|string Dependent on the executed command this method
  635. * will return different data types:
  636. *
  637. * - `true` for commands that return "status reply" with the message `'OK'` or `'PONG'`.
  638. * - `string` for commands that return "status reply" that does not have the message `OK` (since version 2.0.1).
  639. * - `string` for commands that return "integer reply"
  640. * as the value is in the range of a signed 64 bit integer.
  641. * - `string` or `null` for commands that return "bulk reply".
  642. * - `array` for commands that return "Multi-bulk replies".
  643. *
  644. * See [redis protocol description](http://redis.io/topics/protocol)
  645. * for details on the mentioned reply types.
  646. * @throws Exception for commands that return [error reply](http://redis.io/topics/protocol#error-reply).
  647. */
  648. public function executeCommand($name, $params = [])
  649. {
  650. $this->open();
  651. $params = array_merge(explode(' ', $name), $params);
  652. $command = '';
  653. $paramsCount = 0;
  654. foreach ($params as $arg) {
  655. if ($arg === null) {
  656. continue;
  657. }
  658. $paramsCount++;
  659. $command .= '$' . mb_strlen($arg, '8bit') . "\r\n" . $arg . "\r\n";
  660. }
  661. $command = '*' . $paramsCount . "\r\n" . $command;
  662. \Yii::trace("Executing Redis Command: {$name}", __METHOD__);
  663. if ($this->retries > 0) {
  664. $tries = $this->retries;
  665. while ($tries-- > 0) {
  666. try {
  667. return $this->sendCommandInternal($command, $params);
  668. } catch (SocketException $e) {
  669. \Yii::error($e, __METHOD__);
  670. // backup retries, fail on commands that fail inside here
  671. $retries = $this->retries;
  672. $this->retries = 0;
  673. $this->close();
  674. $this->open();
  675. $this->retries = $retries;
  676. }
  677. }
  678. }
  679. return $this->sendCommandInternal($command, $params);
  680. }
  681. /**
  682. * Sends RAW command string to the server.
  683. * @throws SocketException on connection error.
  684. */
  685. private function sendCommandInternal($command, $params)
  686. {
  687. $written = @fwrite($this->_socket, $command);
  688. if ($written === false) {
  689. throw new SocketException("Failed to write to socket.\nRedis command was: " . $command);
  690. }
  691. if ($written !== ($len = mb_strlen($command, '8bit'))) {
  692. throw new SocketException("Failed to write to socket. $written of $len bytes written.\nRedis command was: " . $command);
  693. }
  694. return $this->parseResponse(implode(' ', $params));
  695. }
  696. /**
  697. * @param string $command
  698. * @return mixed
  699. * @throws Exception on error
  700. */
  701. private function parseResponse($command)
  702. {
  703. if (($line = fgets($this->_socket)) === false) {
  704. throw new SocketException("Failed to read from socket.\nRedis command was: " . $command);
  705. }
  706. $type = $line[0];
  707. $line = mb_substr($line, 1, -2, '8bit');
  708. switch ($type) {
  709. case '+': // Status reply
  710. if ($line === 'OK' || $line === 'PONG') {
  711. return true;
  712. } else {
  713. return $line;
  714. }
  715. case '-': // Error reply
  716. throw new Exception("Redis error: " . $line . "\nRedis command was: " . $command);
  717. case ':': // Integer reply
  718. // no cast to int as it is in the range of a signed 64 bit integer
  719. return $line;
  720. case '$': // Bulk replies
  721. if ($line == '-1') {
  722. return null;
  723. }
  724. $length = (int)$line + 2;
  725. $data = '';
  726. while ($length > 0) {
  727. if (($block = fread($this->_socket, $length)) === false) {
  728. throw new SocketException("Failed to read from socket.\nRedis command was: " . $command);
  729. }
  730. $data .= $block;
  731. $length -= mb_strlen($block, '8bit');
  732. }
  733. return mb_substr($data, 0, -2, '8bit');
  734. case '*': // Multi-bulk replies
  735. $count = (int) $line;
  736. $data = [];
  737. for ($i = 0; $i < $count; $i++) {
  738. $data[] = $this->parseResponse($command);
  739. }
  740. return $data;
  741. default:
  742. throw new Exception('Received illegal data from redis: ' . $line . "\nRedis command was: " . $command);
  743. }
  744. }
  745. }