<?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) 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
以上就介绍了二级缓存和三级缓存 自己用的PHP缓存类,包括了二级缓存和三级缓存方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

PHP在电子商务、内容管理系统和API开发中广泛应用。1)电子商务:用于购物车功能和支付处理。2)内容管理系统:用于动态内容生成和用户管理。3)API开发:用于RESTfulAPI开发和API安全性。通过性能优化和最佳实践,PHP应用的效率和可维护性得以提升。

PHP可以轻松创建互动网页内容。1)通过嵌入HTML动态生成内容,根据用户输入或数据库数据实时展示。2)处理表单提交并生成动态输出,确保使用htmlspecialchars防XSS。3)结合MySQL创建用户注册系统,使用password_hash和预处理语句增强安全性。掌握这些技巧将提升Web开发效率。

PHP和Python各有优势,选择依据项目需求。1.PHP适合web开发,尤其快速开发和维护网站。2.Python适用于数据科学、机器学习和人工智能,语法简洁,适合初学者。

PHP仍然具有活力,其在现代编程领域中依然占据重要地位。1)PHP的简单易学和强大社区支持使其在Web开发中广泛应用;2)其灵活性和稳定性使其在处理Web表单、数据库操作和文件处理等方面表现出色;3)PHP不断进化和优化,适用于初学者和经验丰富的开发者。

PHP在现代Web开发中仍然重要,尤其在内容管理和电子商务平台。1)PHP拥有丰富的生态系统和强大框架支持,如Laravel和Symfony。2)性能优化可通过OPcache和Nginx实现。3)PHP8.0引入JIT编译器,提升性能。4)云原生应用通过Docker和Kubernetes部署,提高灵活性和可扩展性。

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

PHP和Python各有优势,适合不同场景。1.PHP适用于web开发,提供内置web服务器和丰富函数库。2.Python适合数据科学和机器学习,语法简洁且有强大标准库。选择时应根据项目需求决定。

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

Dreamweaver CS6
视觉化网页开发工具

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

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

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。