discuz_memory.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: discuz_memory.php 36362 2017-02-04 02:02:03Z nemohou $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class discuz_memory extends discuz_base
  12. {
  13. private $config;
  14. private $extension = array();
  15. private $memory;
  16. private $prefix;
  17. private $userprefix;
  18. public $type;
  19. public $enable = false;
  20. public $debug = array();
  21. public function __construct() {
  22. }
  23. public function init($config) {
  24. $this->config = $config;
  25. $this->prefix = empty($config['prefix']) ? substr(md5($_SERVER['HTTP_HOST']), 0, 6).'_' : $config['prefix'];
  26. unset($this->config['prefix']);
  27. foreach($this->config as $cache => $config) {
  28. $available = is_array($config) ? !empty($config['server']) : !empty($config);
  29. if($available && !is_object($this->memory)) {
  30. $class_name = 'memory_driver_'.$cache;
  31. $this->memory = new $class_name();
  32. $this->memory->init($config);
  33. if(!$this->memory->enable) {
  34. $this->memory = null;
  35. } else {
  36. $this->type = $this->memory->cacheName;
  37. $this->enable = true;
  38. }
  39. }
  40. }
  41. }
  42. public function get($key, $prefix = '') {
  43. static $getmulti = null;
  44. $ret = false;
  45. if($this->enable) {
  46. if(!isset($getmulti)) $getmulti = method_exists($this->memory, 'getMulti');
  47. $this->userprefix = $prefix;
  48. if(is_array($key)) {
  49. if($getmulti) {
  50. $ret = $this->memory->getMulti($this->_key($key));
  51. if($ret !== false && !empty($ret)) {
  52. $_ret = array();
  53. foreach((array)$ret as $_key => $value) {
  54. $_ret[$this->_trim_key($_key)] = $value;
  55. }
  56. $ret = $_ret;
  57. }
  58. } else {
  59. $ret = array();
  60. $_ret = false;
  61. foreach($key as $id) {
  62. if(($_ret = $this->memory->get($this->_key($id))) !== false && isset($_ret)) {
  63. $ret[$id] = $_ret;
  64. }
  65. }
  66. }
  67. if(empty($ret)) $ret = false;
  68. } else {
  69. $ret = $this->memory->get($this->_key($key));
  70. if(!isset($ret)) $ret = false;
  71. }
  72. }
  73. return $ret;
  74. }
  75. public function set($key, $value, $ttl = 0, $prefix = '') {
  76. $ret = false;
  77. if($value === false) $value = '';
  78. if($this->enable) {
  79. $this->userprefix = $prefix;
  80. $ret = $this->memory->set($this->_key($key), $value, $ttl);
  81. }
  82. return $ret;
  83. }
  84. public function rm($key, $prefix = '') {
  85. $ret = false;
  86. if($this->enable) {
  87. $this->userprefix = $prefix;
  88. $key = $this->_key($key);
  89. foreach((array)$key as $id) {
  90. $ret = $this->memory->rm($id);
  91. }
  92. }
  93. return $ret;
  94. }
  95. public function clear() {
  96. $ret = false;
  97. if($this->enable && method_exists($this->memory, 'clear')) {
  98. $ret = $this->memory->clear();
  99. }
  100. return $ret;
  101. }
  102. public function inc($key, $step = 1) {
  103. static $hasinc = null;
  104. $ret = false;
  105. if($this->enable) {
  106. if(!isset($hasinc)) $hasinc = method_exists($this->memory, 'inc');
  107. if($hasinc) {
  108. $ret = $this->memory->inc($this->_key($key), $step);
  109. } else {
  110. if(($data = $this->memory->get($key)) !== false) {
  111. $ret = ($this->memory->set($key, $data + ($step)) !== false ? $this->memory->get($key) : false);
  112. }
  113. }
  114. }
  115. return $ret;
  116. }
  117. public function dec($key, $step = 1) {
  118. static $hasdec = null;
  119. $ret = false;
  120. if($this->enable) {
  121. if(!isset($hasdec)) $hasdec = method_exists($this->memory, 'dec');
  122. if($hasdec) {
  123. $ret = $this->memory->dec($this->_key($key), $step);
  124. } else {
  125. if(($data = $this->memory->get($key)) !== false) {
  126. $ret = ($this->memory->set($key, $data - ($step)) !== false ? $this->memory->get($key) : false);
  127. }
  128. }
  129. }
  130. return $ret;
  131. }
  132. private function _key($str) {
  133. $perfix = $this->prefix.$this->userprefix;
  134. if(is_array($str)) {
  135. foreach($str as &$val) {
  136. $val = $perfix.$val;
  137. }
  138. } else {
  139. $str = $perfix.$str;
  140. }
  141. return $str;
  142. }
  143. private function _trim_key($str) {
  144. return substr($str, strlen($this->prefix.$this->userprefix));
  145. }
  146. public function getextension() {
  147. return $this->extension;
  148. }
  149. public function getconfig() {
  150. return $this->config;
  151. }
  152. }
  153. ?>