cache.memcache.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * memcache 操作
  4. */
  5. defined('InShopNC') or exit('Access Invalid!');
  6. class CacheMemcache extends Cache {
  7. /**
  8. * 配置信息
  9. *
  10. * @var unknown_type
  11. */
  12. private $config;
  13. /**
  14. * 键值前缀(区分各业务层)
  15. *
  16. * @var string
  17. */
  18. private $type;
  19. /**
  20. * 键值前缀(系统级别)
  21. *
  22. * @var string
  23. */
  24. private $prefix;
  25. public function __construct(){
  26. $this->config = C('memcache');
  27. if (!extension_loaded('memcache') || !is_array($this->config[1])) {
  28. throw_exception('memcache failed to load');
  29. }
  30. $this->init();
  31. }
  32. /**
  33. * 初始化
  34. * @return void
  35. */
  36. private function init(){
  37. $this->prefix= $this->config['prefix'] ? $this->config['prefix'] : substr(md5($_SERVER['HTTP_HOST']), 0, 6).'_';
  38. $this->handler = new Memcache;
  39. if (function_exists('memcache_add_server')){ //版本 > 2.0.0
  40. foreach ($this->config as $key=>$conf) {
  41. if (is_numeric($key)){
  42. $this->enable = $this->handler->addServer($conf['host'], $conf['port'], $conf['pconnect']?true:false);
  43. }
  44. }
  45. }else{
  46. $func = $this->config[1]['pconnect'] ? 'pconnect' : 'connect';
  47. $this->enable = @$this->handler->$func($this->config[1]['host'], $this->config[1]['port']);
  48. }
  49. }
  50. /**
  51. * 判断是否有效
  52. *
  53. * @return bool
  54. */
  55. public function isConnected(){
  56. return $this->enable;
  57. }
  58. /**
  59. * 设置值
  60. *
  61. * @param mixed $key
  62. * @param mixed $value
  63. * @param string $type
  64. * @param int $ttl
  65. * @return bool
  66. */
  67. public function set($key, $value, $type='', $ttl = SESSION_EXPIRE){
  68. if (!$this->enable) return false;
  69. $this->type = $type;
  70. return $this->handler->set($this->_key($key), $value, MEMCACHE_COMPRESSED, $ttl);
  71. }
  72. /**
  73. * 取得值
  74. *
  75. * @param mixed $key
  76. * @param mixed $type
  77. * @return bool
  78. */
  79. public function get($key, $type=''){
  80. if (!$this->enable) return false;
  81. $this->type = $type;
  82. return $this->handler->get($this->_key($key));
  83. }
  84. /**
  85. * 删除值
  86. *
  87. * @param mixed $key
  88. * @param mixed $type
  89. * @return bool
  90. */
  91. public function rm($key, $type=''){
  92. $this->type = $type;
  93. return $this->handler->delete($this->_key($key));
  94. }
  95. /**
  96. * 清空值
  97. *
  98. * @return bool
  99. */
  100. public function clear(){
  101. return $this->handler->flush();
  102. }
  103. /**
  104. * 加前缀
  105. *
  106. * @param string $str
  107. * @return string
  108. */
  109. private function _key($str) {
  110. return $this->prefix.$this->type.$str;
  111. }
  112. /**
  113. * 递增
  114. *
  115. * @param string $key
  116. * @param int $step
  117. * @return int/false
  118. */
  119. public function inc($key, $step = 1) {
  120. return $this->handler->increment($this->_key($key), $step);
  121. }
  122. /**
  123. * 递减
  124. *
  125. * @param string $key
  126. * @param int $step
  127. * @return int/false
  128. */
  129. public function dec($key, $step = 1) {
  130. return $this->handler->decrement($this->_key($key), $step);
  131. }
  132. }