Full Page Cache

Implementation of High Performance Full page cache as html file and serves from apache server directly as html file without communicating to phalcon framework or php 


Logic Implementation is this

We are caching the whole page for 2 days using phalcon frontend and backend file caching and then we are directly configuring to get the cached html files using htaccess file until the cache expires, if the cache expries the file will be deleted, so if file is not exist it will be redirected to phalcon's index.php and create the cache file again and for the next time the page will be rendered from html cached files. 

Append the following code in public/index.php

 

try{

 $application = new \Phalcon\Mvc\Application();
 $application->setDI($di);

//// File caching logic starts here... 
	 
// Cache the file for 2 days
 $frontendOptions = [
     'lifetime' => 172800
 ];


 $frontCache = new \Phalcon\Cache\Frontend\Output($frontendOptions);

// Code to create the cache files in url structure

$folder = explode('/',$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
$folder1 = $folder[1];
$count = count($folder);
$file = end($folder);
reset($folder);
$key = $count - 1;
unset($folder[$key]);
$path = implode('/',$folder);
if(!is_dir('cache/'.$path)) $value = mkdir('cache/'.$path, 0777, true);

 // Create an output cache

 // Set the cache directory
 $backendOptions = [
     'cacheDir' => 'cache/'.$path.'/' ,
 ];

 // Create the File backend
 $cache = new \Phalcon\Cache\Backend\File($frontCache, $backendOptions);

if($folder1 == "" || $file == "")  $file = "index"; 
$content = $cache->start($file.".html");  
 
 
 if ($content === null) {
     echo '<h1>', time(), '</h1>';
	echo $application->handle()->getContent(); 
	 
     $cache->save();
 } else {
 
     echo $content;
	 
 }
	
//// File caching logic ends here... 
   
	
	
	
	
} catch (Phalcon\Exception $e) {
    echo $e->getMessage();
} catch (PDOException $e) {
    echo $e->getMessage();
}

And create a folder "cache" in public folder give 777 permissions 

edit the .htaccess file at root folder and add these lines

 

AddDefaultCharset UTF-8

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^$ public/     [L]
    RewriteRule (.*) public/cache/%{HTTP_HOST}/$1.html [L]
</IfModule>


Code is here "https://github.com/karthikeyan-manivasagam/Phalcon-FullPageCache"