Skip to content

Instantly share code, notes, and snippets.

@artdevue
Last active December 16, 2015 10:59
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save artdevue/5424557 to your computer and use it in GitHub Desktop.
The driver Extfile file cache framework for Laravel
<?php namespace Laravel\Cache\Drivers;
use ___PHPSTORM_HELPERS\object;
class Extfile extends Driver {
/**
* The path to which the cache files should be written.
*
* @var string
*/
protected $path;
/**
* Create a new File cache driver instance.
*
* @param string $path
* @return void
*/
public function __construct($path)
{
$this->path = $path;
}
/**
* Feature extraction of data from the php file cache.
*
* @param string
* @return void
*/
public function get_file($key)
{
$value= null;
if (file_exists($key))
{
if ($files = @fopen($key, 'rb'))
{
if (flock($files, LOCK_SH))
{
$value= @include $key;
flock($files, LOCK_UN);
if ($value === null)
{
fclose($files);
@ unlink($key);
}
}
@fclose($files);
}
}
return $value;
}
/**
* Determine if an item exists in the cache.
*
* @param string $key
* @return bool
*/
public function has($key)
{
return ( ! is_null($this->get($key)));
}
public function key_file($key, $isdir = null)
{
$fileName = $key;
$fileDir = '';
if($pos = strripos($key, '/'))
{
$fileName = substr($key, $pos + 1);
$fileDir = substr($key, 0, $pos);
if(isset($isdir) && !is_dir($this->path.$fileDir))
{
if(!mkdir($this->path.$fileDir,0777,true))
{
return false;
}
}
$fileDir = $fileDir.'/';
}
return (object) compact('fileName', 'fileDir');
}
/**
* Retrieve an item from the cache driver.
*
* @param string $key
* @return mixed
*/
protected function retrieve($key)
{
$key_file = $this->key_file($key);
if (!file_exists($this->path.$key_file->fileDir.$key_file->fileName.'.php')) return null;
// File based caches store have the expiration timestamp stored in
// UNIX format prepended to their contents. We'll compare the
// timestamp to the current time when we read the file.
if(!$cache = $this->get_file(File($this->path.$key_file->fileDir.$key_file->fileName.'.php')))
{
return $this->forget($key);
}
return $cache;
}
/**
* Write an item to the cache for a given number of minutes.
* if you specify zero minutes, then remove to keep the cache.
*
* <code>
* // Put an item in the cache for 15 minutes
* Cache::put('name', 'Taylor', 15);
* </code>
*
* @param string $key
* @param mixed $value
* @param int $minutes
* @return void
*/
public function put($key, $value, $minutes)
{
if ($value !== null)
{
if ($minutes === true)
$minutes = 0;
$expirationTS= $minutes ? time() + $minutes * 60 : 0;
$expireContent= '';
if ($expirationTS)
{
$expireContent= 'if(time() > ' . $expirationTS . '){return null;}';
}
if(!$key_file = $this->key_file($key,true))
{
return;
}
$content= '<?php ' . $expireContent . ' return ' . var_export($value,true) . ';'; // var_export($value, true)
file_put_contents($this->path.$key_file->fileDir.$key_file->fileName.'.php', $content, LOCK_EX);
}
return;
}
/**
* Write an item to the cache for five years.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function forever($key, $value)
{
return $this->put($key, $value, 2628000);
}
/**
* Delete an item from the cache.
*
* @param string $key
* @return void
*/
public function forget($key)
{
$key_file = $this->key_file($key);
if (file_exists($this->path.$key_file->fileDir.$key_file->fileName.'.php'))
@unlink($this->path.$key_file->fileDir.$key_file->fileName.'.php');
}
/**
* Flush the entire cache.
* or remove nested folders without a slash at the beginning and at the end of
*
* <code>
* // recursively delete the files of the folder test (all attached files will be deleted)
* Cache::flush('myfolder/test');
* </code>
* @return void
*/
public function flush($dir = null)
{
$fileDir = isset($dir) ? $this->path.$dir.'/' : $this->path;
if ($objs = glob($fileDir."/*"))
{
foreach($objs as $obj)
{
is_dir($obj) ? $this->flush(str_replace($this->path, '', $obj)) : unlink($obj);
}
}
if($fileDir != $this->path)
{
if(is_dir($fileDir))
rmdir($fileDir);
}
//array_map('unlink', glob($this->path.$fileDir.'*'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment