搜索
首页后端开发php教程php中apc和文件缓存类的实现代码

  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 isLocked($key)); // 最多做十次睡眠等待解锁,超时则跳过并解锁
  71. $this->isLocked($key) && $this->unlock($key);
  72. return $this;
  73. }
  74. }
  75. /**

  76. * APC扩展缓存实现
  77. *
  78. * @by bbs.it-home.org
  79. * @category Mjie
  80. * @package Cache
  81. * @license New BSD License
  82. * @version $Id: Cache/Apc.php 版本号 2010-04-18 23:02 cmpan $
  83. */
  84. class Cache_Apc extends Cache_Abstract {
  85. protected $_prefix = 'cache.mjie.net';

  86. public function __construct() {

  87. if (!function_exists('apc_cache_info')) {
  88. throw new CacheException('apc extension didn\'t installed');
  89. }
  90. }
  91. /**

  92. * 保存缓存变量
  93. *
  94. * @param string $key
  95. * @param mixed $value
  96. * @return bool
  97. */
  98. public function store($key, $value) {
  99. return apc_store($this->_storageKey($key), $value);
  100. }
  101. /**

  102. * 读取缓存
  103. *
  104. * @param string $key
  105. * @return mixed
  106. */
  107. public function fetch($key) {
  108. return apc_fetch($this->_storageKey($key));
  109. }
  110. /**

  111. * 清除缓存
  112. *
  113. * @return Cache_Apc
  114. */
  115. public function clear() {
  116. apc_clear_cache();
  117. return $this;
  118. }
  119. /**

  120. * 删除缓存单元
  121. *
  122. * @return Cache_Apc
  123. */
  124. public function delete($key) {
  125. apc_delete($this->_storageKey($key));
  126. return $this;
  127. }
  128. /**

  129. * 缓存单元是否被锁定
  130. *
  131. * @param string $key
  132. * @return bool
  133. */
  134. public function isLocked($key) {
  135. if ((apc_fetch($this->_storageKey($key) . '.lock')) === false) {
  136. return false;
  137. }
  138. return true;
  139. }
  140. /**

  141. * 锁定缓存单元
  142. *
  143. * @param string $key
  144. * @return Cache_Apc
  145. */
  146. public function lock($key) {
  147. apc_store($this->_storageKey($key) . '.lock', '', 5);
  148. return $this;
  149. }
  150. /**

  151. * 缓存单元解锁
  152. *
  153. * @param string $key
  154. * @return Cache_Apc
  155. */
  156. public function unlock($key) {
  157. apc_delete($this->_storageKey($key) . '.lock');
  158. return $this;
  159. }
  160. /**

  161. * 完整缓存名
  162. *
  163. * @param string $key
  164. * @return string
  165. */
  166. private function _storageKey($key) {
  167. return $this->_prefix . '_' . $key;
  168. }
  169. }
  170. /**
  171. * 文件缓存实现
  172. *
  173. * @by bbs.it-home.org
  174. * @category Mjie
  175. * @package Cache
  176. * @license New BSD License
  177. * @version $Id: Cache/File.php 版本号 2010-04-18 16:46 cmpan $
  178. */
  179. class Cache_File extends Cache_Abstract {
  180. protected $_cachesDir = 'cache';

  181. public function __construct() {

  182. if (defined('DATA_DIR')) {
  183. $this->_setCacheDir(DATA_DIR . '/cache');
  184. }
  185. }
  186. /**

  187. * 获取缓存文件
  188. *
  189. * @param string $key
  190. * @return string
  191. */
  192. protected function _getCacheFile($key) {
  193. return $this->_cachesDir . '/' . substr($key, 0, 2) . '/' . $key . '.php';
  194. }
  195. /**
  196. * 读取缓存变量
  197. * 为防止信息泄露,缓存文件格式为php文件,并以""开头
  198. *
  199. * @param string $key 缓存下标
  200. * @return mixed
  201. */
  202. public function fetch($key) {
  203. $cacheFile = self::_getCacheFile($key);
  204. if (file_exists($cacheFile) && is_readable($cacheFile)) {
  205. return unserialize(@file_get_contents($cacheFile, false, NULL, 13));
  206. }
  207. return false;
  208. }
  209. /**
  210. * 缓存变量
  211. * 为防止信息泄露,缓存文件格式为php文件,并以""开头
  212. *
  213. * @param string $key 缓存变量下标
  214. * @param string $value 缓存变量的值
  215. * @return bool
  216. */
  217. public function store($key, $value) {
  218. $cacheFile = self::_getCacheFile($key);
  219. $cacheDir = dirname($cacheFile);
  220. if(!is_dir($cacheDir)) {
  221. if([url=mailto:!@mkdir($cacheDir]!@mkdir($cacheDir[/url], 0755, true)) {
  222. throw new CacheException("Could not make cache directory");
  223. }
  224. }
  225. return @file_put_contents($cacheFile, '' . serialize($value));
  226. }
  227. /**
  228. * 删除缓存变量
  229. *
  230. * @param string $key 缓存下标
  231. * @return Cache_File
  232. */
  233. public function delete($key) {
  234. if(empty($key)) {
  235. throw new CacheException("Missing argument 1 for Cache_File::delete()");
  236. }
  237. $cacheFile = self::_getCacheFile($key);
  238. if([url=mailto:!@unlink($cacheFile]!@unlink($cacheFile[/url])) {
  239. throw new CacheException("Cache file could not be deleted");
  240. }
  241. return $this;
  242. }
  243. /**
  244. * 缓存单元是否已经锁定
  245. *
  246. * @param string $key
  247. * @return bool
  248. */
  249. public function isLocked($key) {
  250. $cacheFile = self::_getCacheFile($key);
  251. clearstatcache();
  252. return file_exists($cacheFile . '.lock');
  253. }
  254. /**
  255. * 锁定
  256. *
  257. * @param string $key
  258. * @return Cache_File
  259. */
  260. public function lock($key) {
  261. $cacheFile = self::_getCacheFile($key);
  262. $cacheDir = dirname($cacheFile);
  263. if(!is_dir($cacheDir)) {
  264. if([url=mailto:!@mkdir($cacheDir]!@mkdir($cacheDir[/url], 0755, true)) {
  265. if(!is_dir($cacheDir)) {
  266. throw new CacheException("Could not make cache directory");
  267. }
  268. }
  269. }
  270. // 设定缓存锁文件的访问和修改时间
  271. @touch($cacheFile . '.lock');
  272. return $this;
  273. }
  274. /**
  275. * 解锁
  276. *
  277. * @param string $key
  278. * @return Cache_File
  279. */
  280. public function unlock($key) {
  281. $cacheFile = self::_getCacheFile($key);
  282. @unlink($cacheFile . '.lock');
  283. return $this;
  284. }
  285. /**
  286. * 设置文件缓存目录
  287. * @param string $dir
  288. * @return Cache_File
  289. */
  290. protected function _setCacheDir($dir) {
  291. $this->_cachesDir = rtrim(str_replace('\\', '/', trim($dir)), '/');
  292. clearstatcache();
  293. if(!is_dir($this->_cachesDir)) {
  294. mkdir($this->_cachesDir, 0755, true);
  295. }
  296. //
  297. return $this;
  298. }
  299. /**
  300. * 清空所有缓存
  301. *
  302. * @return Cache_File
  303. */
  304. public function clear() {
  305. // 遍历目录清除缓存
  306. $cacheDir = $this->_cachesDir;
  307. $d = dir($cacheDir);
  308. while(false !== ($entry = $d->read())) {
  309. if('.' == $entry[0]) {
  310. continue;
  311. }
  312. $cacheEntry = $cacheDir . '/' . $entry;
  313. if(is_file($cacheEntry)) {
  314. @unlink($cacheEntry);
  315. } elseif(is_dir($cacheEntry)) {
  316. // 缓存文件夹有两级
  317. $d2 = dir($cacheEntry);
  318. while(false !== ($entry = $d2->read())) {
  319. if('.' == $entry[0]) {
  320. continue;
  321. }
  322. $cacheEntry .= '/' . $entry;
  323. if(is_file($cacheEntry)) {
  324. @unlink($cacheEntry);
  325. }
  326. }
  327. $d2->close();
  328. }
  329. }
  330. $d->close();
  331. return $this;
  332. }
  333. }
  334. /**
  335. * 缓存单元的数据结构
  336. * array(
  337. * 'time' => time(), // 缓存写入时的时间戳
  338. * 'expire' => $expire, // 缓存过期时间
  339. * 'valid' => true, // 缓存是否有效
  340. * 'data' => $value // 缓存的值
  341. * );
  342. */
  343. final class Cache {
  344. /**
  345. * 缓存过期时间长度(s)
  346. *
  347. * @var int
  348. */
  349. private $_expire = 3600;
  350. /**
  351. * 缓存处理类
  352. *
  353. * @var Cache_Abstract
  354. */
  355. private $_storage = null;
  356. /**
  357. * @return Cache
  358. */
  359. static public function createCache($cacheClass = 'Cache_File') {
  360. return new self($cacheClass);
  361. }
  362. private function __construct($cacheClass) {
  363. $this->_storage = new $cacheClass();
  364. }
  365. /**
  366. * 设置缓存
  367. *
  368. * @param string $key
  369. * @param mixed $value
  370. * @param int $expire
  371. */
  372. public function set($key, $value, $expire = false) {
  373. if (!$expire) {
  374. $expire = $this->_expire;
  375. }
  376. $this->_storage->checkLock($key);
  377. $data = array('time' => time(), 'expire' => $expire, 'valid' => true, 'data' => $value);
  378. $this->_storage->lock($key);
  379. try {
  380. $this->_storage->store($key, $data);
  381. $this->_storage->unlock($key);
  382. } catch (CacheException $e) {
  383. $this->_storage->unlock($key);
  384. throw $e;
  385. }
  386. }
  387. /**
  388. * 读取缓存
  389. *
  390. * @param string $key
  391. * @return mixed
  392. */
  393. public function get($key) {
  394. $data = $this->fetch($key);
  395. if ($data && $data['valid'] && !$data['isExpired']) {
  396. return $data['data'];
  397. }
  398. return false;
  399. }
  400. /**
  401. * 读缓存,包括过期的和无效的,取得完整的存贮结构
  402. *
  403. * @param string $key
  404. */
  405. public function fetch($key) {
  406. $this->_storage->checkLock($key);
  407. $data = $this->_storage->fetch($key);
  408. if ($data) {
  409. $data['isExpired'] = (time() - $data['time']) > $data['expire'] ? true : false;
  410. return $data;
  411. }
  412. return false;
  413. }
  414. /**
  415. * 删除缓存
  416. *
  417. * @param string $key
  418. */
  419. public function delete($key) {
  420. $this->_storage->checkLock($key)
  421. ->lock($key)
  422. ->delete($key)
  423. ->unlock($key);
  424. }
  425. public function clear() {

  426. $this->_storage->clear();
  427. }
  428. /**
  429. * 把缓存设为无效
  430. *
  431. * @param string $key
  432. */
  433. public function setInvalidate($key) {
  434. $this->_storage->checkLock($key)
  435. ->lock($key);
  436. try {
  437. $data = $this->_storage->fetch($key);
  438. if ($data) {
  439. $data['valid'] = false;
  440. $this->_storage->store($key, $data);
  441. }
  442. $this->_storage->unlock($key);
  443. } catch (CacheException $e) {
  444. $this->_storage->unlock($key);
  445. throw $e;
  446. }
  447. }
  448. /**

  449. * 设置缓存过期时间(s)
  450. *
  451. * @param int $expire
  452. */
  453. public function setExpire($expire) {
  454. $this->_expire = (int) $expire;
  455. return $this;
  456. }
  457. }
  458. ?>
复制代码


声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
PHP与Python:了解差异PHP与Python:了解差异Apr 11, 2025 am 12:15 AM

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

php:死亡还是简单地适应?php:死亡还是简单地适应?Apr 11, 2025 am 12:13 AM

PHP不是在消亡,而是在不断适应和进化。1)PHP从1994年起经历多次版本迭代,适应新技术趋势。2)目前广泛应用于电子商务、内容管理系统等领域。3)PHP8引入JIT编译器等功能,提升性能和现代化。4)使用OPcache和遵循PSR-12标准可优化性能和代码质量。

PHP的未来:改编和创新PHP的未来:改编和创新Apr 11, 2025 am 12:01 AM

PHP的未来将通过适应新技术趋势和引入创新特性来实现:1)适应云计算、容器化和微服务架构,支持Docker和Kubernetes;2)引入JIT编译器和枚举类型,提升性能和数据处理效率;3)持续优化性能和推广最佳实践。

您什么时候使用特质与PHP中的抽象类或接口?您什么时候使用特质与PHP中的抽象类或接口?Apr 10, 2025 am 09:39 AM

在PHP中,trait适用于需要方法复用但不适合使用继承的情况。1)trait允许在类中复用方法,避免多重继承复杂性。2)使用trait时需注意方法冲突,可通过insteadof和as关键字解决。3)应避免过度使用trait,保持其单一职责,以优化性能和提高代码可维护性。

什么是依赖性注入容器(DIC),为什么在PHP中使用一个?什么是依赖性注入容器(DIC),为什么在PHP中使用一个?Apr 10, 2025 am 09:38 AM

依赖注入容器(DIC)是一种管理和提供对象依赖关系的工具,用于PHP项目中。DIC的主要好处包括:1.解耦,使组件独立,代码易维护和测试;2.灵活性,易替换或修改依赖关系;3.可测试性,方便注入mock对象进行单元测试。

与常规PHP阵列相比,解释SPL SplfixedArray及其性能特征。与常规PHP阵列相比,解释SPL SplfixedArray及其性能特征。Apr 10, 2025 am 09:37 AM

SplFixedArray在PHP中是一种固定大小的数组,适用于需要高性能和低内存使用量的场景。1)它在创建时需指定大小,避免动态调整带来的开销。2)基于C语言数组,直接操作内存,访问速度快。3)适合大规模数据处理和内存敏感环境,但需谨慎使用,因其大小固定。

PHP如何安全地上载文件?PHP如何安全地上载文件?Apr 10, 2025 am 09:37 AM

PHP通过$\_FILES变量处理文件上传,确保安全性的方法包括:1.检查上传错误,2.验证文件类型和大小,3.防止文件覆盖,4.移动文件到永久存储位置。

什么是无效的合并操作员(??)和无效分配运算符(?? =)?什么是无效的合并操作员(??)和无效分配运算符(?? =)?Apr 10, 2025 am 09:33 AM

JavaScript中处理空值可以使用NullCoalescingOperator(??)和NullCoalescingAssignmentOperator(??=)。1.??返回第一个非null或非undefined的操作数。2.??=将变量赋值为右操作数的值,但前提是该变量为null或undefined。这些操作符简化了代码逻辑,提高了可读性和性能。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中