Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

May the cache be with you

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 Source
Octocat-spinner-32 Specs
Octocat-spinner-32 LICENSE
Octocat-spinner-32 README.md
Octocat-spinner-32 namespace
README.md

PHP-Cache (PHP 5.3)

Abstract base library to cache any data to any backend. Uses file system as a fall back and for 'persistent' data.

Specs

  • Run the "run" script in the Specs directory

Example

use Cache\Cache;

$c = new Cache('/path/to/file/cache', array(
    'prefix' => 'MyAppPrefix'
));

$c->store('anyKey', $anyData);
$c->retrieve('anyKey'); // returns $anyData !
$c->erase('anyKey');

Actual Use-Case Example

$data = $c->retrieve('data');
if (!$data){
    $data = doSomeExpensiveCalculation();
    $c->store('data', $data);
}

$data; // Has expensive data!

Or, with PHP 5.3 Style

$data = $c->retrieve('myKey', function($c, $key){
    return $c->store($key, doSomeExpensiveCalculation());
});

The above function is only being executed if there is no associated data to 'myKey'. If a function gets passed like this, the 'retrieve' method returns whatever the passed in function returns. As a convenience the 'store' method returns the input (= second argument).

Here There Be Tags

$c->store('a', 'banana banana banana', array(
    'tags' => 'bananas'
));

$c->store('b', 'apple apple banana', array(
    'tags' => array('apples', 'bananas')
));

$c->eraseByTag('bananas');

$c->retrieve('a'); // null

See the Source or Specs for more

Something went wrong with that request. Please try again.