Skip to content

Instantly share code, notes, and snippets.

@edhaase
Created January 15, 2016 14:42
Show Gist options
  • Save edhaase/ad6fcafb437fbd83ed33 to your computer and use it in GitHub Desktop.
Save edhaase/ad6fcafb437fbd83ed33 to your computer and use it in GitHub Desktop.
<?php
class TwigAPCuCache implements \Twig_CacheInterface
{
/**
* {@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)
{
$r = apcu_store($key, [
'data' => $content,
'timestamp' => time()
]);
}
/**
* {@inheritdoc}
*/
public function load($key)
{
$content = apcu_fetch($key);
if ($content!==false && $content!==null && array_key_exists('data',$content)) {
eval('?>'.$content['data']);
}
}
/**
* {@inheritdoc}
*/
public function getTimestamp($key)
{
$content = apcu_fetch($key);
if ($content !== false && array_key_exists('timestamp',$content)) {
return $content['timestamp'];
}
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment