登录  /  注册
PHP缓存技术实现_PHP教程
php中文网
发布: 2016-07-13 10:38:31
原创
547人浏览过

发个PHP缓存实现,实现了apc和文件缓存,继承Cache_Abstract即可实现调用第三方的缓存工具。
参考shindig的缓存类和apc。
Php代码

  1. class CacheException extends Exception {}
  2. /**
  3. * 缓存抽象类
  4. */
  5. abstract class Cache_Abstract {
  6. /**
  7. * 读缓存变量
  8. *
  9. * @param string $key 缓存下标
  10. * @return mixed
  11. */
  12. abstract public function fetch($key);
  13. /**
  14. * 缓存变量
  15. *
  16. * @param string $key 缓存变量下标
  17. * @param string $value 缓存变量的值
  18. * @return bool
  19. */
  20. abstract public function store($key, $value);
  21. /**
  22. * 删除缓存变量
  23. *
  24. * @param string $key 缓存下标
  25. * @return Cache_Abstract
  26. */
  27. abstract public function delete($key);
  28. /**
  29. * 清(删)除所有缓存
  30. *
  31. * @return Cache_Abstract
  32. */
  33. abstract public function clear();
  34. /**
  35. * 锁定缓存变量
  36. *
  37. * @param string $key 缓存下标
  38. * @return Cache_Abstract
  39. */
  40. abstract public function lock($key);
  41. /**
  42. * 缓存变量解锁
  43. *
  44. * @param string $key 缓存下标
  45. * @return Cache_Abstract
  46. */
  47. abstract public function unlock($key);
  48. /**
  49. * 取得缓存变量是否被锁定
  50. *
  51. * @param string $key 缓存下标
  52. * @return bool
  53. */
  54. abstract public function isLocked($key);
  55. /**
  56. * 确保不是锁定状态
  57. * 最多做$tries次睡眠等待解锁,超时则跳过并解锁
  58. *
  59. * @param string $key 缓存下标
  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)); // 最多做十次睡眠等待解锁,超时则跳过并解锁
  71. $this->isLocked($key) && $this->unlock($key);
  72. return $this;
  73. }
  74. }
  75. /**
  76. * APC扩展缓存实现
  77. *
  78. *
  79. * @category Mjie
  80. * @package Cache
  81. * @author 流水孟春
  82. * @copyright Copyright (c) 2008-
  83. * @license New BSD License
  84. * @version $Id: Cache/Apc.php 版本号 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. * 保存缓存变量
  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. * 读取缓存
  105. *
  106. * @param string $key
  107. * @return mixed
  108. */
  109. public function fetch($key) {
  110. return apc_fetch($this->_storageKey($key));
  111. }
  112. /**
  113. * 清除缓存
  114. *
  115. * @return Cache_Apc
  116. */
  117. public function clear() {
  118. apc_clear_cache();
  119. return $this;
  120. }
  121. /**
  122. * 删除缓存单元
  123. *
  124. * @return Cache_Apc
  125. */
  126. public function delete($key) {
  127. apc_delete($this->_storageKey($key));
  128. return $this;
  129. }
  130. /**
  131. * 缓存单元是否被锁定
  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. * 锁定缓存单元
  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. * 缓存单元解锁
  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. * 完整缓存名
  164. *
  165. * @param string $key
  166. * @return string
  167. */
  168. private function _storageKey($key) {
  169. return $this->_prefix . '_' . $key;
  170. }
  171. }
  172. /**
  173. * 文件缓存实现
  174. *
  175. *
  176. * @category Mjie
  177. * @package Cache
  178. * @author 流水孟春
  179. * @copyright Copyright (c) 2008-
  180. * @license New BSD License
  181. * @version $Id: Cache/File.php 版本号 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. * 获取缓存文件
  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. * 读取缓存变量
  203. * 为防止信息泄露,缓存文件格式为php文件,并以""开头
  204. *
  205. * @param string $key 缓存下标
  206. * @return mixed
  207. */
  208. public function fetch($key) {
  209. $cacheFile = self::_getCacheFile($key);
  210. if (file_exists($cacheFile) && is_readable($cacheFile)) {
  211. // include 方式
  212. //return include $cacheFile;
  213. // 系列化方式
  214. return unserialize(@file_get_contents($cacheFile, false, NULL, 13));
  215. }
  216. return false;
  217. }
  218. /**
  219. * 缓存变量
  220. * 为防止信息泄露,缓存文件格式为php文件,并以""开头
  221. *
  222. * @param string $key 缓存变量下标
  223. * @param string $value 缓存变量的值
  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. // 用include方式
  235. //return @file_put_contents($cacheFile, '
  236. return @file_put_contents($cacheFile, '' . serialize($value));
  237. }
  238. /**
  239. * 删除缓存变量
  240. *
  241. * @param string $key 缓存下标
  242. * @return Cache_File
  243. */
  244. public function delete($key) {
  245. if(emptyempty($key)) {
  246. throw new CacheException("Missing argument 1 for Cache_File::delete()");
  247. }
  248. $cacheFile = self::_getCacheFile($key);
  249. if(!@unlink($cacheFile)) {
  250. throw new CacheException("Cache file could not be deleted");
  251. }
  252. return $this;
  253. }
  254. /**
  255. * 缓存单元是否已经锁定
  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. * 锁定
  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. // 设定缓存锁文件的访问和修改时间
  282. @touch($cacheFile . '.lock');
  283. return $this;
  284. }
  285. /**
  286. * 解锁
  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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/735086.htmlTechArticle发个PHP缓存实现,实现了apc和文件缓存,继承Cache_Abstract即可实现调用第三方的缓存工具。 参考shindig的缓存类和apc。 Php代码 ?php classCacheE...

来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 技术文章
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

 | 本站CDN由 数掘科技 提供

登录PHP中文网,和优秀的人一起学习!
全站2000+教程免费学