Memcached.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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')) {
  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. $this->_conn = new \Memcached();
  71. foreach ($this->_default_policy['servers'] as $server) {
  72. $result = $this->_conn->addServer($server['host'], $server['port'], $this->_default_policy['persistent']);
  73. if (!$result) {
  74. return \Qii::setError(false, __LINE__, 1005, $server['host'], $server['port']);
  75. }
  76. }
  77. }
  78. /**
  79. * 写入缓存
  80. *
  81. * @param string $id
  82. * @param mixed $data
  83. * @param array $policy
  84. * @return boolean
  85. */
  86. public function set($id, $data, array $policy = null)
  87. {
  88. if (!isset($policy['life_time'])) $policy['life_time'] = $this->_default_policy['life_time'];
  89. return $this->_conn->set($id, $data, $policy['life_time']);
  90. }
  91. /**
  92. * 读取缓存,失败或缓存撒失效时返回 false
  93. *
  94. * @param string $id
  95. *
  96. * @return mixed
  97. */
  98. public function get($id)
  99. {
  100. return $this->_conn->get($id);
  101. }
  102. /**
  103. * 删除指定的缓存
  104. *
  105. * @param string $id
  106. * @return boolean
  107. */
  108. public function remove($id)
  109. {
  110. return $this->_conn->delete($id);
  111. }
  112. /**
  113. * 清除所有的缓存数据
  114. *
  115. * @return boolean
  116. */
  117. public function clean()
  118. {
  119. return $this->_conn->flush();
  120. }
  121. }
  122. ?>