自己用的PHP缓存类,自己PHP缓存类_PHP教程
自己用的PHP缓存类,自己PHP缓存类
<?php /** * 缓存类,实现数据,输出缓存 * @author ZhouHr 2012-11-09 http://www.ketann.com * @copyright version 0.1 */ class Cache { private static $_instance; protected $_cacheId = null; const CLEANING_MODE_ALL = 'all'; const CLEANING_MODE_OLD = 'old'; protected $_options = array( 'cache_dir' => null, //数据缓存目录 'life_time' => 7200, //缓存时间 'page_dir' => null, //文本缓存目录 'cache_prefix' => 'cache_' //缓存前缀 ); private function __construct(){} //创建__clone方法防止对象被复制克隆 private function __clone(){} /** * 取缓存对象,如果存在直接返回,如果不存在实例化本身 * @return object cache */ public static function getInstance(){ if(! self::$_instance){ self::$_instance = new self(); } return self::$_instance; } /** * 设置缓存参数集 * @param array $options 要设置的缓存参数集 */ public function setOptions($options = array()){ while (list($name, $value) = each($options)) { $this->setOption($name, $value); } } /** * 取得当前缓存参数,如果$name为空返回全部参数,否则返回该参数值 * @param string $name 要返回的参数名称 * @return string or array $option; */ public function getOption($name = null){ if(null === $name) return $this->_options; if (!is_string($name)) { throwException("不正确的参数名称 : $name"); } if (array_key_exists($name, $this->_options)){ return $this->_options[$name]; } } /** * 设置缓存参数 * @param array $options 要设置的缓存参数 */ protected function setOption($name, $value){ if (!is_string($name)) { throwException("不正确的参数名称 : $name"); } $name = strtolower($name); if (array_key_exists($name, $this->getOption())){ $this->_options[$name] = $value; } if ($this->_options['cache_dir'] === null) { $this->setOption('cache_dir', $this->getTmpDir() . DIRECTORY_SEPARATOR); } if ($this->_options['page_dir'] === null) { $this->setOption('page_dir', $this->getTmpDir() . DIRECTORY_SEPARATOR); } } /** * 读取数据缓存,如果不存在或过期,返回false * @param string $id 缓存ID * @return false or data */ public function load($id){ $this->_cacheId = $id; $file = $this->getOption('cache_dir') . $this->getOption('cache_prefix') . $this->_cacheId; if (@filemtime($file) >= time()){ return unserialize(file_get_contents($file)); } else { @unlink($file); return false; } } /** * 保存数据缓存,并设置缓存过期时间 * @param array or string $data 要缓存的数据 * @param int $lifeTime 缓存过期时间 */ public function save($data, $lifeTime = null){ if(null !== $lifeTime) $this->setOption('life_time', $lifeTime); $file = $this->getOption('cache_dir') . $this->getOption('cache_prefix') . $this->_cacheId; $data = serialize($data); @file_put_contents($file, $data); @chmod($file, 0777); @touch($file, time() + $this->getOption('life_time'])); } /** * 读取输出缓存,如果不存在或缓存过期将重新开启输出缓存 * @param string $id 缓存ID */ public function start($id){ $this->_cacheId = $id; $file = $this->getOption('page_dir') . $this->getOption('cache_prefix') . $this->_cacheId; if (@filemtime($file) >= time()){ return file_get_contents($file); } else { @unlink($file); ob_start(); return false; } } /** * 删除指定ID缓存 * @param string $id 缓存ID */ public function remove($id){ $this->_cacheId = $id; //删除附合条件的数据缓存 $file = $this->getOption('cache_dir') . $this->getOption('cache_prefix') . $this->_cacheId; @unlink($file); //删除附合条件的输出缓存 $file = $this->getOption('page_dir') . $this->getOption('cache_prefix') . $this->_cacheId; @unlink($file); } /** * 保存输出缓存,并设置缓存过期时间 * @param int $lifeTime 缓存过期时间 */ public function end($lifeTime = null){ if(null !== $lifeTime) $this->setOption('life_time', $lifeTime); $file = $this->getOption('page_dir') . $this->getOption('cache_prefix') . $this->_cacheId; $data = ob_get_contents(); ob_end_clean(); @file_put_contents($file, $data); @chmod($file, 0777); @touch($file, time() + $this->getOption('life_time'])); } /** * 根据参数清除相应缓存 * @param string $mode 缓存类型,包括(CLEANING_MODE_ALL:所有缓存, CLEANING_MODE_OLD: 过期缓存) */ public function clear($mode = CLEANING_MODE_OLD){ $dirs = array('cache_dir', 'page_dir'); foreach($dirs as $value){ if(null != $this->getOption($value)){ $files = scandir($this->getOption($value)); switch ($mode) { case CLEANING_MODE_ALL: default: foreach ($files as $val){ @unlink($this->getOption($value) . $val); } break; case CLEANING_MODE_OLD: default: foreach ($files as $val){ if (filemtime($this->getOption($value) . $val) < time()){ @unlink($this->getOption($value) . $val); } } break; } } } } /** * 取临时文件夹为缓存文件夹 * @return $dir 临时文件夹路径 */ public function getTmpDir(){ $tmpdir = array(); foreach (array($_ENV, $_SERVER) as $tab) { foreach (array('TMPDIR', 'TEMP', 'TMP', 'windir', 'SystemRoot') as $key) { if (isset($tab[$key])) { if (($key == 'windir') or ($key == 'SystemRoot')) { $dir = realpath($tab[$key] . '\\temp'); } else { $dir = realpath($tab[$key]); } if ($this->_isGoodTmpDir($dir)) { return $dir; } } } } $upload = ini_get('upload_tmp_dir'); if ($upload) { $dir = realpath($upload); if ($this->_isGoodTmpDir($dir)) { return $dir; } } if (function_exists('sys_get_temp_dir')) { $dir = sys_get_temp_dir(); if ($this->_isGoodTmpDir($dir)) { return $dir; } } //通过尝试创建一个临时文件来检测 $tempFile = tempnam(md5(uniqid(rand(), TRUE)), ''); if ($tempFile) { $dir = realpath(dirname($tempFile)); unlink($tempFile); if ($this->_isGoodTmpDir($dir)) { return $dir; } } if ($this->_isGoodTmpDir('/tmp')) { return '/tmp'; } if ($this->_isGoodTmpDir('\\temp')) { return '\\temp'; } throw new Exception('无法确定临时目录,请手动指定cache_dir', E_USER_ERROR); } /** * 验证给定的临时目录是可读和可写的 * * @param string $dir 临时文件夹路径 * @return boolean true or false 临时文件夹路径是否可读写 */ protected function _isGoodTmpDir($dir){ if (is_readable($dir)) { if (is_writable($dir)) { return true; } } return false; } }//endclass

热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

CakePHP 是 PHP 的开源框架。它的目的是使应用程序的开发、部署和维护变得更加容易。 CakePHP 基于类似 MVC 的架构,功能强大且易于掌握。模型、视图和控制器 gu

Visual Studio Code,也称为 VS Code,是一个免费的源代码编辑器 - 或集成开发环境 (IDE) - 可用于所有主要操作系统。 VS Code 拥有针对多种编程语言的大量扩展,可以轻松编写

CakePHP 是一个开源MVC 框架。它使开发、部署和维护应用程序变得更加容易。 CakePHP 有许多库可以减少大多数常见任务的过载。

本教程演示了如何使用PHP有效地处理XML文档。 XML(可扩展的标记语言)是一种用于人类可读性和机器解析的多功能文本标记语言。它通常用于数据存储

JWT是一种基于JSON的开放标准,用于在各方之间安全地传输信息,主要用于身份验证和信息交换。1.JWT由Header、Payload和Signature三部分组成。2.JWT的工作原理包括生成JWT、验证JWT和解析Payload三个步骤。3.在PHP中使用JWT进行身份验证时,可以生成和验证JWT,并在高级用法中包含用户角色和权限信息。4.常见错误包括签名验证失败、令牌过期和Payload过大,调试技巧包括使用调试工具和日志记录。5.性能优化和最佳实践包括使用合适的签名算法、合理设置有效期、

字符串是由字符组成的序列,包括字母、数字和符号。本教程将学习如何使用不同的方法在PHP中计算给定字符串中元音的数量。英语中的元音是a、e、i、o、u,它们可以是大写或小写。 什么是元音? 元音是代表特定语音的字母字符。英语中共有五个元音,包括大写和小写: a, e, i, o, u 示例 1 输入:字符串 = "Tutorialspoint" 输出:6 解释 字符串 "Tutorialspoint" 中的元音是 u、o、i、a、o、i。总共有 6 个元
