redis.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php namespace Laravel;
  2. class Redis {
  3. /**
  4. * The active Redis connections.
  5. *
  6. * @var array
  7. */
  8. protected static $connections = array();
  9. /**
  10. * The name of the Redis connection.
  11. *
  12. * @var string
  13. */
  14. public $name;
  15. /**
  16. * The configuration array for the Redis connection.
  17. *
  18. * @var array
  19. */
  20. public $config = array();
  21. /**
  22. * The connection to the Redis database.
  23. *
  24. * @var resource
  25. */
  26. protected $connection;
  27. /**
  28. * Create a new Redis connection instance.
  29. *
  30. * @param string $name
  31. * @param array $config
  32. * @return void
  33. */
  34. public function __construct($name, $config)
  35. {
  36. $this->name = $name;
  37. $this->config = $config;
  38. }
  39. /**
  40. * Create a new Redis connection instance.
  41. *
  42. * @param string $connection
  43. * @param array $config
  44. * @return Redis
  45. */
  46. public static function make($name, $config)
  47. {
  48. return new static($name, $config);
  49. }
  50. /**
  51. * Create a new Redis connection instance.
  52. *
  53. * Redis connections are managed as singletons, so if the connection has
  54. * already been established, that same connection instance will be returned
  55. * on subsequent requests for the connection.
  56. *
  57. * @param string $connection
  58. * @return Redis
  59. */
  60. public static function connection($name)
  61. {
  62. if ( ! array_key_exists($name, static::$connections))
  63. {
  64. $config = Config::get("database.redis.{$name}");
  65. if (is_null($config))
  66. {
  67. throw new \Exception("Redis connection [$name] has not been configured.");
  68. }
  69. static::$connections[$name] = static::make($name, $config)->connect();
  70. }
  71. return static::$connections[$name];
  72. }
  73. /**
  74. * Connect to the Redis database.
  75. *
  76. * The Redis instance itself will be returned by the method.
  77. *
  78. * @return Redis
  79. */
  80. public function connect()
  81. {
  82. $this->connection = @fsockopen($this->config['host'], $this->config['port'], $error, $message);
  83. if ($this->connection === false)
  84. {
  85. throw new \Exception("Error establishing Redis connection [{$this->name}]: {$error} - {$message}");
  86. }
  87. return $this;
  88. }
  89. /**
  90. * Execute a command agaisnt the Redis database.
  91. *
  92. * @param string $method
  93. * @param array $parameters
  94. * @return mixed
  95. */
  96. public function do($method, $parameters)
  97. {
  98. fwrite($this->connection, $this->command($method, $parameters));
  99. $reply = trim(fgets($this->connection, 512));
  100. }
  101. /**
  102. * Build the Redis command based from a given method and parameters.
  103. *
  104. * @param string $method
  105. * @param array $parameters
  106. * @return string
  107. */
  108. protected function command($method, $parameters)
  109. {
  110. $command = '*'.(count($parameters) + 1).CRLF.'$'.strlen($method).CRLF.strtoupper($method).CRLF;
  111. foreach ($parameters as $parameter)
  112. {
  113. $command .= '$'.strlen($parameter).CRLF.$parameter.CRLF;
  114. }
  115. return $command;
  116. }
  117. /**
  118. * Dynamically make calls to the Redis database.
  119. */
  120. public function __call($method, $parameters)
  121. {
  122. return $this->do($method, $parameters);
  123. }
  124. /**
  125. * Close the connection to the Redis database.
  126. *
  127. * @return void
  128. */
  129. public function __destruct()
  130. {
  131. fclose($this->connection);
  132. }
  133. }