View previous topic :: View next topic |
Author |
Message |
kleinerChemiker Smarty Rookie
Joined: 12 Dec 2003 Posts: 28
|
Posted: Thu Jun 23, 2005 1:30 pm Post subject: garbage-collection-function |
|
|
a function that removes old unused cached files would be great. (fileatime())
MIK |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Jun 23, 2005 1:47 pm Post subject: |
|
|
Here you go (untested):
[php:1:3b061d02e4]Smarty_GC extends Smarty {
var $_gc_ratio = 50; // probability ratio
var $_gc_exp = 3600; // cache expire time
function Smarty_GC() {
$this->Smarty();
if(rand(1,$this->_gc_ratio) == 1)
$this->clear_all_cache($this->_gc_exp);
}
}
$smarty = new Smarty_GC;
...[/php:1:3b061d02e4] |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Jun 23, 2005 1:56 pm Post subject: |
|
|
If you want something that works with fileatime(), then make a custom function for it. Here's a quick and dirty method using find (linux/unix):
[php:1:eeb8573cf0]Smarty_GC extends Smarty {
var $_gc_ratio = 50; // probability ratio
var $_gc_exp = 2; // cache access time in days
function Smarty_GC() {
$this->Smarty();
if(rand(1,$this->_gc_ratio) == 1)
$this->garbage_collect();
}
function garbage_collect() {
$_cache_dir = $this->cache_dir;
$_atime = $this->_gc_exp;
`/usr/bin/find $_cache_dir/. -atime +$_atime -exec rm -rf {} \;`;
}
}[/php:1:eeb8573cf0]
Last edited by mohrt on Thu Jun 23, 2005 4:48 pm; edited 1 time in total |
|
Back to top |
|
kleinerChemiker Smarty Rookie
Joined: 12 Dec 2003 Posts: 28
|
Posted: Thu Jun 23, 2005 3:37 pm Post subject: |
|
|
thx
but its an win-system. maybe i can alter the clear_cache funktion. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Jun 23, 2005 3:42 pm Post subject: |
|
|
You'll have to write a method that uses the PHP file functions to traverse the cache directory and unlink the files that are stale. Overriding the Smarty clear_cache function is up to you. |
|
Back to top |
|
kleinerChemiker Smarty Rookie
Joined: 12 Dec 2003 Posts: 28
|
Posted: Thu Jun 23, 2005 4:44 pm Post subject: |
|
|
im just working on it, but my problem is, that i dont have experiance with oop. but im trying
during my exploration, i have an question in the file core.rmdir.php. there is this code:
Code: |
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* delete a dir recursively (level=0 -> keep root)
* WARNING: no tests, it will try to remove what you tell it!
*
* @param string $dirname
* @param integer $level
* @param integer $exp_time
* @return boolean
*/
// $dirname, $level = 1, $exp_time = null
function smarty_core_rmdir($params, &$smarty)
{
if(!isset($params['level'])) { $params['level'] = 1; }
if(!isset($params['exp_time'])) { $params['exp_time'] = null; }
if($_handle = @opendir($params['dirname'])) {
while (false !== ($_entry = readdir($_handle))) {
if ($_entry != '.' && $_entry != '..') {
if (@is_dir($params['dirname'] . DIRECTORY_SEPARATOR . $_entry)) {
$_params = array(
'dirname' => $params['dirname'] . DIRECTORY_SEPARATOR . $_entry,
'level' => $params['level'] + 1,
'exp_time' => $params['exp_time']
);
require_once(SMARTY_CORE_DIR . 'core.rmdir.php');
smarty_core_rmdir($_params, $smarty);
}
else {
$smarty->_unlink($params['dirname'] . DIRECTORY_SEPARATOR . $_entry, $params['exp_time']);
}
}
}
closedir($_handle);
}
if ($params['level']) {
return @rmdir($params['dirname']);
}
return (bool)$_handle;
}
|
why do we do this: require_once(SMARTY_CORE_DIR . 'core.rmdir.php');
we must have included this file, because we are in this file
Last edited by kleinerChemiker on Thu Jun 23, 2005 4:52 pm; edited 1 time in total |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Jun 23, 2005 4:49 pm Post subject: |
|
|
Just use the above example and replace the guts of the garbage_collect() function with your own removal logic. You have access to the Smarty properties with $this, such as $this->cache_dir. |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Thu Jun 23, 2005 5:31 pm Post subject: |
|
|
kleinerChemiker wrote: | why do we do this: require_once(SMARTY_CORE_DIR . 'core.rmdir.php');
we must have included this file, because we are in this file |
correct! fixed in CVS. thanks! |
|
Back to top |
|
kleinerChemiker Smarty Rookie
Joined: 12 Dec 2003 Posts: 28
|
Posted: Thu Jun 23, 2005 6:30 pm Post subject: |
|
|
my setup incl. garbage collection:
Code: |
class smarty_ow extends Smarty {
function smarty_ow()
{
$this->Smarty();
$this->template_dir = _SMARTY_ROOT_DIR_ . _SMARTY_TPL_DIR_;
$this->compile_dir = _SMARTY_ROOT_DIR_ . _SMARTY_COMPILE_DIR_;
#$this->config_dir = '/web/www.mydomain.com/smarty/guestbook/configs/';
$this->cache_dir = _SMARTY_ROOT_DIR_ . _SMARTY_CACHE_DIR_;
$this->debug_tpl = SMARTY_ROOT_DIR;
$this->caching = 2;
$this->garbage_collect();
}
function GetDirContents($dir){
if ($root=@opendir($dir)){
while ($file=readdir($root)){
if($file=="." || $file==".."){continue;}
if(is_dir($dir.$file)){
$files=array_merge($files,GetDirContents($dir.$file.'/'));
}else{
$files[]=$dir.$file;
}
}
}
return $files;
}
function garbage_collect($_gc_exp = 604800, $_gc_ratio = 100) {
if(mt_rand(1,$this->_gc_ratio) == 1) {
$files = GetDirContents($this->cache_dir);
foreach ($files as $file) {
if ((time()-fileatime($file)) > $_gc_exp) {
unlink($file);
}
}
}
}
}
|
|
|
Back to top |
|
|