Redis.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace Qii\Cache;
  3. use Qii\Cache\Redis\Cluster;
  4. use Qii\Exceptions\Errors;
  5. use Qii\Exceptions\MethodNotFound;
  6. \Qii\Autoloader\Import::requires(array(dirname(__FILE__) . DS . 'Redis/Client.php', dirname(__FILE__) . DS . 'Redis/Cluster.php'));
  7. /**
  8. * PHP 操作 redis
  9. * @author Jinhui.Zhu
  10. *
  11. */
  12. class Redis implements Intf
  13. {
  14. const VERSION = '1.2';
  15. public $redis;
  16. protected $policy = array(
  17. /**
  18. * 缓存服务器配置,参看$_default_server
  19. * 允许多个缓存服务器
  20. */
  21. 'servers' => array(['host' => '127.0.0.1', 'port' => '6379', 'password' => '']),
  22. /**
  23. * 缓存有效时间
  24. *
  25. * 如果设置为 0 表示缓存永不过期
  26. */
  27. 'life_time' => 900
  28. );
  29. public function __construct(array $policy = null)
  30. {
  31. if (!extension_loaded('redis')) {
  32. throw new MethodNotFound(\Qii::i(1006), __LINE__);
  33. }
  34. if (is_array($policy)) {
  35. $this->policy = array_merge($this->policy, $policy);
  36. }
  37. $redisServer = array();
  38. foreach ($this->policy['servers'] AS $value) {
  39. $redisServer[] = array('host' => $value['host'], 'port' => $value['port'], 'password' => (isset($value['password']) ? $value['password'] : ''));
  40. }
  41. $this->redis = new Cluster($redisServer, 128);
  42. }
  43. /**
  44. * 保存指定key的数据
  45. */
  46. public function hMset($id, $data, array $policy = null)
  47. {
  48. $res = false;
  49. if (is_array($policy)) {
  50. $this->policy = array_merge($this->policy, $policy);
  51. }
  52. try {
  53. $res = $this->redis->hMset($id, $data);
  54. if (isset($this->policy['life_time']) && $this->policy['life_time'] > 0) {
  55. $this->redis->expire($id, $this->policy['life_time']);
  56. }
  57. } catch (\CredisException $e) {
  58. throw new Errors(\Qii::i(-1, $e->getMessage()), __LINE__);
  59. }
  60. return $res;
  61. }
  62. /**
  63. * 保存指定key的数据
  64. */
  65. public function set($id, $value, array $policy = null)
  66. {
  67. $res = false;
  68. if (is_array($policy)) {
  69. $this->policy = array_merge($this->policy, $policy);
  70. }
  71. try {
  72. $res = $this->redis->set($id, $value);
  73. if (isset($this->policy['life_time']) && $this->policy['life_time'] > 0) {
  74. $this->redis->expire($id, $this->policy['life_time']);
  75. }
  76. } catch (\CredisException $e) {
  77. throw new Errors(\Qii::i(-1, $e->getMessage()), __LINE__);
  78. }
  79. return $res;
  80. }
  81. /**
  82. * 获取指定key的数据
  83. */
  84. public function hGet($id)
  85. {
  86. if ($this->redis->exists($id)) {
  87. return $this->redis->hGetAll($id);
  88. }
  89. return null;
  90. }
  91. /**
  92. * 获取指定key的数据
  93. */
  94. public function get($id)
  95. {
  96. if ($this->redis->exists($id)) {
  97. return $this->redis->get($id);
  98. }
  99. return null;
  100. }
  101. public function exists($id)
  102. {
  103. return $this->redis->exists($id);
  104. }
  105. /**
  106. * 删除指定key的数据
  107. */
  108. public function remove($id)
  109. {
  110. if ($this->redis->exists($id)) {
  111. return $this->redis->delete($id);
  112. }
  113. }
  114. /**
  115. * 清除当前db的所有数据
  116. */
  117. public function clean()
  118. {
  119. $this->redis->flushdb();
  120. }
  121. public function __call($method, $args)
  122. {
  123. return call_user_func_array(array($this->redis, $method), $args);
  124. }
  125. }