PHP Classes

AhCache: Store cached data in files

Recommend this page to a friend!
  Info   View files Documentation   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 174 All time: 8,787 This week: 455Up
Version License PHP version Categories
ahcache 2.3GNU Free Document...5.0PHP 5, Files and Folders, Cache


This class can store cached data in files.

It can check if a given cache file exists and it has not passed more than a given expiry time. The class checks the last modified timestamp to determine if the cache file is expired.

In that case the class can retrieve the cached data. Otherwise the class can store the cache data in a file for a given expiry period.

The cache directory is derived from the cache ID value. The class can create sub-directories to reduce the number of cache files per directory.

The class can also delete individual cache files, all cached files about a given age of one or all modules.

Picture of Axel Hahn
  Performance   Level  
Name: Axel Hahn <contact>
Classes: 5 packages by
Country: Switzerland Switzerland
Age: 52
All time rank: 267926 in Switzerland Switzerland
Week rank: 185 Up3 in Switzerland Switzerland Up
Innovation award
Innovation award
Nominee: 2x







2009-07-20 1.0 cache class on 2011-08-27 1.1 comments added; sCacheFile is private 2012-02-04 2.0 cache serialzable types; more methods, i.e.:

              - comparison of timestimp with a sourcefile
              - cleanup unused cachefiles

2012-05-15 2.1 isExpired() returns as bool; new method iExpired() to get

             expiration in sec

2012-05-15 2.2 - rename to AhCache

             - _cleanup checks with file_exists

2014-03-31 2.3 - added _setup() that to includes custom settings

             - limit number of files in cache directory

--- typical usage:

example using expiration (ttl value):

$iTtl=60*5; // 5 min 
$myCache=new AhCache("my-app","task-id");  
if($myCache->isExpired()) {  
    // cache does not exist or is expired
    // save cache
    $myCache->write($sContent, $iTtl);  
} else {  
    // read cached data
// output
echo $sContent;  

example compare age of cache with age of a sourcefile

$myCache=new AhCache("my-app","task-id");  
$sContent=$myCache->read(); // read cached data
// comparison of last modified time (mtime)
if (!$myCache->isNewerThanFile($sCsvFile)) {  
	// update content 
	// ... and save cache
// output
echo $sContent;

cleanup cache directory

$o=new AhCache("my-app");  
$o->cleanup(606024*1); // delete all Cachefiles of the module "my-app" older 1 day

or cleanup cachefiles of all modules
$o=new Cache(); $o->cleanup(606024*1);

  Files folder image Files  
File Role Description
Plain text file cache.class.php Class caching class
Accessible without login Plain text file cache.class_config.php Conf. config for finetuning class (leave untouched in the beginning)
Accessible without login Plain text file readme.txt Doc. Readme
Accessible without login HTML file simpledoc.html Doc. Class documentation

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:8,787
This week:455Up