PHP常用缓存方式入门教程
第一种,把需要缓存的数据进行处理,形成PHP可以直接执行的文件。在需要缓存数据
的时候,通过include方式引入,并使用。
第二种,把需要的数据通过serialize函数序列化后直接保存到文件。在需要使用缓存
数据的时候,通过反序列化读入文件内容并复制给需要的变量,然后使用。
测试结果:
通过测试我们发现,第二种也就是serialize缓存数据的方式更加高效。(数据略去,
最后提供了文章地址下载,大家可以自行测试)
原因分析:
include方式读取缓存的时候,PHP需要执行几个过程
1.读取文件
2.解析所Include的文件
3.执行,给变量赋值
而serialize序列化方式读取缓存的时候:
1.读取数据
2.反序列化数据内容
3.给变量赋值
从以上内容对比的话,可能是由于解析PHP文件内的数组需要的时间超过unserialize反
序列化数组的时间。如果你有兴趣可以查看《PHP filesystem相关函数和include
require的性能效率研究》:http://www.ccvita.com/163.html
测试文件代码:
下载地址:MooPHP-CacheTest.zip
原创地址:http://www.ccvita.com/311.html 有新的研究心得会在这里更新。
CacheTest_IncludeFile.php
CacheTest_SerializeFile.php
总结分析:
第一种,include缓存的方式
优点:增加数据的保密性,和安全性,缓存内容不会被外界发现。
缺点:速度相对较慢。
用途:保存禁止系统外部得知的数据,比如web系统的设置,甚至MySQL信息等的保存
第二种,serialize序列化缓存的方式
优点:速度较快。
缺点:缓存系统文件路径一点曝光,缓存内容会泄露。
用途:缓存最新文章,相关文章等不担心外部得知的数据的时候,可以使用这种方式。
备注:
当装了ea、apc等PHP内存缓存之后,第一种通过include读取缓存的方式速度会高于第
二种serialize序列化缓存的方式。所以在MooPHP框架中,我们对于非敏感信息采用第
二种方式缓存;敏感信息采用第一种方式缓存

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools