Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

clear_cache / clear_all_cache fails silently...

 
Post new topic   Reply to topic    Smarty Forum Index -> Smarty Development
View previous topic :: View next topic  
Author Message
SimonRees
Smarty n00b


Joined: 16 Jul 2004
Posts: 3
Location: Brighton, UK

PostPosted: Fri Jul 16, 2004 3:32 pm    Post subject: clear_cache / clear_all_cache fails silently... Reply with quote

When I call clear_cache() or clear_all_cache() it returns true in some circumstances even if the cache couldn't be cleared.
My specific situation is using Smarty 2.6.2, PHP 4.3.6 on a windows XP box. If the cache files can't be deleted as a result of file permissions the function call fails to delete the files but returns true.

Looking further into the smarty code the issue is as a result of smarty_core_rmdir(...) in core.rmdir.php which does not take account of the return from _unlink(...) in Smarty.class.php.

What are peoples opinions about the necessity of clear_cache() returning false on (possibly partital) failure? I don't think it would be hard to get smarty_core_rmdir(...) to return false if one of it's attempts to unlink a file fails although I haven't looked into how smarty_core_rmdir(...) is used elsewhere in Smarty.

If I'm not missing something here and the silent failure of clear_cache() is a bug not a feature I could provide a patch to solve this problem.

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


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

PostPosted: Fri Jul 16, 2004 6:35 pm    Post subject: Reply with quote

This isn't a typical problem... if Smarty can create the cache files in the first place, Smarty should be able to remove them. As for returning false if cache files were not removable, that is up to debate I suppose. Should it return false if there were no files to remove, or if the specific file you wanted to delete was not found?
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 Jul 16, 2004 7:12 pm    Post subject: Reply with quote

Perhaps the problem is that it can't remove them because they are in use?
Back to top
View user's profile Send private message
SimonRees
Smarty n00b


Joined: 16 Jul 2004
Posts: 3
Location: Brighton, UK

PostPosted: Sat Jul 17, 2004 1:14 pm    Post subject: Reply with quote

I'm not having a problem as such although I'll mention some potential situations that could cause problems later in this post.

I'm in the process of writing a framework for remote decaching as i'm working on a load balanced multi host website. Part of the framework allows for reporting of an operation error, i.e. the requested clear_cache failed. As there was no documentation on how clear_cache behaved if it couldn't remove the cache files requested I tested it by fiddling with the file perms in the cache directory so it wouldn't be able to delete them. The function call returns true even though the cache files are not removed in that situation.

What I would suggest is that if a call to clear_cache or clear_all_cache cannot remove one (or more) of the files it would be expected to then it should raise some kind of error. The cache file not existing shouldn't be an error as the program requesting the clear_cache shouldn't need to know if a file is cached, same goes for the cache being empty. Also there are already functions for determining if a file is cached.

The user or program requesting the clear_cache should be made aware in the unlikely event that there was a problem clearing the cache as out of date information may be being displayed on their website and they will need to take some kind of corrective action.

One situation I can imagine where file permissions could cause a problem for clear_cache is where users are authenticated by windows on an IIS box. AFAICT (and I'm no windows expert) IIS always runs as the user who logged in - similar things are possible with Apache on unix. If you were building a CMS where the server operating system managed user authentication you would want to know if a user that was supposed to be able to clear a cache actually could or not.
I had not thought of boots suggestion. If that is a possibility then it would be even more useful to know about a failure when calling clear_cache.

I could use a custom cache handler function on the other hand. The example mysql one in the docs appears to be able to return an error if there is a problem.

cheers Simon
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Sat Jul 17, 2004 8:07 pm    Post subject: Reply with quote

I don't think this is very pertinent for the main distribution but you may want to explore the possibilities using a custom cache handler. First of all, the error situations you discussed are not caused by normal operation. Someone actually has to go and touch Smarty's cache directory/files. The one concerning IIS is not quite true--IIS uses a privledge scheme based on impersonation and so as long as the user being impersonated (which can be set per site/virtual site) are sufficient, access is permitted. Having files that are in use not clearing is also not an issue--they will be regenerated when they expire. The general case, IMO, does not merit a cache failure reporting unless it can be done without any extra overhead.

The big question, really, is what would you do with that failure status? Log it? Use it to debug? Better to have a script that probes to ensure the features/permissions you need at setup time than in your runtime (well, for me that is certainly true). If you certify your environment in this way you can rule out most issues in advance and just trust the software to work properly.

That being said, I'd love to see what you come up with or what specific changes you would propose to solve specific conditions.
Back to top
View user's profile Send private message
SimonRees
Smarty n00b


Joined: 16 Jul 2004
Posts: 3
Location: Brighton, UK

PostPosted: Thu Jul 29, 2004 9:57 pm    Post subject: Reply with quote

Been a bit busy recently but here's a diff of the changes I've made so far to core.rmdir.php:

Code:

24a25,26
>    $retval = true;
>
35,36c37,39
<                     require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.rmdir.php');
<                     smarty_core_rmdir($_params, $smarty);
---
>
>                     $retval &= smarty_core_rmdir($_params, $smarty);
>
39c42,43
<                     $smarty->_unlink($params['dirname'] . DIRECTORY_SEPARATOR . $_entry, $params['exp_time']);
---
>                     $retval &= $smarty->_unlink($params['dirname'] . DIRECTORY_SEPARATOR . $_entry, $params['exp_time']);
>
47c51
<        return @rmdir($params['dirname']);
---
>       return @rmdir($params['dirname']) && $retval;
49c53,54
<    return (bool)$_handle;
---
>
>    return (bool)$_handle && $retval;


It's not fully tested but so far appears to return false if the cache can't be cleared for some reason. I haven't profiled the code yet but I can't imagine the performance hit will be much. In testing however I've found some undocumented behaviour where false is returned when an attempt is made to clear the cache by id and the cache file doesn't exist. Is anyone using this behaviour? The docs claim the function returns void. It seems to me is_cached would be the correct way to check if a file exists.
I'll get back once I've fully checked out the existing behaviour of clear_cache and any ideas about how I think it could be modified...

As to what I would do programatically if clearing the cache failed, I'd either log it or report it to the user depending on the situation. Knowning that an attempt to clear the cache had failed would be relevent to my current project where I will have no involvement in the production implementation of my code and no access to the machines it will run on. If the client's sysadmins make a mistake in setting the perms of the cache dirs and then report problems with content not updating it will be useful to see the problem reported in the log file that I'll ask them to send me... Yours is a good point about using a script to set up the required directories but unfortunately the target is windows 2000 and I don't know vb scripting...

btw, wasn't sure why the file was "require"ing itself so removed that line with no functional change as far as I could tell. Please feel free to enlighten me as to why it was there... I'm quite new to php so please forgive any naivity!

cheers Simon
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Smarty Development 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