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

clear_cache() only working if cache_id specified

 
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 -> General
View previous topic :: View next topic  
Author Message
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 674
Location: Germany, border to Switzerland

PostPosted: Fri Mar 30, 2007 6:55 pm    Post subject: clear_cache() only working if cache_id specified Reply with quote

Hi guys,

i've been using Smarty for some years now, never had any problems. I'm currently working myself into all that advanced caching stuff - pretty cool! On the way i stumbled across some weird behaviour:

calling $smarty->clear_cache( 'template.htm' ); doesn't do a thing
calling $smarty->clear_cache( 'template.htm', 'cache_id' ); does what it's supposed to.

The former is supposed to delete all caches derived from "template.htm" disregarding any cache_groups, but it doesn't do a thing. The cache-files are written to disk after $smarty->display( 'template.htm', 'cache_id' ); is called, and the rights are correctly set: rw-r--r--

I'm running Smarty 2.6.18 on php 5.2.1-pl3-gentoo as apache-module.
eaccelerator is currently deactivated, suhosin is still on.

I tried to keep the example reasonable short an concise.
+ It doesn't matter if I enable sub-directory-caching or not.
+ It doesn't matter if I throw in that function-thing or not.

file: cachetest.php
Code:
<?php

error_reporting( E_ALL );
ini_set('display_errors', true);


/*
 *  SETUP SMARTY
 */

define('SMARTY_DIR', '/usr/share/php/smarty/');
require_once(SMARTY_DIR.'Smarty.class.php');

$tpl = new Smarty();
$tpl->template_dir   = '/var/www/dev.domain.de/templates/html/';
$tpl->compile_dir    = '/var/www/dev.domain.de/data/templates/php/html/';
$tpl->cache_dir    = '/var/www/dev.domain.de/data/templates/cache/html/';
$tpl->config_dir    = '/var/www/dev.domain.de/templates/html/';
$tpl->use_sub_dirs    = true;
$tpl->caching       = true;
$tpl->debugging    = true;
$tpl->error_reporting = E_ALL;


/*
 *  REACT ON PARAMETER
 */

if( !empty($_GET['clean']) )
{
   if( $_GET['clean'] == 'cachegroup' )
      $tpl->clear_cache( 'cachetest.htm', 'pflipp' );
   else
      $tpl->clear_cache( 'cachetest.htm' );

   header('Location: http://'.$_SERVER['HTTP_HOST'].'/cachetest.php');
   exit;
}


/*
 *  SETUP THE FUCTION-CACHE-TEST
 */

class TestClass
{
   public $endtime;

   public function __construct()
   {
      $this->endtime = time() + 60;
   }
}

function remaining_seconds($params, &$smarty) {
    $remain = $params['endtime'] - time();
    if($remain >= 0){
        return $remain . ' second(s)';
    }else{
        return 'done';
    }
}
$tpl->register_function('remaining', 'remaining_seconds', false, array('endtime'));


/*
 *  CONTROLS OUTSIDE OF SMARTY
 */
echo '<a href="cachetest.php?clean=all">clear all cache_groups for cachetest</a><br />';
echo '<a href="cachetest.php?clean=cachegroup">clear cache_group "pflipp"</a><br />';


/*
 *  MANAGE TEMPLATE OUTPUT
 */
if ( !$tpl->is_cached('cachetest.htm' , 'pflipp') )
{
   echo 'generated<br />';

    $tpl->assign_by_ref('obj', new TestClass() );
    $tpl->assign('time', time());
}
else
   echo 'read from cache<br />';

$tpl->display('cachetest.htm', 'pflipp');

?>


file: cachetest.htm
Code:

Time Generated: {$time}<br />
Time Remaining: {remaining endtime=$obj->endtime}


I looked through the list and this forum, but couldn't find a hint. I'm not saying i found a bug, but i can't make out any mistakes on my side. I simply don't get what's happening here. (And I'm not willing to flood Smarty with debugging-stuff until there is no other choice.)

Thanks for any help,
Rod
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Fri Mar 30, 2007 10:50 pm    Post subject: Reply with quote

Hi globe.

I could be wrong on this, but I'm pretty sure this is just a case where either the docs aren't entirely clear or that Smarty is simply not acting as to your intuition.

What you are really trying to do is:
Code:
$smarty->clear_cache( 'template.tpl', '*' );
and Smarty doesn't excactly have such a function (more on that in a bit).

When you write:
Code:
$smarty->clear_cache( 'template.tpl' );
what you are really telling Smarty to do is to clear the caches for 'template.tpl' where cache_id == null. ie: clear the cache for the default where no cache_id is set. In other words, this does not trigger a '*' match but rather selects a single cache_id (which is the null id).

So how to manage a whole group of caches at once? With cache grouping! For example, to achieve your goal you might want to prepend the template name to each cache group. That way you could use:
Code:
$smarty->clear_cache( 'template.tpl', 'template.tpl' );
and that would clear all the template.tpl caches (except the null id cache, assuming you used it).

I think I got the details right on this, so please excuse me if I misspoke Wink
Back to top
View user's profile Send private message
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 674
Location: Germany, border to Switzerland

PostPosted: Sat Mar 31, 2007 1:32 pm    Post subject: Reply with quote

Hi boots,

i guess the docs are somewhat misleading, then. I thought this was a bug rather than misunderstanding the docs, since i never experienced this before. But i just checked the last couple projects using Smarty and found out, that i was actually caching stuff, but never cleared caches by template, only by template and cache_group.

Your example did not work that well, though.

the cache is checked and displayed with:
Quote:
$smarty->is_cached( 'template.htm', 'tpl|pflipp' );
$smarty->display( 'template.htm', 'tpl|pflipp' );


trying to clear the cache did *not* work:
Code:
$smarty->clear_cache( 'template.htm', 'tpl' );


trying to clear the cache did work:
Code:
$smarty->clear_cache( 'template.htm', 'tpl|pflipp' );


trying to clear the cache did work:
Code:
$smarty->clear_cache( null, 'tpl' );


Once again tested with and without the function-thing, sub-dir-caching, and so on.

I'm not sure what's going wrong here. i can live with cache_clearing for template null for the moment, but I'm not to happy about it - although i don't really know why I'm not happy about it.

In any case, the docs should make this fact _very_ clear. I know documentation is not the programmer's favourite, but it has to be done _right_ to be useful to others.

Best regards,
Rod
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sat Mar 31, 2007 9:36 pm    Post subject: Reply with quote

Actually, I think I did confuse it there a little bit. I should have re-checked the manual page on multiple cache groups before answering as it does give a somewhat clearer view on how this works:

http://www.smarty.net/manual/en/caching.groups.php
Back to top
View user's profile Send private message
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 674
Location: Germany, border to Switzerland

PostPosted: Sun Apr 01, 2007 9:45 am    Post subject: Reply with quote

Hi boots,

okay then. I should read the docs more carefully, i guess.
My problem's solved, thanks!

Best regards,
Rod
Back to top
View user's profile Send private message Visit poster's website
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 -> General 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