View previous topic :: View next topic |
Author |
Message |
redl Smarty Rookie
Joined: 10 Jun 2015 Posts: 9
|
Posted: Wed Jun 10, 2015 3:46 pm Post subject: smarty3 and caching with memcached |
|
|
Unfortunately smarty3.1.24 does not work properly with cache stored into memcached
PHP 5.5.9-1ubuntu4.9 with memcache extention
code:
Code: | define('SMARTY_RESOURCE_CHAR_SET', 'UTF-8');
require_once($_SERVER["DOCUMENT_ROOT"].'/smarty/SmartyBC.class.php');
$smarty = new SmartyBC();
...
$smarty->caching_type = 'memcache'; #note: load successful
$smarty->setCaching(Smarty::CACHING_LIFETIME_SAVED);
....
if (!$smarty->isCached('mytemplate.tpl', "a|b|c")) {
# do something
$smarty->cache_lifetime = 14400;
}
$smarty->display('mytemplate.tpl', "a|b|c"); |
First call:
result successfully stored in cache (memcached), but not displayed because
Code: | PHP Parse error: syntax error, unexpected '}' in /mypath/smarty/sysplugins/smarty_cacheresource_keyvaluestore.php(106) : eval()'d code on line 211
PHP Fatal error: Uncaught --> Smarty: Invalid compiled template for 'mytemplate.tpl' <-- \n thrown in /mypath/smarty/sysplugins/smarty_internal_template.php on line 362 |
There is no problems with the same code and default caching_type |
|
Back to top |
|
redl Smarty Rookie
Joined: 10 Jun 2015 Posts: 9
|
Posted: Wed Jun 10, 2015 4:51 pm Post subject: |
|
|
Update:
Code:
Code: | define('SMARTY_RESOURCE_CHAR_SET', 'UTF-8');
require_once($_SERVER["DOCUMENT_ROOT"].'/smarty/SmartyBC.class.php');
$smarty = new SmartyBC();
$smarty->caching_type = 'memcache';
$smarty->setCaching(Smarty::CACHING_LIFETIME_SAVED);
if (!$smarty->isCached('t.tpl')) {
$smarty->assign("title", "Darling");
echo "Process....";
$smarty->cache_lifetime = 14400;
}
$smarty->display('t.tpl'); |
Template:
Code: | <html>
<head>
<title>{$title}</title>
</head>
<body>
<h1>Hello, {$title}</h1>
</body>
</html> |
First page load result:
Process....
Errors:
Code: | PHP Parse error: syntax error, unexpected '}' in /www/includes/smarty/sysplugins/smarty_cacheresource_keyvaluestore.php(106) : eval()'d code on line 48
PHP Fatal error: Uncaught --> Smarty: Invalid compiled template for 't.tpl' <-- \n thrown in /www/includes/smarty/sysplugins/smarty_internal_template.php on line 362 |
First reload:
Process....
Hello, Darling
Errors:
Code: | PHP Parse error: syntax error, unexpected '}' in /www/includes/smarty/sysplugins/smarty_cacheresource_keyvaluestore.php(106) : eval()'d code on line 48
PHP Notice: Undefined index: cache_lifetime in /www/includes/smarty/sysplugins/smarty_template_cached.php on line 194
PHP Notice: Undefined index: cache_lifetime in /www/includes/smarty/sysplugins/smarty_template_cached.php on line 194 |
Second reload:
Hello, Darling
No errors
And so on:
Hello, Darling
No errors |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Wed Jun 10, 2015 9:17 pm Post subject: |
|
|
I though, "eval()" was eradicated? |
|
Back to top |
|
redl Smarty Rookie
Joined: 10 Jun 2015 Posts: 9
|
Posted: Thu Jun 11, 2015 12:50 pm Post subject: |
|
|
AnrDaemon wrote: | I though, "eval()" was eradicated? |
Sorry, don't understand. What do you mean "eradicated"? |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Thu Jun 11, 2015 7:54 pm Post subject: |
|
|
AnrDaemon wrote: | I though, "eval()" was eradicated? |
Cache resources other file system return the cached PHP code as string. So Smarty must eval it. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Thu Jun 11, 2015 8:12 pm Post subject: |
|
|
I can't reproduce the problem with cacheresource.memcache.php from the demo folder.
Did you use this or something else?
What is very confusing is that your provided template source does not result in cache code which has 48 lines, but line 48 fails according to the error message.
Erase the memcache content and the template_c folder completely. Does the problem still exists? |
|
Back to top |
|
redl Smarty Rookie
Joined: 10 Jun 2015 Posts: 9
|
Posted: Thu Jun 11, 2015 8:33 pm Post subject: |
|
|
U.Tews wrote: | I can't reproduce the problem with cacheresource.memcache.php from the demo folder.
Did you use this or something else? |
No. Just one difference - another IP address of remote memcached server in _consruct
U.Tews wrote: | Erase the memcache content and the template_c folder completely. Does the problem still exists? |
Yes. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Thu Jun 11, 2015 9:50 pm Post subject: |
|
|
Can you please cache a small template in the file system and memcache.
Provide the content of the file system cache file and the error message you get with memcache. |
|
Back to top |
|
redl Smarty Rookie
Joined: 10 Jun 2015 Posts: 9
|
Posted: Fri Jun 12, 2015 9:56 am Post subject: |
|
|
U.Tews wrote: | I can't reproduce the problem with cacheresource.memcache.php from the demo folder. |
I found the problem. Please look at the following message
Last edited by redl on Fri Jun 12, 2015 12:00 pm; edited 1 time in total |
|
Back to top |
|
redl Smarty Rookie
Joined: 10 Jun 2015 Posts: 9
|
Posted: Fri Jun 12, 2015 10:34 am Post subject: |
|
|
U.Tews wrote: | I can't reproduce the problem with cacheresource.memcache.php from the demo folder. |
Ooops. Yet another issue
You can reproduce the problem only with non compiled template AND with the following setting in php.ini
mbstring.func_overload = 2
Please remember that this setting can only be changed from the php.ini file
I think that the problem in following code in smarty_cacheresource_keyvaluestore.php
Code: | $s = unpack("N", substr($content, 0, 4));
$m = unpack("N", substr($content, 4, 4));
$content = substr($content, 8); |
substr() works with chars not bytes
Simple dummy replace for this code (str_split() will split into bytes, rather than characters when dealing with a multi-byte encoded string)
Code: |
$arr1 = str_split($content, 8);
$mts = str_split($arr1[0], 4);
$s = unpack("N", $mts[0]);
$m = unpack("N", $mts[1]);
unset($arr1[0]);
$content = implode("",$arr1);
|
|
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Sat Jun 13, 2015 5:42 pm Post subject: |
|
|
Thanks for finding the cause.
This fix is now in the dev-master version.
You can get the ZIP file here https://github.com/smarty-php/smarty/archive/master.zip
If you are using composer install the dev-master version.
we will soon release 3.1.25
I did not use str_split as it could result in a larger $arr1 array. |
|
Back to top |
|
redl Smarty Rookie
Joined: 10 Jun 2015 Posts: 9
|
Posted: Sat Jun 13, 2015 7:44 pm Post subject: |
|
|
U.Tews wrote: | Thanks for finding the cause.
This fix is now in the dev-master version. |
Great! Thank you. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Sun Jun 14, 2015 9:52 am Post subject: |
|
|
You can use unpack() directly if you only want a part of the string to be decoded.
In the aforementioned case,
Code: | extract(unpack('N1s/N1m/a*tail', $content)); |
|
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Sun Jun 14, 2015 10:21 am Post subject: |
|
|
Or even better in your specific case,
Code: | extract(unpack('N1s/N1m/a*content', $content)); |
Two operations instead of five. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Sun Jun 14, 2015 6:36 pm Post subject: |
|
|
I will test it later |
|
Back to top |
|
|