Skip to content
Incredibly simple & small php caching system
Find file
New pull request
Fetching latest commit...
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



A very simple caching tool for php 5.3+ .

Basic Usage

Say you have some code:

echo complex_function_which_takes_ages($product_name, $vars);

all you do is wrap this in the omnicache function:

echo omnicache('product-'.$product_name, function()use($product_name, $vars){
    complex_function_which_takes_ages($product_name, $vars)});

and it'll all just work. Well. Almost. You need to set the global value: $cache_dir to somewhere where you have write access.

The first argument of omnicache is the unique 'key' for this cache object. In the example above, it's the 'product-' followed by the product name.

This means you can easily look at your cache dir and find / delete objects are no longer valid. If you update a product template, you can then remove all 'product-*' cache files.

More Advanced Usage

Internally, OMNIcache is a class, the omnicache function is just a wrapper for this:

$temp = new OMNIcache($key);
return $temp->cache($callback);

the OMNIcache class also has the following functions:

  • exists() -> Bool checks if the current cache file exists.
  • get() -> String returns the current cache (if it exists)
  • write($contents) writes $contents to the cache file
  • clear() remove the cache file from the filesystem
  • cache($callback) returns the cachefile if it exists, otherwise runs the callback and writes it to the cachefile.

Yes, this is very simple. By keeping it this simple, there should be less room for error.

You can use this wrapping system very easily in your own classes too:

class MyClass {
    function getComplexStuff($product, $vars){
            function () use ($product, $vars) {
                $this->getComplexStuff($product, $vars); });
    function getComplexStuffNoCache($product, $vars){
        // ... do slow complex stuff ...

(you could probably shorten/simplify this with some combination of get_args and user_func stuff.)


There isn't a whole load of error checking done here. There aren't calls to check if the cache dir is writable, and it won't print pretty error messages if you've got permissions errors. I figured if you want something cacheable, that usually means you want it to happen quickly, and those sorts of errors should be caught by some kind of permissions/sanity check script globally for the whole project.

One thing we could do would be insert a few try/catches around so in event of file errors, it'll just continue normally just without caching. I think this would be the best solution. try/catch doesn't have too big a performance hit in php, I believe. However, on my system here it works quite well without these extra calls, and just throws up PHP Warnings (which should be hidden in a production env. anyway, and if debugging, I'd rather not hide).

Any ideas/comments/suggestions are very welcome!

Something went wrong with that request. Please try again.