PHP code example of corollarium / cachearium

1. Go to this page and download the library: Download corollarium/cachearium library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

corollarium / cachearium example snippets



$cache::$debugOnPage = true;

...
if (!$cache->start($key)) {
	// some stuff
	$cache->end();
}
...

// this is 


$cache->setLog(true);
....
$cache->report(); // will print a detailed report

$data = 'xxxx';

// store
$cache = CacheAbstract::factory('your backend');
$cache->store($data, new CacheKey('Namespace', 'Subname'));

// get it later
try { 
	$data2 = $cache->get(new CacheKey('Namespace', 'Subname'));
	// $data2 == 'xxxx';
}
catch (NotCachedException($e)) {
	// handle not cached
}

// store new value with automatic invalidation
$data = 'yyy';
$cache->store($data, new CacheKey('Namespace', 'Subname'));

$data = 'xxxx';

// store
$cache = CacheAbstract::factory('your backend');
$cache->storeData(new CacheData(new CacheKey('Namespace', 'Subname'), $data));

// get it later
try { 
	$data2 = $cache->getData(new CacheKey('Namespace', 'Subname'));
	// $data2->getFirstData() == 'xxxxx'
}
catch (NotCachedException($e)) {
	// handle not cached
}

// store new value with automatic invalidation
$lifeTime = 3000;
$fancyData = 'someData';
$cache->storeData(new CacheData(new CacheKey('Namespace', 'Subname'), $fancyData), $lifeTime);

$cache = CacheAbstract::factory('your backend');

// create a storage key and bucket
$key = new CacheKey('Namespace', 'Subname');
$cd = new CacheData($key, $data);

// add dependencies. setDependencies will generate immediately, avoiding races.
// otherwise you find results, the DB changes in another process and you get a
// stale dependency. note that addDependencies does not do that, leaving the
// hash to be calculated later
$dep = new CacheKey('Namespace', 'SomeDep');
$cd->setDependencies([$dep]);

// store.
$data = 'xxxx';
$cache->storeData($cd);

// at this point $cache->get($key) will return your data

// invalidate a dependency. This will be called on your write method.
$cache->invalidate($dep);

// at this point $cache->get($key) will throw an NotCachedException

function someSearch() {
	$key = new CacheKey('search', 'someSearch'); // keys for this data
	$cache = CacheAbstract::factory('backend');
	try {
		return $cache->get($key); // TODO
	}
	catch (NotCachedException($e)) {
		// handle not cached below
	}

	$searchdata = getSomeData(); // results of some horrible slow query

	// attributes that are used in this search
	$dependencies = [
		new CacheKey('attribute', 'name'), 
		new CacheKey('attribute', 'description')
		new CacheKey('attribute', 'cost')
	];
	
	// create cache data
	$cd = 
	$cache->storeData(
		(new CacheData($key, $searchdata))
		->setDependencies($dependencies);
	);
	
	return $searchdata;
}

function writeSomeStuff() {
	// changed or added some attribute value in some object

	$cache = CacheAbstract::factory('backend');
	$cache->invalidate(new CacheKey('attribute', 'name')); // invalidates any cache that depends on this key
}

class Foo extends Model {
	use Cached;
	
	/**
	 * Unique object id in your application (primary key)
	 */
	public function getId() {
		return $this->id;	
	}
	
	public function cacheClean() {
		$cache = CacheAbstract::factory('backend');
		$cache->clean('Foo', $this->getId());
	}
	
	public function save() {
		// save stuff on db
		$this->cacheClean(); // clear any cache associated with this item
	}

	public function cacheStore($data, $key) {
		$cache = CacheAbstract::factory('backend');
		return $cache->save($data, 'Foo', $this->getId(), $key);
	}

	public function cacheGet($key) {
		$cache = CacheAbstract::factory('backend');
		return $cache->get('Foo', $this->getId(), $key);
	}
}


	$cache = CacheAbstract::factory('your backend');
	
	$cache->start(new CacheKey('main'));
	
		$cache->start(new CacheKey('header'));
		$cache->end();
		
		foreach ($somestuff as $stuff) {
			$stuff->render();
		}
	
		$cache->start(new CacheKey('footer'));
			
		$cache->end();
	$cache->end();
	
	class Stuff {
		public function getCacheKey() {
			return new CacheKey('stuff', $this->getId());
		}
		
		public function write() {
			write_some_stuff();
	
			$cache = CacheAbstract::factory('your backend');
			$cache->clean($this->getCacheKey());
		}
	
		public function render() {
			$cache = CacheAbstract::factory('your backend');
			$cache->start($stuff->getCacheKey()->setSub('render'));
	
			$html = '<p>some html here</p>';
	
			// other dependency if you have it
			$cache->addDependency($otherstuff->getCacheKey()); 
			
			$cache->end();
		}
	}


	function callbackTesterStart() {
		return rand();
	}

	$key = new CacheKey("startcallback", 1);
	$cache->start($key);
	echo "something ";
	
	// this will never be cached, every call to start() will use the rest
	// of the cached data and call the callback everytime for the variable data 
	$cache->appendCallback('callbackTesterStart');
	
	// everything goes on as planned here
	echo " otherthing";
	$output = $cache->end(false);

function application_cacheDependencies() {
	// can return an array or a string
	return [Application::getLanguage(), somethingelse()];
}