Created
January 15, 2016 14:42
-
-
Save edhaase/ad6fcafb437fbd83ed33 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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