Skip to content

Instantly share code, notes, and snippets.

@bizley
Forked from Ocramius/Ocramius_PhpCache.php
Created October 31, 2019 21:37
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 bizley/86fa1bfed840e2c295fdcb2106e304a3 to your computer and use it in GitHub Desktop.
Save bizley/86fa1bfed840e2c295fdcb2106e304a3 to your computer and use it in GitHub Desktop.
Simple cache adapter based on var_export and include.
<?php
/**
* Simple php cache using var_export generated files
*
* @author Marco Pivetta <ocramius@gmail.com>
*/
class Ocramius_PhpCache {
const DEFAULT_TTL = 3600;
/**
* @var string
*/
protected $cacheDir;
/**
* @var int
*/
protected $defaultTtl;
/**
* @param string $cacheDir where to store cache files
*/
public function __construct($cacheDir, $ttl = self::DEFAULT_TTL) {
$cacheDir = realpath($cacheDir);
if(!$cacheDir) {
throw new InvalidArgumentException('Provided cache dir does not exist');
}
if(!is_dir($cacheDir)) {
throw new InvalidArgumentException('Provided cache dir is not a directory');
}
if(!(is_readable($cacheDir) && is_writable($cacheDir))) {
throw new InvalidArgumentException('Provided cache dir is not writable and readable');
}
$this->cacheDir = $cacheDir;
$this->defaultTtl = (int) $ttl;
}
public function read($key) {
$key = (string) $key;
$hash = $this->hashKey($key);
$fileName = $this->cacheDir . DIRECTORY_SEPARATOR . $hash . '.php';
$cached = (@include($fileName));
if(
$cached
&& isset($cached['key'])
&& isset($cached['hash'])
&& isset($cached['timestamp'])
&& isset($cached['ttl'])
&& isset($cached['value'])
&& ((time() - $cached['timestamp']) < $cached['ttl'])
&& $cached['key'] === $key
&& $cached['hash'] === $hash
) {
return $cached['value'];
}
if($cached) {
@unlink($fileName);
}
return false;
}
public function write($key, $value, $ttl = null) {
$ttl = $ttl > 0 ? $ttl : $this->defaultTtl;
$key = (string) $key;
$hash = $this->hashKey($key);
$saved = @file_put_contents(
$this->cacheDir . DIRECTORY_SEPARATOR . $hash . '.php',
'<?php return ' . var_export(
array(
'key' => $key,
'hash' => $hash,
'value' => $value,
'timestamp' => time(),
'ttl' => $ttl,
),
true
) . ';'
);
}
protected function hashKey($key) {
return md5($key);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment