Jeff-Lewis / smarty-php

Automatically exported from code.google.com/p/smarty-php
0 stars 0 forks source link

ClearCache not working with "string:" templates #169

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Enable caching and fetch or display to "string:{$foo}"
2. Now call clearCache on "string:{$foo}"
3. The cache file isn't cleared.

(Tested versions : Smarty 3.1.14, Smarty 3.1.16)

Why ?

I've found why while working in a custom cache resource implementation, but the 
problem also exists in the default cache implementation.

If you extend Smarty_CacheResource_Custom, when you call $smarty->fetch(), here 
is the value of the $name parameter into the 
Smarty_CacheResource_Custom::fetch() method : 

string:{$foo}

Here's the value of the $name parameter into the 
Smarty_CacheResource_Custom::delete() method :

{$foo}

They don't match, so the cache isn't cleared.

I'm using the "string:" templates for caching distant REST API responses, and I 
don't need a real .tpl file for this.

Thanks,
Ben

Original issue reported on code.google.com by bpolas...@gmail.com on 8 Jan 2014 at 11:30

GoogleCodeExporter commented 9 years ago
Edit :
Smarty_CacheResource_Custom::fetch() => {$foo}
Smarty_CacheResource_Custom::delete() => string:{$foo}

I swapped them by mistake, but it doesn't change the issue.

Original comment by bpolas...@gmail.com on 8 Jan 2014 at 11:42

GoogleCodeExporter commented 9 years ago
PS : Temporarily fixed it by extending Smarty_CacheResource_Custom::delete() 
with this code :

protected function delete($name = null, $cache_id = null, $compile_id = null, 
$exp_time = null) { 

    if (preg_match('/(^string:(.*)$)/', $name))
        $name = substr($name, 7);

    // [...]

}

The cache is now correctly cleared.

Original comment by bpolas...@gmail.com on 8 Jan 2014 at 11:51

GoogleCodeExporter commented 9 years ago
It was a more general problem. Smarty_CacheResource_Custom did not handle all 
template resource type specification on clearCache() calls correctly.

The fix is now in the SVN trunk and will later be included in 3.1.17 

Original comment by Uwe.Tews@googlemail.com on 8 Jan 2014 at 9:10

GoogleCodeExporter commented 9 years ago
Brilliant. :-) 

Original comment by bpolas...@gmail.com on 8 Jan 2014 at 9:30

GoogleCodeExporter commented 9 years ago
Hello Uwe,

I've noticed the issue has been fixed in Smarty 3.1-17.
However, it has only been fixed on the Smarty_CacheResource_Custom 
implementation, not on Smarty_CacheResource_KeyValueStore.

Consider I'm assigning an XML string into an $Xml var, and I display() to 
string:{$Xml}, before I want to clearCache() for another reason.

$Xml = '<xml><node>some xml stuff</node></xml>';
$smarty->assign('Xml', $Xml);
$smarty->display('string:{$Xml}');
// ...
$smarty->clearCache('string:{$Xml}');

When I use the APC cache handler, that extends 
Smarty_CacheResource_KeyValueStore, here's the key given to the write() method :
7ce21065625fafe8fe1492df4f8d07b81a3428eb#_Xml_#dc0516381f79e352c32ef4bdcf305701#
0f11d3a0d13624bfaff21c253842f23a

And when I clearCache(), here's the key given to the delete() method :
7ce21065625fafe8fe1492df4f8d07b81a3428eb#string_Xml_#e5ea6606b0a18f282e6685609f5
38eff#0f11d3a0d13624bfaff21c253842f23a

Since the key isn't strictly identical, the cache isn't cleared.

Same issue then.

Thanks,
Ben

Original comment by bpolas...@gmail.com on 13 Mar 2014 at 10:36

GoogleCodeExporter commented 9 years ago
This is now fixed in the SVN trunk.
It will be included in 3.1.18

Original comment by Uwe.Tews@googlemail.com on 15 Mar 2014 at 9:52