git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@3012 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
218 lines
6.5 KiB
PHP
218 lines
6.5 KiB
PHP
<?php
|
|
/** DomFramework
|
|
@package domframework
|
|
@author Dominique Fournier <dominique@fournier38.fr> */
|
|
|
|
/** A cache manager in file */
|
|
class cachefile
|
|
{
|
|
/** Where to store the cached information */
|
|
public $directory = "./cache";
|
|
/** If TRUE : no information is cached */
|
|
public $nocache = false;
|
|
|
|
/** Check if there is some garbage to clean */
|
|
public function garbage ()
|
|
{
|
|
try
|
|
{
|
|
$this->cachedir ();
|
|
}
|
|
catch (Exception $e)
|
|
{
|
|
throw new Exception ($e->getMessage(), $e->getCode());
|
|
}
|
|
|
|
$fileCache = $this->directory."/".sha1 ("Garbage-Lock");
|
|
$res = false;
|
|
if (file_exists ($fileCache))
|
|
{
|
|
$data = file_get_contents ($fileCache);
|
|
$data = unserialize ($data);
|
|
if (($data["createTime"] + $data["ttl"]) >= time ())
|
|
{
|
|
$res = $data["data"];
|
|
}
|
|
}
|
|
|
|
if ($res === false)
|
|
{
|
|
$data = array ("ttl"=>24*60*60,
|
|
"createTime"=>time(),
|
|
"data"=>"CACHE-Garbage");
|
|
file_put_contents ($fileCache, serialize ($data));
|
|
chmod ($fileCache, 0666);
|
|
$files = glob ($this->directory."/*", GLOB_NOSORT);
|
|
foreach ($files as $fileCache)
|
|
{
|
|
$data = file_get_contents ($fileCache);
|
|
if ($data === false)
|
|
{
|
|
unlink ($fileCache);
|
|
continue;
|
|
}
|
|
|
|
$data = unserialize ($data);
|
|
if (! isset ($data["ttl"]) || ! isset ($data["data"]) ||
|
|
! isset ($data["createTime"]))
|
|
{
|
|
unlink ($fileCache);
|
|
continue;
|
|
}
|
|
|
|
if (($data["createTime"] + $data["ttl"]) <= time ())
|
|
{
|
|
unlink ($fileCache);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/** This function check if the cachedir exists and create it if it is not the
|
|
case.
|
|
Check if the cache dir is writable and readable */
|
|
public function cachedir ()
|
|
{
|
|
if (! isset ($this->directory) || $this->directory === "")
|
|
throw new Exception (dgettext("domframework",
|
|
"No cache directory defined"), 500);
|
|
if (! file_exists ($this->directory))
|
|
{
|
|
// Need to create the cache dir
|
|
$parent = realpath (dirname ($this->directory));
|
|
if (! is_writeable (dirname ($this->directory)))
|
|
throw new Exception (sprintf (dgettext("domframework",
|
|
"Directory %s is not writable : can not create cache directory"),
|
|
$parent), 500);
|
|
if (!mkdir ($this->directory))
|
|
throw new Exception (sprintf (dgettext("domframework",
|
|
"Can not create cache directory %s"),
|
|
$this->directory), 500);
|
|
chmod ($this->directory, 0777);
|
|
}
|
|
if (! is_writable ($this->directory))
|
|
throw new Exception (sprintf (dgettext("domframework",
|
|
"Cache directory %s is not writable"),
|
|
$this->directory), 500);
|
|
if (! is_readable ($this->directory))
|
|
throw new Exception (sprintf (dgettext("domframework",
|
|
"Cache directory %s is not readable"),
|
|
$this->directory), 500);
|
|
if (!file_exists ($this->directory."/.htaccess"))
|
|
file_put_contents ($this->directory."/.htaccess", "deny from all\n");
|
|
return true;
|
|
}
|
|
|
|
/** This function write data in cache
|
|
The cache system can not save FALSE value
|
|
@param string $id Cache identifier (add the authentication, the METHOD...)
|
|
@param string $data The data to save
|
|
@param integer|null $ttl The cache Time to Leave in seconds (3600s by
|
|
default)*/
|
|
public function write ($id, $data, $ttl = 3600)
|
|
{
|
|
if ($this->nocache !== false)
|
|
return false;
|
|
if ($data === false)
|
|
throw new Exception (dgettext("domframework",
|
|
"Can not store FALSE in cache"), 500);
|
|
try
|
|
{
|
|
$this->cachedir ();
|
|
}
|
|
catch (Exception $e)
|
|
{
|
|
throw new Exception ($e->getMessage(), $e->getCode());
|
|
}
|
|
|
|
$this->garbage ();
|
|
$fileCache = $this->directory."/".sha1 ($id);
|
|
touch ($fileCache.".lock");
|
|
$data = array ("ttl"=>$ttl,
|
|
"createTime"=>time(),
|
|
"data"=>$data);
|
|
file_put_contents ($fileCache, serialize ($data));
|
|
unlink ($fileCache.".lock");
|
|
chmod ($fileCache, 0666);
|
|
return true;
|
|
}
|
|
|
|
/** This function read data from cache. Return FALSE in case of empty or too
|
|
older cache
|
|
@param string $id Cache identifier (add the authentication, the METHOD) */
|
|
public function read ($id)
|
|
{
|
|
if ($this->nocache !== false)
|
|
return false;
|
|
try
|
|
{
|
|
$this->cachedir ();
|
|
}
|
|
catch (Exception $e)
|
|
{
|
|
throw new Exception ($e->getMessage(), $e->getCode());
|
|
}
|
|
|
|
$this->garbage ();
|
|
$fileCache = $this->directory."/".sha1 ($id);
|
|
if (!file_exists ($fileCache))
|
|
return false;
|
|
if (!is_readable ($fileCache))
|
|
throw new Exception (sprintf (dgettext("domframework",
|
|
"File cache %s is not readable"),
|
|
$fileCache), 500);
|
|
if (!is_writable ($fileCache))
|
|
throw new Exception (sprintf (dgettext("domframework",
|
|
"File cache %s is not writable"),
|
|
$fileCache), 500);
|
|
// Lock : waiting the reconstruction of the cache by another process
|
|
// Waiting 10s maximum to re-create the cache file
|
|
$startTime = time ();
|
|
while (file_exists ($fileCache.".lock") && time () < $startTime + 10)
|
|
{
|
|
usleep (100000);
|
|
}
|
|
|
|
// The lock is pending 10s (stale) : removing it to read the data quicker
|
|
// next time
|
|
if (file_exists ($fileCache.".lock"))
|
|
unlink ($fileCache.".lock");
|
|
|
|
$data = file_get_contents ($fileCache);
|
|
if ($data === false)
|
|
{
|
|
unlink ($fileCache);
|
|
return false;
|
|
}
|
|
|
|
$data = unserialize ($data);
|
|
if (! isset ($data["ttl"]) || ! isset ($data["data"]) ||
|
|
! isset ($data["createTime"]))
|
|
{
|
|
unlink ($fileCache);
|
|
return false;
|
|
}
|
|
|
|
if (($data["createTime"] + $data["ttl"]) >= time ())
|
|
{
|
|
if (file_exists ($fileCache.".lock"))
|
|
unlink ($fileCache.".lock");
|
|
return $data["data"];
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/** This function delete an id in cache
|
|
@param string $id Cache identifier (add the authentication, the METHOD...)
|
|
*/
|
|
public function delete ($id)
|
|
{
|
|
$fileCache = $this->directory."/".sha1 ($id);
|
|
if (!file_exists ($fileCache))
|
|
return false;
|
|
unlink ($fileCache);
|
|
return true;
|
|
}
|
|
}
|