Categories > TinyButStrong general >

Delete cache files

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Emil
Date: 2006-11-26
Time: 04:39

Delete cache files

Hi, Skrol,

I am using cache plug-in, and I got several cache files like:

cache_topic1_page1.tmp
cache_topic1_page2.tmp
cache_topic2_page1.tmp....etc.

However, I have no idea how many pages will be generated  for topic one in the future. if i just want to remove all topic1's cache file, I have to remove all cache files.

I don't want to create many folders for each topic. can I use the '*' for the cache id to delete those topic1's cache files? like: "topic1*"?

I tried before, it doesn't work... is there any ways to play with it?
By: TomH
Date: 2006-11-26
Time: 16:44

Re: Delete cache files

Hello,
One way that I use to do clean up is to purge all cached files that have gotten old past a certain amount of time.

Using a function to sweep the cache directory - I then know that odl files are killed and content pages will be refreshed with updated/edited content the next time they are requested.

The function looks like this...
// ================================================================
// A little something to help clean up cache files
// WARNING --- use at your own risk ! ! ! ! !
//  ====
// Cheers, Tom Henry --->>>  http://tomhenry.us
// Some examples of TBS/ezSQL based apps http://tomhenry.us/tbs3/
//    ======         USAGE examples         =======           
//  The default time to keep files: $cache_time = 60;                    // 60minutes (1 hr)                        
//  The default cache_directory:  $cache_dir = "./cache";
//  If you want to inspect the file processing that is taking place then use    //  the return value. An array where each row has "file" and "msg" entries
//  $retval[]  =  array("file"=>" $filename ", "msg"=> " was NOT unlinked ($fileage mins) ");           
//  =======================================

function cache_sweeper($cache_time=180, $cache_dir = '$db->cache_dir') // default minutes to keep files 180 mins = 3 hrs
{
   if ($handle = opendir("$cache_dir")) {  //  edit for your cache dir
    /* This is the correct way to loop over the directory. */
       while (false !== ($filename = readdir($handle)))
    {            
     if( !is_dir($filename) ){
          $fileage = round( ( time() - filemtime("$cache_dir/$filename" ) )/60 ); //  edit for your cache dir

if( $fileage > $cache_time ){                                              unlink("$cache_dir/$filename");
$retval[]  = array("file"=>" $filename ", "msg"=> " WAS unlinked ($fileage mins) ");
}else{                   
$retval[]  =  array("file"=>" $filename ", "msg"=> " was NOT unlinked ($fileage mins) ");
}

}
}

if(!is_array($retval)){
$retval[]=array("file"=>"Cache files ", "msg"=> " NO cache files found ");
}
}
return $retval;
}
// ===========

HTH,


By: Emil
Date: 2006-11-27
Time: 01:31

Re: Delete cache files

Thanks TomH, However, I just want to use '*' charactor for the cacheID when I want to remove caches.

however, I fixed it. ( a stupid change.... not sure is there any smart way... )


I am using a sub-class of TBS. The function below is a private function, anyway the point is when we found a "*" in the cacheID and it is not alone. we backup the original cacheMask and create a new one for our cacheID (eg. 'topic1*'). When the mask changed, we use a single '*' to delete all topic1XXXXXX cache files. Finally, we changed the mask back.
    /**
    * TinyButStrong Cache plugin function call
    *
    * @param    string  cache file id
    * @param    int     action code or expiry time.
    * @param    string  cache file dir location
    * @access private
    * @return mixed (bool | int)
    */
    function _doCacheAction($cacheID_, $actionOrAge_, $cacheDir_){
        $cacheVar =& $this->insVars['cache'];

        if(!$cacheVar['plugInLoaded']){
            $this->PlugIn(TBS_INSTALL, TBS_CACHE, CACHE_DIR, CACHE_PREFIX.'*.tmp');
            $cacheVar['plugInLoaded'] = true;
        }

        if( strrpos($cacheID_, '*') > 0 && $actionOrAge_ === TBS_DELETE && isset($this->CacheMask) ){
            $originalMask_      = $this->CacheMask;
            $this->CacheMask    = CACHE_PREFIX.$cacheID_.'.tmp';
            $result_            = $this->plugIn(TBS_CACHE, '*', $actionOrAge_, $cacheDir_);
            $this->CacheMask    = $originalMask_; // recover the mask
            return $result_;
        }
        return $this->plugIn(TBS_CACHE, $cacheID_, $actionOrAge_, $cacheDir_);
    }

By: Skrol29
Date: 2006-11-27
Time: 03:22

Re: Delete cache files

Hi,

An upgrade of the Cache plug-in will be provided with TBS 3.2.0 tomorrow.
This upgrade will give a solution for your problem.
There is a fix and also a new command for the plug-in.
By: Skrol29
Date: 2006-11-28
Time: 00:45

Re: Delete cache files

Hi,

TBS 3.2.0 is available, and the Cache plug-in is upgraded.
By: Emil
Date: 2006-11-28
Time: 01:06

Re: Delete cache files

Thanks Skrol29 !!...^_________^