PHP Cache Technology Implementation _php Tutorial

Source: Internet
Author: User
PHP cache implementation, the implementation of APC and file cache, inherited cache_abstract can be implemented to call third-party caching tools.
Refer to the Shindig cache class and APC.
PHP code
  1. Class Cacheexception extends Exception {}
  2. /**
  3. * Caching Abstract classes
  4. */
  5. Abstract class Cache_abstract {
  6. /**
  7. * Read Cache variables
  8. *
  9. * @param string $key cache subscript
  10. * @return Mixed
  11. */
  12. Abstract public Function fetch ($KEY);
  13. /**
  14. * Cache Variables
  15. *
  16. * @param string $key cache variable Subscript
  17. * @param string $value The value of the cache variable
  18. * @return BOOL
  19. */
  20. Abstract Public function Store ($key, $value);
  21. /**
  22. * Delete Cache variables
  23. *
  24. * @param string $key cache subscript
  25. * @return Cache_abstract
  26. */
  27. Abstract Public Function Delete ($key);
  28. /**
  29. * Clear (delete) except all caches
  30. *
  31. * @return Cache_abstract
  32. */
  33. Abstract public Function clear ();
  34. /**
  35. * Lock Cache variables
  36. *
  37. * @param string $key cache subscript
  38. * @return Cache_abstract
  39. */
  40. Abstract Public Function lock ($key);
  41. /**
  42. * Cache Variable Unlocked
  43. *
  44. * @param string $key cache subscript
  45. * @return Cache_abstract
  46. */
  47. Abstract public Function unlock ($key);
  48. /**
  49. * Gets whether the cache variable is locked
  50. *
  51. * @param string $key cache subscript
  52. * @return BOOL
  53. */
  54. Abstract public Function isLocked ($key);
  55. /**
  56. * Make sure not locked status
  57. * Up to $tries sleep waiting to be unlocked, timeout skipped and unlocked
  58. *
  59. * @param string $key cache subscript
  60. */
  61. Public Function Checklock ($key) {
  62. if (! $this->islocked ($key)) {
  63. return $this;
  64. }
  65. $tries = 10;
  66. $count = 0;
  67. do {
  68. Usleep (200);
  69. $count + +;
  70. } while ($count <= $tries && $this->islocked ($key)); Do up to 10 sleep waits to unlock, timeout to skip and unlock
  71. $this->islocked ($key) && $this->unlock ($key);
  72. return $this;
  73. }
  74. }
  75. /**
  76. * APC Extended Cache implementation
  77. *
  78. *
  79. * @category Mjie
  80. * @package Cache
  81. * @author Water Mengchun
  82. * @copyright Copyright (c) 2008-
  83. * @license New BSD License
  84. * @version $Id: cache/apc.php version number 2010-04-18 23:02 Cmpan $
  85. */
  86. Class Cache_apc extends Cache_abstract {
  87. protected $_prefix = ' cache.mjie.net ';
  88. Public Function __construct () {
  89. if (!function_exists (' Apc_cache_info ')) {
  90. throw new Cacheexception (' APC extension didn\ ' t installed ');
  91. }
  92. }
  93. /**
  94. * Save Cache variables
  95. *
  96. * @param string $key
  97. * @param mixed $value
  98. * @return BOOL
  99. */
  100. Public function Store ($key, $value) {
  101. Return Apc_store ($this->_storagekey ($key), $value);
  102. }
  103. /**
  104. * Read Cache
  105. *
  106. * @param string $key
  107. * @return Mixed
  108. */
  109. Public function Fetch ($key) {
  110. Return Apc_fetch ($this->_storagekey ($key));
  111. }
  112. /**
  113. * Clear Cache
  114. *
  115. * @return CACHE_APC
  116. */
  117. Public Function Clear () {
  118. Apc_clear_cache ();
  119. return $this;
  120. }
  121. /**
  122. * Delete Cache unit
  123. *
  124. * @return CACHE_APC
  125. */
  126. Public Function Delete ($key) {
  127. Apc_delete ($this->_storagekey ($key));
  128. return $this;
  129. }
  130. /**
  131. * Whether the cache unit is locked
  132. *
  133. * @param string $key
  134. * @return BOOL
  135. */
  136. Public Function isLocked ($key) {
  137. if (Apc_fetch ($this->_storagekey ($key). '. Lock ')) = = = = False) {
  138. return false;
  139. }
  140. return true;
  141. }
  142. /**
  143. * Lock Cache Unit
  144. *
  145. * @param string $key
  146. * @return CACHE_APC
  147. */
  148. Public function Lock ($key) {
  149. Apc_store ($this->_storagekey ($key). '. Lock ', ', 5 ';
  150. return $this;
  151. }
  152. /**
  153. * Cache Unit Unlocked
  154. *
  155. * @param string $key
  156. * @return CACHE_APC
  157. */
  158. Public function Unlock ($key) {
  159. Apc_delete ($this->_storagekey ($key). '. Lock ');
  160. return $this;
  161. }
  162. /**
  163. * Full Cache Name
  164. *
  165. * @param string $key
  166. * @return String
  167. */
  168. Private Function _storagekey ($key) {
  169. Return $this->_prefix. '_' . $key;
  170. }
  171. }
  172. /**
  173. * File Cache implementation
  174. *
  175. *
  176. * @category Mjie
  177. * @package Cache
  178. * @author Water Mengchun
  179. * @copyright Copyright (c) 2008-
  180. * @license New BSD License
  181. * @version $Id: cache/file.php version number 2010-04-18 16:46 Cmpan $
  182. */
  183. Class Cache_file extends Cache_abstract {
  184. Public $useSubdir = false;
  185. Protected $_cachesdir = ' cache ';
  186. Public Function __construct () {
  187. if (defined (' Data_dir ')) {
  188. $this->_setcachedir (Data_dir. '/cache ');
  189. }
  190. }
  191. /**
  192. * Get cache files
  193. *
  194. * @param string $key
  195. * @return String
  196. */
  197. protected function _getcachefile ($key) {
  198. $subdir = $this->usesubdir? substr ($key, 0, 2). '/' : '';
  199. Return $this->_cachesdir. '/' . $subdir. $key. '. php ';
  200. }
  201. /**
  202. * Read Cache variables
  203. * To prevent information disclosure, the cache file format is PHP file and begins with " "
  204. *
  205. * @param string $key cache subscript
  206. * @return Mixed
  207. */
  208. Public function Fetch ($key) {
  209. $cacheFile = Self::_getcachefile ($key);
  210. if (file_exists ($cacheFile) && is_readable ($cacheFile)) {
  211. Include method
  212. return include $cacheFile;
  213. Serialization method
  214. Return Unserialize (@file_get_contents ($cacheFile, False, NULL, 13));
  215. }
  216. return false;
  217. }
  218. /**
  219. * Cache Variables
  220. * To prevent information disclosure, the cache file format is PHP file and begins with " "
  221. *
  222. * @param string $key cache variable Subscript
  223. * @param string $value The value of the cache variable
  224. * @return BOOL
  225. */
  226. Public function Store ($key, $value) {
  227. $cacheFile = Self::_getcachefile ($key);
  228. $cacheDir = DirName ($cacheFile);
  229. if (!is_dir ($cacheDir)) {
  230. if (! @mkdir ($cacheDir, 0755, True)) {
  231. throw new Cacheexception ("Could not make cache directory");
  232. }
  233. }
  234. Use the Include method
  235. Return @file_put_contents ($cacheFile, '
  236. Return @file_put_contents ($cacheFile, " . Serialize ($value));
  237. }
  238. /**
  239. * Delete Cache variables
  240. *
  241. * @param string $key cache subscript
  242. * @return Cache_file
  243. */
  244. Public Function Delete ($key) {
  245. if (Emptyempty ($key)) {
  246. throw new Cacheexception ("Missing argument 1 for Cache_file::d elete ()");
  247. }
  248. $cacheFile = Self::_getcachefile ($key);
  249. if (! @unlink ($cacheFile)) {
  250. throw new Cacheexception ("Cache file could not being deleted");
  251. }
  252. return $this;
  253. }
  254. /**
  255. * Whether the cache unit is locked
  256. *
  257. * @param string $key
  258. * @return BOOL
  259. */
  260. Public Function isLocked ($key) {
  261. $cacheFile = Self::_getcachefile ($key);
  262. Clearstatcache ();
  263. Return File_exists ($cacheFile. '. Lock ');
  264. }
  265. /**
  266. * Lock
  267. *
  268. * @param string $key
  269. * @return Cache_file
  270. */
  271. Public function Lock ($key) {
  272. $cacheFile = Self::_getcachefile ($key);
  273. $cacheDir = DirName ($cacheFile);
  274. if (!is_dir ($cacheDir)) {
  275. if (! @mkdir ($cacheDir, 0755, True)) {
  276. if (!is_dir ($cacheDir)) {
  277. throw new Cacheexception ("Could not make cache directory");
  278. }
  279. }
  280. }
  281. Setting access and modification times for cache lock files
  282. @touch ($cacheFile. '. Lock ');
  283. return $this;
  284. }
  285. /**
  286. * Unlock
  287. *
  288. * @param string $key
  289. * @return Cache_file
  290. */
  291. Public function Unlock ($key) {
  292. $cacheFile = Self::_getcachefile ($key);
  293. @unlink ($cacheFile. '. Lock ');
  294. Return

http://www.bkjia.com/PHPjc/735086.html www.bkjia.com true http://www.bkjia.com/PHPjc/735086.html techarticle PHP Cache Implementation, the implementation of APC and file cache, inherited cache_abstract can be implemented to call third-party caching tools. Refer to the Shindig cache class and APC. PHP code? PHP Classcachee ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.