Due to its powerful and scalability, PHP has been greatly developed in recent years. Compared with traditional ASP websites, PHP has an absolute speed advantage, if you want mssql to convert 60 thousand pieces of data to PHP, it will take 40 seconds and ASP will take less than 2 minutes. however, due to the increasing number of website data, we are eager to call data more quickly. We do not need to drop data from the database every time. We can choose from other places, such as a file, or a memory address, which is the Cache Technology of PHP.
In-depth analysis
In general, the purpose of caching is to put data in one place to make access faster. There is no doubt that the memory is the fastest, but can hundreds of MB of data be stored in the memory? This is not realistic. Of course, sometimes it is temporarily stored as a server cache, for example, if the ob_start () cache page is enabled, the page content will be cached in the memory before the file header is sent, knowing that the page output will be clear automatically or wait for the ob_get_contents to return, or be cleared by the ob_end_clean display, which can be used well in the generation of static pages and can be well reflected in the template.
In addition, there is an object application in ASP that can save common parameters. This is also a bit of cache. However, in PHP, I have not seen developers generate such objects yet. It is indeed unnecessary. ASP. the NET page cache technology uses viewstate, while the cache is File Association (not necessarily accurate), the file is modified, and the cache is updated, if the file is not modified and does not Time Out (note 1), read the cache and return the result. This is the idea. Let's look at the source code:
- <? PHP
- Class cache {
- Private $ cache_dir;
- Private $ expireTime = 180; // The cache time is 60 seconds.
- Function _ construct ($ cache_dirname ){
- If (! @ Is_dir ($ cache_dirname )){
- If (! @ Mkdir ($ cache _ dirname, 0777 )){
- $ This-> warn (the cache file does not exist and cannot be created. You need to create it manually .);
- Return false;
- }
- }
- $ This-> cache_dir = $ cache_dirname;
- }
- Function _ destruct (){
- Echo Cache class bye .;
- }
- Function get_url (){
- If (! Isset ($ _ SERVER [REQUEST_URI]) {
- $ Url = $ _ SERVER [REQUEST_URI];
- } Else {
- $ Url = $ _ SERVER [SCRIPT_NAME];
- $ Url. = (! Emptyempty ($ _ SERVER [QUERY_STRING])? ? . $ _ SERVER [QUERY_STRING]:;
- }
- Return $ url;
- }
- Function warn ($ errorstring ){
- Echo"<B> <fontColor = red>Error:<Pre>". $ Errorstring ."</Pre> </font> </B>";
- }
-
- Function cache_page ($ pageurl, $ pagedata ){
- If (! $ Fso = fopen ($ pageurl, w )){
- $ This->Warns (the cached file cannot be opened.); // trigger_error
- Return false;
- }
- If (! Flock ($ fso, LOCK_EX) {// LOCK_NB, locking
- $ This->Warns (the cache file cannot be locked.); // trigger_error
- Return false;
- }
- If (! Fwrite ($ fso, $ pagedata) {// write byte stream, serialize writes to other formats
- $ This->Warns (the cache file cannot be written.); // trigger_error
- Return false;
- }
- Flock ($ fso, LOCK_UN); // release lock
- Fclose ($ fso );
- Return true;