Memcache.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 Memcache 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('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. $this->_conn = new \Memcache();
  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['compressed'])) $policy['compressed'] = $this->_default_policy['compressed'];
  89. if (!isset($policy['life_time'])) $policy['life_time'] = $this->_default_policy['life_time'];
  90. return $this->_conn->set($id, $data, $policy['compressed'] ? MEMCACHE_COMPRESSED : 0, $policy['life_time']);
  91. }
  92. /**
  93. * 读取缓存,失败或缓存撒失效时返回 false
  94. *
  95. * @param string $id
  96. *
  97. * @return mixed
  98. */
  99. public function get($id)
  100. {
  101. return $this->_conn->get($id);
  102. }
  103. /**
  104. * 删除指定的缓存
  105. *
  106. * @param string $id
  107. * @return boolean
  108. */
  109. public function remove($id)
  110. {
  111. return $this->_conn->delete($id);
  112. }
  113. /**
  114. * 清除所有的缓存数据
  115. *
  116. * @return boolean
  117. */
  118. public function clean()
  119. {
  120. return $this->_conn->flush();
  121. }
  122. }
  123. ?>