Page Cache to Avoid Using PHP Engine

{ Posted on Nov 20 2009 by admin }
Categories : PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
 
<?php
/**
* @Author: Rbernal
* @ 11-13-09
* @ page caching
*/
 
class PageCache{
 
	/**
	 * @ path where we save or cache files
	 */
	private $cache_path;
 
	/**
	 * @ page information basename and the dir
	 */
	private $page_info;
 
	/**
	 * @ file content to be saved on a as cache files
	 */
	private $page_content;
 
 
	/**
	 * @ file content to be saved on a as cache files
	 */
	public $use_cache = true;
 
 
	/**
	 * @ constructor
	 */
	public function __construct( $base_path = '/aopi/cache/teng.site88.com/' )
	{
		$this->cache_path = $base_path;
		$this->getPageInfo();
		$this->createDirectory();
	}
 
 
	/**
	 * @ set page information
	 */
	private function getPageInfo()
	{   
		$this-> page_info = pathinfo( $_SERVER['REQUEST_URI'] );
		$this-> page_info['dirname'] = substr($this-> page_info['dirname'],1).'/';
	}
 
 
	/**
	 * @ create directory if Directory does not exist
	 */
	private function createDirectory()
	{
		$this->cache_path = $this->cache_path.$this-> page_info['dirname'];
		//if( $this-> page_info['dirname'] != '' && !file_exists($this->cache_path)  )
		if( !file_exists($this->cache_path)  )
		{  
			mkdir($this->cache_path, 0777, true);
		}
 
	}
 
 
	/**
	 * @start reading the page for caching
	 */
	public function startCache()
	{
		ob_start();
	}
 
 
	/**
	 * @limit the output buffer and save
	 */
	public function endCache()
	{
		$this->page_content = ob_get_contents();
		$this->saveCache();
	}
 
 
	/**
	 * @remove output buffer
	 */
	private function clean()
	{
		ob_clean();
	}
 
 
	/**
	 * @save cache file if not exist
	 */
	private function saveCache()
	{
		$file = $this->cache_path.$this-> page_info['basename'];
		if( !file_exists($file) &&  $this -> use_cache )
		{  
		 	file_put_contents($file, $this->page_content);
		}
		//$this->clean();
 
	}
 
}
?>

and here’s the htaccess redirection snippet

1
2
RewriteCond %{DOCUMENT_ROOT}/cache/%{REQUEST_URI} -f
RewriteRule ^(.+) /cache/$1 [L]

sample implementation

1
2
3
4
5
6
7
8
9
10
if(PAGE_CACHE){
	require DOC_ROOT.'../lib/PageCache.php';
	$cache = new PageCache();
	// initialize and declare cache DIR
	$cache  -> startCache();
} 
//html here
if(PAGE_CACHE){
	$cache -> endCache();
}

using the codes above after the page has been cached it wont bother PHP it will call the cache file directly


5 Responses to “Page Cache to Avoid Using PHP Engine”

  1. Nice. :)

    But how long is the cache timeout before it will revalidate??

  2. not sure about your question remember that it is apache it self who finds the static file to be displayed and once it doest see any cache it will just ignore the rewrite condition and use normal way i mean use the original dynamic page

  3. Sir, how do you set the cache folder. Assuming its at the root level.
    Medyo naguguluhan po ako sa example: ‘/aopi/cache/teng.site88.com/’

    Thanks in advance

  4. Nice caching. So how do we expire the page? Are we going to have garbage collection or similar (cron, whatever?) that will delete those old files so that Apache will return the original dynamic page?

    I was always wondering how Wordpress do their page caching, maybe it was the same as what you’ve posted. Nice!

  5. actually the code is PHP5 that will recursively create folders
    sample i have /foot/sub1/sub2 if these folders does not exist then it will recursively create those.

    In WP they have this admin that delete cache i believe they are the same the only difference is that WP still uses PHP engine to locate the file.

    yes, There should be a cron or maybe manually delete them besides you wont refresh your cache monthly or weekly most probably when you have updates lets say layout or html tags then thats the time to delete them.

Post a Comment