cachefile : Run the garbage each 24h automatically

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1585 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2014-07-28 11:44:18 +00:00
parent 7631d8cb71
commit c5f4f745b0

View File

@@ -18,10 +18,25 @@ class cachefile
throw new Exception ($e->getMessage(), $e->getCode());
}
$res = $this->read ("CACHE-Garbage");
$fileCache = $this->directory."/".sha1 ("Garbage-Lock");
$res = false;
if (file_exists ($fileCache))
{
$datas = file_get_contents ($fileCache);
$datas = unserialize ($datas);
if (($datas["createTime"] + $datas["ttl"]) >= time ())
{
$res = $datas["data"];
}
}
if ($res === false)
{
$this->write ("CACHE-Garbage", "Let's go, garbage !", 24*60*60);
$datas = array ("ttl"=>24*60*60,
"createTime"=>time(),
"data"=>"CACHE-Garbage");
file_put_contents ($fileCache, serialize ($datas));
chmod ($fileCache, 0666);
$files = glob ($this->directory."/*", GLOB_NOSORT);
foreach ($files as $fileCache)
{
@@ -40,7 +55,7 @@ class cachefile
continue;
}
if (($datas["createTime"] + $datas["ttl"]) >= time ())
if (($datas["createTime"] + $datas["ttl"]) <= time ())
{
unlink ($fileCache);
}
@@ -100,6 +115,7 @@ class cachefile
throw new Exception ($e->getMessage(), $e->getCode());
}
$this->garbage ();
$fileCache = $this->directory."/".sha1 ($id);
touch ($fileCache.".lock");
$datas = array ("ttl"=>$ttl,
@@ -127,6 +143,7 @@ class cachefile
throw new Exception ($e->getMessage(), $e->getCode());
}
$this->garbage ();
$fileCache = $this->directory."/".sha1 ($id);
if (!file_exists ($fileCache))
return false;