PCacher - Statistical cache engine for PHP/MySQL web applications
What is it
Simply as the title says a script that collects page hits from a web application and then selects the pages that have the most hits and caches them
Compatibility and requirements
PHP 5 (CLI) and MySQL 4.1 and above
Installation instructions
Step 1.
Create the database table from the .sql file.
Step 2.
Take the code from the page_views_manager.php file and use it on every page you want it to count the hits.
You can either use it as it is and calling it like this: require_once('page_views_manager.php'); or
modify it to work with your code.
Basically what it does is store in the database table (created from step 1) the hits each page receives, which will be used later from the cache core code file to create the static web pages.
Notice that this code assumes an already open connection to MySQL. Currently it expects it to be named $conn.
You can change it to whatever you have named your database connection variable.
Step 3.
Create a directory for the cached files outside the public_html folder.
srv# mkdir cached_files
Create another directory for the code files again outside the public_html folder.
srv# mkdir cache_manager
Make sure permissions of the folders match the ones the engine will run as.
Step 4.
Drop the 2 core files into the cache_manager folder. cacher.php and class.db.php
File cacher.php is the cache engine itself and file class.db.php is a helper class for the database. You can of course switch it with yours. That
means you have to change the variable in line 5 of this file to reflect the path of your own database class file.
define(DB_CLASS_PATH, 'class.db.php');
Another variable to set at line 6 is the url of the site.
define(URL, 'http://www.mysite.com');
Also if you use the class.db.php file, remember to set your database connection data (username, password, database name). There are the first lines in the file.
Usage
How to run the script
Assuming the path to the php binary is /usr/local/php/bin/php and you are inside the cache_manager folder (Step 3) you can run the script as:
srv# /usr/local/php/bin/php -f cacher.php 100P /path/to/cached_files yes
What are these arguments?
The script needs 3 arguments in order to work.
Argument 1 sets the mode of the script. A number followed by the character P tells the script to cache that number of files.
A number followed by the character V tells the script to cache pages that have a view count bigger that this number.
Argument 2 is the full path to the folder where the static files are to be stored.
Argument 3 if its yes it will delete or previously cached files before creating the new ones. If no it will leave them intact and either overwrite them or create new ones.
How to call the static pages
You can add a code on top of your web application or on top of every page, that checks if the static file exists and call it or run the php code.
An example would be:
$cfolder = /path/to/cached/files; // path to cached files folder
$file = $cfolder.'/'.str_replace('/', '@', urldecode($_SERVER['REQUEST_URI']));
if (file_exists($file))
{
@require_once($file);
exit();
}
Normally if you are on a custom framework you won't use exit(); since you might want other code to execute at the end of the page like a performance time counter.
Security
Make sure at least you run php binary as root since the script uses the rm command to delete all files from a folder.
srv# chown root cacher.php
srv# chmod 600 cacher.php
