Memcached.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * Memcache缓存
  4. * @author Jinhui Zhu<jinhui.zhu@live.cn>2015-10-25 21:41
  5. *
  6. */
  7. namespace Qii\Cache;
  8. class Memcached implements Intf
  9. {
  10. const VERSION = '1.2';
  11. /**
  12. * memcached连接句柄
  13. *
  14. * @var resource
  15. */
  16. protected $_conn;
  17. /**
  18. * 默认的缓存策略
  19. *
  20. * @var array
  21. */
  22. protected $_default_policy = array(
  23. /**
  24. * 缓存服务器配置,参看$_default_server
  25. * 允许多个缓存服务器
  26. */
  27. 'servers' => array(),
  28. /**
  29. * 是否压缩缓存数据
  30. */
  31. 'compressed' => false,
  32. /**
  33. * 缓存有效时间
  34. *
  35. * 如果设置为 0 表示缓存永不过期
  36. */
  37. 'life_time' => 900,
  38. /**
  39. * 是否使用持久连接
  40. */
  41. 'persistent' => true,
  42. );
  43. /**
  44. * 默认的缓存服务器
  45. *
  46. * @var array
  47. */
  48. protected $_default_server = array(
  49. /**
  50. * 缓存服务器地址或主机名
  51. */
  52. 'host' => '127.0.0.1',
  53. /**
  54. * 缓存服务器端口
  55. */
  56. 'port' => '11211',
  57. );
  58. public function __construct(array $policy = null)
  59. {
  60. if (!extension_loaded('memcached') && !extension_loaded('memcache')) {
  61. return \Qii::setError(false, __LINE__, 1004);
  62. }
  63. if (is_array($policy)) {
  64. $this->_default_policy = array_merge($this->_default_policy, $policy);
  65. }
  66. if (empty($this->_default_policy['servers'])) {
  67. $this->_default_policy['servers'][] = $this->_default_server;
  68. }
  69. if (!isset($this->_default_policy['persistent'])) $this->_default_policy['persistent'] = '';
  70. if(extension_loaded('memcached'))
  71. {
  72. $this->_conn = new \Memcached();
  73. }
  74. else
  75. {
  76. $this->_conn = new \Memcache();
  77. }
  78. foreach ($this->_default_policy['servers'] as $server) {
  79. $result = $this->_conn->addServer($server['host'], $server['port'], $this->_default_policy['persistent']);
  80. if (!$result) {
  81. return \Qii::setError(false, __LINE__, 1005, $server['host'], $server['port']);
  82. }
  83. }
  84. }
  85. /**
  86. * 写入缓存
  87. *
  88. * @param string $id
  89. * @param mixed $data
  90. * @param array $policy
  91. * @return boolean
  92. */
  93. public function set($id, $data, array $policy = null)
  94. {
  95. if (is_array($policy)) {
  96. $this->_default_policy = array_merge($this->_default_policy, $policy);
  97. }
  98. if($this->_conn instanceof \Memcache || $this->_conn instanceof \Memcached){
  99. $data = serialize($data);
  100. if($this->_conn instanceof \Memcache) {
  101. return $this->_conn->set($id, $data, MEMCACHE_COMPRESSED, $this->_default_policy['life_time']);
  102. }
  103. return $this->_conn->set($id, $data, $this->_default_policy['life_time']);
  104. }
  105. return $this->_conn->set($id, $data, $this->_default_policy['life_time']);
  106. }
  107. /**
  108. * 读取缓存,失败或缓存撒失效时返回 false
  109. *
  110. * @param string $id
  111. *
  112. * @return mixed
  113. */
  114. public function get($id)
  115. {
  116. $data = $this->_conn->get($id);
  117. if($this->_conn instanceof \Memcache || $this->_conn instanceof \Memcached){
  118. $data = unserialize($data);
  119. }
  120. return $data;
  121. }
  122. /**
  123. * 删除指定的缓存
  124. *
  125. * @param string $id
  126. * @return boolean
  127. */
  128. public function remove($id)
  129. {
  130. return $this->_conn->delete($id);
  131. }
  132. /**
  133. * 清除所有的缓存数据
  134. *
  135. * @return boolean
  136. */
  137. public function clean()
  138. {
  139. return $this->_conn->flush();
  140. }
  141. public function __call($method, $args)
  142. {
  143. return call_user_func_array(array($this->_conn, $method), $args);
  144. }
  145. }