Skip to content

Instantly share code, notes, and snippets.

@OndrejSlamecka
Created January 3, 2012 18:37
Show Gist options
  • Save OndrejSlamecka/1556225 to your computer and use it in GitHub Desktop.
Save OndrejSlamecka/1556225 to your computer and use it in GitHub Desktop.
Class for caching Texy! results
<?php
/**
* TexyCache - class for caching Texy! results
* -------------------------------------------
*
* Using Texy! the great web text markup-language processor by David Grudl (http://davidgrudl.com)
* For more information please visit http://texy.info
*
* @author Ondrej Slamecka, 2010
* @license New BSD license (see http://texy.info/en/licence)
*/
class TexyCache /* extends \Nette\Object */
{
private $texy;
private $path;
public function __construct($path = '')
{
$this->texy = new \Texy;
$this->path = $path;
if (!is_dir($this->path)) {
mkdir($this->path, 0777);
}
}
public function getTexy()
{
return $this->texy;
}
public function process($text)
{
$cachefilepath = $this->path . '/_' . md5($text) . '.tcf';
if (is_file($cachefilepath)) {
$cache = file_get_contents($cachefilepath);
$cache = @unserialize($cache);
return $cache[0];
} else {
$text = $this->texy->process($text);
// Folder must exist!
fwrite(fopen($cachefilepath, 'w'), serialize(array(0 => $text)));
return $text;
}
}
public function flushCache()
{
foreach (scandir($this->path) as $tcf) {
if ($tcf != '.' && $tcf != '..') {
unlink($this->path . '/' . $tcf);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment