1. Start
  2. Features
  3. Explanation
  4. Implementation
  5. Support
  6. Resources

PHP Simple Cache

Speedup websites and save server resources


PHP Simple Cache lets you create cache for any PHP website quickly and easily. Use this script to speed up your website.

Features


PHP Simple Cache Features

Explanation


PHP is a programming language to create dynamic websites. Dynamic websites display content that is requested by user. This content changes if user submits form, logs in, sends email, submits comment, add/retrieve data in/from database etc. PHP scripts also retrieve data from database and display it on a dynamic page. Every time such page is loaded in browser, PHP server has to translate PHP script, request database server, retrieve data and then display it for user.

Cache reduces server's effort. Cache stores a translated version of page (HTML) to display it later. When this page is called again, cache verifies its cached version. If cached copy of page is available and is not expired, it will be displayed and PHP server won't have to translate PHP scripts and connect to database server to retrieve data. This process reduces server memory usage, server effort snd server resources consumption.

If some kind of dynamic action is performed, old cache is deleted and a new fresh cached copy of page is created for later use. In this way, you website remains up and running at fast speed. A cached version of page is much faster than original dynamic page.

It is logical and based on rule that "Only go through PHP scripts and database if content of page changes. If content is same, store its copy, do not go through PHP scripts and database tables and display cached copy."

PHP Simple Cache is a small PHP accelerator that substantially increases the speed of PHP website and improves the web page generation process by server. Basically it cahces a page that has been loaded in browser once. Next time cached HTML version of PHP page loads instead of telling PHP server to translate the PHP code again. Similarly, in case of data retrival from database server, it will cache that data and store it in cached file to display it later, instead of connecting to database again.

It enables dynamic PHP pages to load quickly and reduces server load by saving server translated page into cached files and serving that saved data on demand.

Implementation


Quick Start

Include script file (cache.php) at the top of every page in your website that you want to be cached and that's it. If not every page, you can also include this script in database file, configuration file or settings file of your project to cache whole website. Turn off cache for individual pages by passing a single variable to the page you do not want to be cached. If passing a variable or submitting a form, include another single script file to empty cache, so that dynamic functionality of website remains intact.

In short, cache all PHP website, but when performaing a dynamic action on any individual page, refresh cache to ensure site's stable functionality and optimal performance at the same time.

Use Cache

Grab cache.php file. Open it in editor, adjust 'cache folder' settings (name and location), adjust cache time and include cache.php in website. That's it.

By default settings, you need to create siteCache folder at root of your website. Place cache.php file in root of website and you are good to go after including cache.php file in your project, like below:

include("cache.php");
//.. rest of page code ..//

 

Turn Cache Off for Individual Page

$nocache="on";
//.. rest of page code ..//

OR

$nocache="on";
include("cache.php");
//.. rest of page code ..//


 

Delete Cache

If you perform any dynamic task/action at website, or session data changes, or user logs in etc. you will need to delete cache to see changes or updates.

include("empty-cache.php");


Delete Current Single Page Cache

Include delete-single.php at the top of page before you include cache.php to delete cache for the single current page page. example-cache-on.php uses this functionality. After posting variables or submitting form, cache for single pages clears and we tell script to do not cache that one time. On next page reload, cache is created again. You have to implement in between any kind of check, that if form is submitted or variables are posted, then clear cache for that page and do not create cache for that time.

// delete only current cached page
include("delete-single.php");

// include cache file to create current page cache again
include("cache.php");
// Rest of the page code goes here

inlucde delete-single.php page where you want to empty cache for current page. For one time, Make sure delete-single.php use same cache path and file path as defined in cache.php.

 

Cache Folder Path

Its defined in cache.php file. Change siteCache name and path in cache.php file, according to your need.

Use absolute path for cache folder. This will work while you use modrewrite for URL to create pretty URLs. Relative path will not work locally (wamp, localhost). Relative path will also not work for pretty URLs.

// absolute path - recommended
define('CACHE_PATH', $_SERVER["DOCUMENT_ROOT"]."/siteCache/");
// while $_SERVER["DOCUMENT_ROOT"] is your root directory path at your web host .. e.g. http://www.mywebsite.com
// If your project and the cache folder are not at the root path of server, e.g. http://www.mywebsite.com/project/siteCache/
// Then cache folder path will be like below
// define('CACHE_PATH', $_SERVER["DOCUMENT_ROOT"]."/project/siteCache/");


// relative path - not recommended
define('CACHE_PATH', "siteCache/");

If your cache is not working, make sure you have defined absolute path in cache.php for siteCache folder to store cached files.

Details

Mainly this project uses one file (cache.php) and one folder (siteCache). When you include cache.php file at top of page, it creates cache file for that page and stores it in siteCache folder.

Additionally, use empty-cache.php, to delete cache if needed. Or use delete-single.php, to delete current page cache.

Downloaded file contains:

cache.php
delete-single.php
empty-cache.php
example-cache-off.php
example-cache-on.php
example-cached-page.php
example-empty-cache.php
index.php

bootstrap folder
code folder
documentation folder
siteCache folder

You need cache.php, delete-single.php, empty-cache.php and siteCache folder. Get it from 'downloaded folder root' or from folder named as code. Copy these in your project. Include cache.php file at top of every page of your project and you are done.

example-cache-on.php, example-cache-off.php and example-empty-cache.php also explain how to use cache, turn it off or delete cache.

Cache Folder

siteCache is the folder that will store cached pages
This folder is defined in cache.php and empty-cache.php file

Cache Settings

Open cache.php to

Change cache folder name and path if required (use absolute path)
Define cache time in hours

How to Cache a Page

Include cache.php at top page like below:
 

include("cache.php");
//.. rest of page code ..//


How to Not Cache a Page (Exclude Page from Caching)

Include this code at top of page that you do not want to be cached
 

$nocache="on";
//.. rest of page code ..//

OR

$nocache="on";
include("cache.php");
//.. rest of page code ..//


or simply do not include cache.php file in that page.

How to Cache Whole Website

Include cache.php file in db file, settings file, configuration or session file of your project.
Any file that is being used in every page of project can include cache.php, to enable full website caching.
 

include("cache.php");
//.. rest of page code ..//


How to Use Cache Script with DB file and Sessions

You have to start session at the top of page while working with sessions.
After session start, include cache.php file
DB file can go after that.

Include this code at top of every page

<?php session_start();
include("cache.php");
include("db-file.php");
?>


Where db-file.php can be sessions.php or config.php according to your project settings and need.

Delete Cache Files

Include empty-cache.php at the point where you want to delete the cached files.

example:

<?php include("empty-cache.php"); ?>

When you submit forms or store data in sessions,
first delete cache files by including empty-cache.php at the top of page or anywhere and
then store or retrieve data and then display the page that is being cached to show fresh version of page.

If you find trouble in implementing this script, Go to online demo of this project or support or contact me. Ask me anything related to this script and I will be glad to help.

Support


PHP Simple Cache Home

Online Demo / Updates / Change Log/ Quick Start Guide

FAQs

Support / Ask

http://techmynd.org/
http://www.phpmagicbook.com/

Resources


This project has used bootstrap farmework for webpage styling.

Icons Used

http://www.iconfinder.com/icondetails/28664/256/lightning_power_weather_icon
http://www.iconfinder.com/icondetails/63467/128/database_storage_icon