for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Millennium\Cache\Drivers;
use Millennium\Cache\Exceptions\FileStorage\FileStorageMisconfiguration;
use Millennium\Cache\Interfaces\CacheDriverInterface;
class FileStorageDriver implements CacheDriverInterface
{
/**
* @var string
*/
private $cachePath;
* @param array $options
*
* @throws FileStorageMisconfiguration
public function __construct(array $options = null)
if (null === $options) {
throw new FileStorageMisconfiguration();
}
if (!isset($options['path'])) {
throw new FileStorageMisconfiguration('Please set path for file storage driver.');
if (!is_dir($options['path']) || !is_readable($options['path'])) {
throw new \Millennium\Cache\Exceptions\FileStorage\FileStorageDirectoryNotReadableException($options['path']);
$this->cachePath = rtrim($options['path'], DIRECTORY_SEPARATOR);
* @param string $key
* @return bool
public function fetch($key)
if (file_exists($this->cachePath.DIRECTORY_SEPARATOR.$key.'.cache')) {
list($expire, $data) = unserialize(file_get_contents($this->cachePath.DIRECTORY_SEPARATOR.$key.'.cache'));
if (filemtime($this->cachePath.DIRECTORY_SEPARATOR.$key.'.cache') <= $expire) {
return $data;
unlink($this->cachePath.DIRECTORY_SEPARATOR.$key.'.cache');
return false;
public function remove($key)
return true;
* @param array $data
public function store($key, $data, $expire = 3600)
return file_put_contents($this->cachePath.DIRECTORY_SEPARATOR.$key.'.cache', serialize([time() + $expire, $data]));