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

smarty3 and caching with memcached
Goto page 1, 2  Next
 
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 -> Bugs
View previous topic :: View next topic  
Author Message
redl
Smarty Rookie


Joined: 10 Jun 2015
Posts: 9

PostPosted: Wed Jun 10, 2015 3:46 pm    Post subject: smarty3 and caching with memcached Reply with quote

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
View user's profile Send private message
redl
Smarty Rookie


Joined: 10 Jun 2015
Posts: 9

PostPosted: Wed Jun 10, 2015 4:51 pm    Post subject: Reply with quote

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
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Jun 10, 2015 9:17 pm    Post subject: Reply with quote

I though, "eval()" was eradicated?
Back to top
View user's profile Send private message
redl
Smarty Rookie


Joined: 10 Jun 2015
Posts: 9

PostPosted: Thu Jun 11, 2015 12:50 pm    Post subject: Reply with quote

AnrDaemon wrote:
I though, "eval()" was eradicated?


Sorry, don't understand. What do you mean "eradicated"?
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Thu Jun 11, 2015 7:54 pm    Post subject: Reply with quote

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
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Thu Jun 11, 2015 8:12 pm    Post subject: Reply with quote

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
View user's profile Send private message
redl
Smarty Rookie


Joined: 10 Jun 2015
Posts: 9

PostPosted: Thu Jun 11, 2015 8:33 pm    Post subject: Reply with quote

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
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Thu Jun 11, 2015 9:50 pm    Post subject: Reply with quote

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
View user's profile Send private message
redl
Smarty Rookie


Joined: 10 Jun 2015
Posts: 9

PostPosted: Fri Jun 12, 2015 9:56 am    Post subject: Reply with quote

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
View user's profile Send private message
redl
Smarty Rookie


Joined: 10 Jun 2015
Posts: 9

PostPosted: Fri Jun 12, 2015 10:34 am    Post subject: Reply with quote

U.Tews wrote:
I can't reproduce the problem with cacheresource.memcache.php from the demo folder.


Ooops. Yet another issue Smile
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
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Sat Jun 13, 2015 5:42 pm    Post subject: Reply with quote

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
View user's profile Send private message
redl
Smarty Rookie


Joined: 10 Jun 2015
Posts: 9

PostPosted: Sat Jun 13, 2015 7:44 pm    Post subject: Reply with quote

U.Tews wrote:
Thanks for finding the cause.
This fix is now in the dev-master version.

Great! Thank you.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sun Jun 14, 2015 9:52 am    Post subject: Reply with quote

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
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sun Jun 14, 2015 10:21 am    Post subject: Reply with quote

Or even better in your specific case,

Code:
extract(unpack('N1s/N1m/a*content', $content));


Two operations instead of five.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Sun Jun 14, 2015 6:36 pm    Post subject: Reply with quote

I will test it later
Back to top
View user's profile Send private message
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 -> Bugs All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
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