PageRenderTime 61ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/New Folder/functions/include/library/Cache.class.php

https://github.com/bigbanton/Monline
PHP | 407 lines | 175 code | 218 blank | 14 comment | 15 complexity | fe490cbbd754dcbdb2f4e76f2665e49d MD5 | raw file
  1. <?php
  2. /**
  3. * //License information must not be removed.
  4. *
  5. * PHP version 5.4x
  6. *
  7. * @Category ### Gripsell ###
  8. * @Package ### Advanced ###
  9. * @Architecture ### Secured ###
  10. * @Copyright (c) 2013 {@URL http://www.gripsell.com Gripsell eApps & Technologies Private Limited}
  11. * @License EULA License http://www.gripsell.com
  12. * @Author $Author: gripsell $
  13. * @Version $Version: 5.3.3 $
  14. * @Last Revision $Date: 2013-21-05 00:00:00 +0530 (Tue, 21 May 2013) $
  15. */
  16. class FalseCache {
  17. public function __call($m, $v)
  18. {
  19. }
  20. } ;
  21. class Cache
  22. {
  23. static private $mCache = null;
  24. static private $gCache = array();
  25. static private $mInstance = null;
  26. static private $mIndex = 0;
  27. static public function Instance()
  28. {
  29. if (!self::$mInstance) self::$mInstance = new Cache();
  30. }
  31. static private function _CreateCacheInstance()
  32. {
  33. $ini = Config::Instance('php');
  34. settype($ini['memcache'], 'array');
  35. if (!class_exists('Memcache', false)) return new FalseCache();
  36. $cache_instance = new Memcache();
  37. foreach($ini['memcache'] AS $one)
  38. {
  39. $server = (string) $one;
  40. list($ip, $port, $weight) = explode(':', $server);
  41. if (!$ip || $port || $weight) continue;
  42. $cache_instance->addServer($ip
  43. , $port
  44. , true
  45. , $weight
  46. , 1
  47. , 15
  48. , true
  49. , array('Cache', 'FailureCallback')
  50. );
  51. self::$mIndex++;
  52. }
  53. return self::$mIndex ? $cache_instance : new FalseCache();
  54. }
  55. private function __construct()
  56. {
  57. self::$mCache = self::_CreateCacheInstance();
  58. }
  59. static public function FailureCallback($ip, $port)
  60. {
  61. self::$mIndex--;
  62. if (self::$mIndex <= 0) self::$mCache = new FalseCache();
  63. }
  64. static function Get($key)
  65. {
  66. self::Instance();
  67. if (is_array($key)) {
  68. $v = array();
  69. foreach($key as $k) {
  70. $vv = self::Get($k);
  71. if ($vv) {
  72. $v[$k] = $vv;
  73. }
  74. }
  75. return $v;
  76. } else {
  77. if (isset(self::$gCache[$key])) {
  78. return self::$gCache[$key];
  79. }
  80. $v = self::$mCache->get($key);
  81. if ($v) {
  82. self::$gCache[$key] = $v;
  83. }
  84. return $v;
  85. }
  86. }
  87. static function Add($key, $var, $flag = 0, $expire = 0)
  88. {
  89. self::Instance();
  90. self::$mCache->add($key, $var, $flag, $expire);
  91. self::$gCache[$key] = $var;
  92. }
  93. static function Dec($key, $value = 1)
  94. {
  95. self::Instance();
  96. return self::$mCache->decrement($key, $value);
  97. }
  98. static function Inc($key, $value = 1)
  99. {
  100. self::Instance();
  101. return self::$mCache->increment($key, $value);
  102. }
  103. static function Replace($key, $var, $flag = 0, $expire = 0)
  104. {
  105. self::Instance();
  106. return self::$mCache->replace($key, $var, $flag, $expire);
  107. }
  108. static function Set($key, $var, $flag = 0, $expire = 0)
  109. {
  110. self::Instance();
  111. self::$mCache->set($key, $var, $flag, $expire);
  112. self::$gCache[$key] = $var;
  113. return true;
  114. }
  115. static function Del($key, $timeout = 0)
  116. {
  117. self::Instance();
  118. if (is_array($key)) {
  119. foreach ($key as $k) {
  120. self::$mCache->delete($k, $timeout);
  121. if (isset(self::$gCache[$k])) unset(self::$gCache[$k]);
  122. }
  123. } else {
  124. self::$mCache->delete($key, $timeout);
  125. if (isset(self::$gCache[$key])) unset(self::$gCache[$k]);
  126. }
  127. return true;
  128. }
  129. static function Flush()
  130. {
  131. self::Instance();
  132. return self::$mCache->flush();
  133. }
  134. static function GetFunctionKey($callback, $args = array())
  135. {
  136. $args = ksort($args);
  137. $patt = "/(=>)\s*'(\d+)'/";
  138. $args_string = var_export($args, true);
  139. $args_string = preg_replace($patt, "\\1\\2", $args_string);
  140. $key = "[FUNC]:$callback($args_string)";
  141. return self::GenKey($key);
  142. }
  143. static function GetStringKey($str = null)
  144. {
  145. settype($str, 'array');
  146. $str = var_export($str, true);
  147. $key = "[STR]:{$str}";
  148. return self::GenKey($key);
  149. }
  150. static function GetObjectKey($tablename, $id)
  151. {
  152. $key = "[OBJ]:$tablename($id)";
  153. return self::GenKey($key);
  154. }
  155. static function GenKey($key)
  156. {
  157. $hash = dirname(__FILE__);
  158. return md5($hash . $key);
  159. }
  160. static function SetObject($tablename, $one)
  161. {
  162. self::Instance();
  163. foreach($one AS $oone) {
  164. $k = self::GetObjectKey($tablename, $oone['id']);
  165. self::Set($k, $oone);
  166. }
  167. return true;
  168. }
  169. static function GetObject($tablename, $id)
  170. {
  171. $single = ! is_array($id);
  172. settype($id, 'array');
  173. $k = array();
  174. foreach($id AS $oid) {
  175. $k[] = self::GetObjectKey($tablename, $oid);
  176. }
  177. $r = Utility::AssColumn(self::Get($k), 'id');
  178. return $single ? array_pop($r) : $r;
  179. }
  180. static function ClearObject($tablename, $id)
  181. {
  182. settype($id, 'array');
  183. foreach($id AS $oid) {
  184. $key = self::GetObjectKey($tablename, $oid);
  185. self::Del($key);
  186. }
  187. return true;
  188. }
  189. }