PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/cache.php

https://github.com/MilkZoft/zan
PHP | 380 lines | 380 code | 0 blank | 0 comment | 1 complexity | 66e853f432b98f539db524f3a7d5c839 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. if (!defined("ACCESS")) {
  3. die("Error: You don't have permission to access here...");
  4. }
  5. class ZP_Cache extends ZP_Load
  6. {
  7. private $file = null;
  8. private $filename = null;
  9. private $filePath = null;
  10. private $groupPath = null;
  11. private $status = CACHE_STATUS;
  12. private $maxIter = 100;
  13. private function checkExpiration($expirationTime)
  14. {
  15. return (time() < $expirationTime) ? true : false;
  16. }
  17. private function checkIntegrity($readHash, $serializedData)
  18. {
  19. return ($readHash === sha1($serializedData)) ? true : false;
  20. }
  21. private function checkUpdating($updateTime)
  22. {
  23. return (time() < $updateTime) ? true : false;
  24. }
  25. public function data($ID, $group = "default", $Class = false, $method = false, $params = array(), $time = CACHE_TIME)
  26. {
  27. if (strtolower(CACHE_DRIVER) == "file") {
  28. if (CACHE_STATUS and $this->get($ID, $group)) {
  29. $data = $this->get($ID, $group);
  30. if (!$data) {
  31. if (!$Class or !$method) {
  32. return false;
  33. }
  34. $data = ($Class) ? call_user_func_array(array($Class, $method), is_array($params) ? $params : array()) : false;
  35. if (CACHE_STATUS and $data) {
  36. $this->save($data, $ID, $group, $time);
  37. }
  38. }
  39. } else {
  40. if (!$Class or !$method) {
  41. return false;
  42. }
  43. $data = ($Class) ? call_user_func_array(array($Class, $method), is_array($params) ? $params : array()) : false;
  44. if (CACHE_STATUS and $data) {
  45. $this->save($data, $ID, $group, $time);
  46. }
  47. }
  48. } elseif (strtolower(CACHE_DRIVER) == "apc") {
  49. if (CACHE_STATUS and $this->get($ID, $group)) {
  50. $data = $this->get($ID, $group);
  51. if (!$data) {
  52. if (!$Class or !$method) {
  53. return false;
  54. }
  55. $data = ($Class) ? call_user_func_array(array($Class, $method), is_array($params) ? $params : array()) : false;
  56. if (CACHE_STATUS and $data) {
  57. $this->save($data, $ID, $group, $time);
  58. }
  59. }
  60. } else {
  61. if (!$Class or !$method) {
  62. return false;
  63. }
  64. $data = ($Class) ? call_user_func_array(array($Class, $method), is_array($params) ? $params : array()) : false;
  65. if (CACHE_STATUS and $data) {
  66. $this->save($data, $ID, $group, $time);
  67. }
  68. }
  69. }
  70. return $data;
  71. }
  72. private function delete($dir)
  73. {
  74. if (empty($dir)) {
  75. return false;
  76. }
  77. if (!file_exists($dir)) {
  78. return true;
  79. }
  80. if (!is_dir($dir) or is_link($dir)) {
  81. $this->deleteFile($dir);
  82. }
  83. foreach (scandir($dir) as $item) {
  84. if ($item === "." or $item === "..") {
  85. continue;
  86. }
  87. if (is_dir($dir . $item)) {
  88. $this->delete($dir . $item . SH);
  89. } else {
  90. $this->deleteFile($dir . $item);
  91. }
  92. }
  93. return @rmdir($dir);
  94. }
  95. private function deleteFile($filename, $iteration = 0)
  96. {
  97. if (is_file($filename) or is_link($filename)) {
  98. @unlink($filename);
  99. if (file_exists($filename) and $iteration < $this->maxIter) {
  100. $this->deleteFile($filename, ++$iteration);
  101. }
  102. }
  103. }
  104. public function get($ID, $groupID = "default")
  105. {
  106. if (strtolower(CACHE_DRIVER) == "file") {
  107. if ($this->status) {
  108. $this->setFileRoutes($ID, $groupID);
  109. if (!file_exists($this->file)) {
  110. return false;
  111. }
  112. $meta = @file_get_contents($this->filePath . $this->filename);
  113. $meta = @unserialize($meta);
  114. $checkExpiration = $this->checkExpiration($meta["expiration_time"]);
  115. $checkIntegrity = $this->checkIntegrity($meta["integrity"], $meta["data"]);
  116. if ($checkExpiration and $checkIntegrity) {
  117. return @unserialize($meta["data"]);
  118. } else {
  119. return $this->remove($ID, $groupID);
  120. }
  121. }
  122. return false;
  123. } elseif (strtolower(CACHE_DRIVER) == "apc") {
  124. if ($data = apc_fetch($ID ."-". $groupID)) {
  125. return $data;
  126. }
  127. return false;
  128. }
  129. }
  130. private function getKey($ID)
  131. {
  132. return sha1($ID);
  133. }
  134. public function getStatus()
  135. {
  136. return $this->status;
  137. }
  138. public function remove($ID, $groupID = "default", $groupLevel = false)
  139. {
  140. if (strtolower(CACHE_DRIVER) == "file") {
  141. $this->setFileRoutes($ID, $groupID);
  142. if ($groupLevel and $groupID !== "default") {
  143. if (!$this->groupPath or empty($this->groupPath)) {
  144. return false;
  145. }
  146. return $this->delete($this->groupPath);
  147. } elseif ($this->filePath and !empty($this->filePath)) {
  148. return $this->delete($this->filePath);
  149. } else {
  150. return false;
  151. }
  152. } elseif (strtolower(CACHE_DRIVER) == "file") {
  153. apc_delete($ID ."-". $groupID);
  154. }
  155. }
  156. public function removeAll($groupID = "default")
  157. {
  158. if (strtolower(CACHE_DRIVER) == "file") {
  159. $this->delete(CACHE_DIR . SH . $this->getKey($groupID) . SH);
  160. } elseif (strtolower(CACHE_DRIVER) == "apc") {
  161. apc_clear_cache("user");
  162. }
  163. }
  164. public function save($data, $ID, $groupID = "default", $time = CACHE_TIME)
  165. {
  166. if (strtolower(CACHE_DRIVER) == "file") {
  167. if ($this->status) {
  168. $this->setFileRoutes($ID, $groupID);
  169. if (!is_dir($this->filePath)) {
  170. if (!mkdir($this->filePath, 0777, true)) {
  171. return false;
  172. }
  173. }
  174. if (!is_array($data) and !is_object($data)) {
  175. $data = array($data);
  176. }
  177. $data = serialize($data);
  178. $hash = sha1($data);
  179. $meta["expiration_time"] = time() + $time;
  180. $meta["integrity"] = $hash;
  181. $meta["data"] = $data;
  182. return @file_put_contents($this->file, serialize($meta), LOCK_EX);
  183. }
  184. return false;
  185. } elseif (strtolower(CACHE_DRIVER) == "apc") {
  186. if ($this->status) {
  187. if (!is_array($data) and !is_object($data)) {
  188. $data = array($data);
  189. }
  190. return apc_store($ID ."-". $groupID, $data, CACHE_TIME);
  191. }
  192. return false;
  193. }
  194. }
  195. private function setFileRoutes($ID, $groupID)
  196. {
  197. $keyName = $this->getKey($ID);
  198. $keyGroup = $this->getKey($groupID);
  199. $levelOne = $keyGroup;
  200. $levelTwo = substr($keyName, 0, 5);
  201. $this->groupPath = CACHE_DIR . SH . $levelOne . SH;
  202. $this->filePath = CACHE_DIR . SH . $levelOne . SH . $levelTwo . SH;
  203. $this->filename = $keyName . CACHE_EXT;
  204. $this->file = $this->filePath . $this->filename;
  205. }
  206. public function setStatus($status)
  207. {
  208. $this->status = $status;
  209. }
  210. public function getValue($ID, $table = "default", $field = "default", $default = false)
  211. {
  212. $data = $this->getValues($table, $field);
  213. if (is_array($data) and isset($data[$ID])) {
  214. return $data[$ID];
  215. }
  216. if ($default === true or !$this->status) {
  217. $this->Db = $this->db();
  218. $data = $this->Db->find($ID, $table, $field);
  219. if (isset($data[0][$field])) {
  220. return $data[0][$field];
  221. }
  222. }
  223. return $default;
  224. }
  225. public function getValues($table = "default", $field = "default")
  226. {
  227. if ($this->status) {
  228. $meta = $this->getMetaValue($table, $field);
  229. return unserialize($meta["data"]);
  230. }
  231. return false;
  232. }
  233. public function getMetaValue($table = "default", $field = "default")
  234. {
  235. if ($this->status) {
  236. $this->setValueFileRoutes($table, $field);
  237. if ($content = @file_get_contents($this->file)) {
  238. $meta = unserialize($content);
  239. if ($this->checkIntegrity($meta["integrity"], $meta["data"])) {
  240. if (!$this->checkUpdating($meta["update_time"])) {
  241. $meta["update_time"] = false;
  242. $this->Db = $this->db();
  243. foreach (unserialize($meta["data"]) as $ID => $value) {
  244. $this->Db->update($table, array($field => $value), $ID);
  245. }
  246. $this->deleteFile($this->file);
  247. }
  248. return $meta;
  249. }
  250. }
  251. }
  252. return false;
  253. }
  254. public function setValue($ID, $value, $table = "default", $field = "default", $update = false)
  255. {
  256. if ($this->status) {
  257. $this->setValueFileRoutes($table, $field);
  258. if (!is_dir($this->filePath)) {
  259. if (!mkdir($this->filePath, 0777, true)) {
  260. return false;
  261. }
  262. }
  263. $meta = $this->getMetaValue($table, $field);
  264. if (!$meta) {
  265. $meta = array();
  266. $data[$ID] = $value;
  267. $data = serialize($data);
  268. $hash = sha1($data);
  269. if ($update !== false) {
  270. $meta["update_time"] = time() + $update;
  271. } else {
  272. $meta["update_time"] = false;
  273. }
  274. } else {
  275. $data = unserialize($meta["data"]);
  276. $update_time = $meta["update_time"];
  277. $data[$ID] = $value;
  278. $data = serialize($data);
  279. $hash = sha1($data);
  280. if ($update !== false and $update_time === false) {
  281. $meta["update_time"] = time() + $update;
  282. }
  283. }
  284. $meta["integrity"] = $hash;
  285. $meta["data"] = $data;
  286. $data = serialize($meta);
  287. @file_put_contents($this->file, $data, LOCK_EX);
  288. } elseif ($update !== false) {
  289. $this->Db = $this->db();
  290. $this->Db->update($table, array($field => $value), $ID);
  291. }
  292. return $value;
  293. }
  294. public function setValueFileRoutes($table, $field)
  295. {
  296. $keyTable = $this->getKey($table);
  297. $keyField = $this->getKey($field);
  298. $dirValue = CACHE_DIR . SH ."values";
  299. $this->filePath = $dirValue . SH . $keyTable . SH;
  300. $this->filename = $keyField . CACHE_EXT;
  301. $this->file = $this->filePath . $this->filename;
  302. }
  303. }