Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

garbage-collection-function

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Feature Requests
View previous topic :: View next topic  
Author Message
kleinerChemiker
Smarty Rookie


Joined: 12 Dec 2003
Posts: 28

PostPosted: Thu Jun 23, 2005 1:30 pm    Post subject: garbage-collection-function Reply with quote

a function that removes old unused cached files would be great. (fileatime())

MIK
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Thu Jun 23, 2005 1:47 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Thu Jun 23, 2005 1:56 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
kleinerChemiker
Smarty Rookie


Joined: 12 Dec 2003
Posts: 28

PostPosted: Thu Jun 23, 2005 3:37 pm    Post subject: Reply with quote

thx

but its an win-system. maybe i can alter the clear_cache funktion.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Thu Jun 23, 2005 3:42 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
kleinerChemiker
Smarty Rookie


Joined: 12 Dec 2003
Posts: 28

PostPosted: Thu Jun 23, 2005 4:44 pm    Post subject: Reply with quote

im just working on it, but my problem is, that i dont have experiance with oop. but im trying Wink

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 Shocked


Last edited by kleinerChemiker on Thu Jun 23, 2005 4:52 pm; edited 1 time in total
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Thu Jun 23, 2005 4:49 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Thu Jun 23, 2005 5:31 pm    Post subject: Reply with quote

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 Shocked


correct! fixed in CVS. thanks!
Back to top
View user's profile Send private message Send e-mail Visit poster's website
kleinerChemiker
Smarty Rookie


Joined: 12 Dec 2003
Posts: 28

PostPosted: Thu Jun 23, 2005 6:30 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Feature Requests All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP