PHP Metameric Cache

PHP Metameric Cache


UPDATED PHP Metameric Cache is a disk based cache mechanism that lets you create cache for selected parts of PHP page or PHP website quickly and easily. It does not depend on URL structure of your website. You can store each cached part with a unique name and then retrieve it or delete it on requirement.

This cache will create segmented cache for your web application for those parts that require heavy server processing. Use this to speed up your website, boost performance and loading time.

You will have to place two lines of code at starting point (where you want to start caching) and one line of code at ending point (where you want to end cache). Same code applies for as many sections as you want to cache. Only the name variable ‘value’ of cached file will be changed for each part (only once). Cache for a section of code, will be stored with its unique name that you specify.

If cache for any section of code has expired, on new cache creation for that section, it will be overwritten. You can delete the cache manually and selectively but normally you do not need it. You can delete all cache anytime.

This system is for those who do not want to cache whole pages. Select your code to cache, name it and put it under cache and forget it.


Recently, I made a dynamic PHP application that had a navigational menu. Elements in menu could display on header, footer, sidebar on admin based selection. The menu had a nice CSS for its styles. I implemented huge PHP checks on that menu to control placements and styles of the menu elements. Actually in the end I realized that these checks on-page-load every time, could slow down application. I needed some sort of caching to cache only that navigation and then few more sections of every page. This cache can solve such problems.


  • NEW: Set different expiry time for any or each cached result.
  • Create cache for specific parts of webpage or create full webpage cache.
  • Easy to integrate in and configure with any website
  • Lightweight script
  • Only connects to the database or go through PHP scripts when cache has expired or cache page does not exist
  • Set cache time easily
  • Delete selected part (file) from cache or all cache files
  • Reduce server load, processing, resources consumption and increase website loading speed making it responsive
  • Reduce database queries requests

This script is for those who do not like ‘full page caching’ or ‘full website caching’. Exclude login or user areas completely from caching. Take selected parts, from website or from PHP page – define their names and cahce those parts individually. This cache will get output from those parts and store them with their unique names in cache folder. Next time when the page loads, if those segments are stored in cache, will be displayed from cache and their real PHP code that generated those parts will not be executed. Real PHP code execution including database requests will be skipped and its already stored output will be displayed.

You can cache as many parts of a page or a website as you like. This gives you full control over your website content. You can decide from where, what and how much to cache. Its simple and easy to use.

Implementation / Usage

start cache
Code or Content no 1
end cache

start cache
Code or Content no 2
end cache

ans so on..

<?php $filename="desired-cache-file-name";
if($cached!='yes') { ?>

data or code to cache...

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

This will create a cache file named as ‘cache_desired-cache-file-name’ and store the data “data or code to cache…” in it.

Repeat this step to cache more data on same page or different pages.

Change Log

– 28 March 2013

  • Added custom ‘cache time validity’ option. You can set different expiry time for each cached section of your website. At the same page, different sections can have different expiry time for their cache.
  • Bug fix – After deleting single cached result, it was not being cached again – FIXED
  • Updated live demo, updated code, updated code hints, updated documentation.

If you want to create cache for full webpages, check ‘Simple PHP Cache’.


Author Image