Skip to content

Instantly share code, notes, and snippets.

@edhaase
Created January 15, 2016 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edhaase/90959902c1f635f5b716 to your computer and use it in GitHub Desktop.
Save edhaase/90959902c1f635f5b716 to your computer and use it in GitHub Desktop.
<?php
class TwigStashCache implements \Twig_CacheInterface
{
private $pool;
public function __construct(\Stash\Interfaces\PoolInterface $pool)
{
$this->pool = $pool;
}
/**
* {@inheritdoc}
*/
public function generateKey($name, $className)
{
$key = "$name;$className";
// Key generation can be tweaked, it doesn't need to be cryptographically secure
// Just fast.
$key = "TWIG_CACHE;" . hash_hmac('sha256', $key, 'TWIG', false);
return $key;
}
/**
* {@inheritdoc}
*/
public function write($key, $content)
{
$item = $this->pool->getItem($key);
$item->set([
'data' => $content,
'timestamp' => time()
]);
}
/**
* {@inheritdoc}
*/
public function load($key)
{
$item = $this->pool->getItem($key);
$content = $item->get();
if(!$item->isMiss()) {
eval('?>'.$content['data']);
}
}
/**
* {@inheritdoc}
*/
public function getTimestamp($key)
{
$item = $this->pool->getItem($key);
$content = $item->get();
if(!$item->isMiss()) {
return $content['timestamp'];
}
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment