redis.php 2.7 KB

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