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

3.1dev: How do I register cache resource?

 
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 -> Smarty Development
View previous topic :: View next topic  
Author Message
Lemon Juice
Smarty Pro


Joined: 24 May 2006
Posts: 109

PostPosted: Tue Aug 16, 2011 9:24 pm    Post subject: 3.1dev: How do I register cache resource? Reply with quote

I'm tempted to try out the new cache resource API but I don't know how I should register my custom cache resource. The examples in the 'demo' folder only show the cache resource classes but I can't find any docs on how to register them. I have tried this:
Code:

$smarty->registerCacheResource('mycache', new Smarty_CacheResource_Mycache);
$smarty->setCaching(true);

This only causes the constructor of Smarty_CacheResource_Mycache to be called but nothing else happens - templates are still cached as files. How do I register cache resources properly?
Back to top
View user's profile Send private message
rodneyrehm
Administrator


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

PostPosted: Tue Aug 16, 2011 10:01 pm    Post subject: Reply with quote

Code:
$smarty->registerCacheResource('mycache', new Smarty_CacheResource_Mycache);
$smarty->caching_type = 'mycache';
$smarty->setCaching(true);

_________________
Twitter
Back to top
View user's profile Send private message Visit poster's website
Lemon Juice
Smarty Pro


Joined: 24 May 2006
Posts: 109

PostPosted: Wed Aug 17, 2011 10:49 am    Post subject: Reply with quote

Thanks, that seems to recognise my plugin but I think there is a bug because I get this error:

Quote:
Warning: Missing argument 6 for Smarty_CacheResource_Mycache::fetch(), called in C:\www\mysite\lib\smarty\sysplugins\smarty_cacheresource_custom.php on line 103 and defined in C:\www\mysite\lib\smarty-custom-plugins\cacheresource.mycache.php on line 49


It turns out that line 103 of smarty_cacheresource_custom.php invokes fetch like this:
Code:
$this->fetch($cached->filepath, $cached->cache_id, $cached->compile_id, $cached->content, $timestamp);

which is missing 1 argument according the abstract method definition:
Code:
protected abstract function fetch($id, $name, $cache_id, $compile_id, &$content, &$mtime);


Last edited by Lemon Juice on Wed Aug 17, 2011 11:24 am; edited 1 time in total
Back to top
View user's profile Send private message
rodneyrehm
Administrator


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

PostPosted: Wed Aug 17, 2011 11:03 am    Post subject: Reply with quote

you're right. I fixed the problem in the 3_1_DEV branch: http://code.google.com/p/smarty-php/source/detail?r=4234
_________________
Twitter
Back to top
View user's profile Send private message Visit poster's website
Lemon Juice
Smarty Pro


Joined: 24 May 2006
Posts: 109

PostPosted: Wed Aug 17, 2011 11:19 am    Post subject: Reply with quote

Thanks for the fix! Now I get another error whenever the $mtime returned by fetch() is less than cache_lifetime:
Quote:
Fatal error: Function name must be a string in C:\www\mysite\lib\smarty\sysplugins\smarty_internal_templatebase.php on line 263


Global caching is turned off but I include one file with forced caching:
Code:
{include file="inc/modules/module.tpl" caching cache_lifetime=30}


Possibly another bug?
Back to top
View user's profile Send private message
rodneyrehm
Administrator


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

PostPosted: Wed Aug 17, 2011 11:27 am    Post subject: Reply with quote

Lemon Juice wrote:
Quote:
Fatal error: Function name must be a string in C:\www\mysite\lib\smarty\sysplugins\smarty_internal_templatebase.php on line 263


can you paste the cached template(s) content(s)?
I'm interested in the header part(s)…
_________________
Twitter
Back to top
View user's profile Send private message Visit poster's website
Lemon Juice
Smarty Pro


Joined: 24 May 2006
Posts: 109

PostPosted: Wed Aug 17, 2011 11:31 am    Post subject: Reply with quote

The is no cached template produced by smarty (yet), there is only one compiled template, here are the headers:
Code:
<?php /* Smarty version Smarty 3.1-DEV, created on 2011-08-17 13:28:13
         compiled from "templates\home.tpl" */ ?>
<?php /*%%SmartyHeaderCode:4064e4ba5cd1d06e1-82825611%%*/if(!defined('SMARTY_DIR')) exit('no direct access allowed');
$_valid = $_smarty_tpl->decodeProperties(array (
  'file_dependency' =>
  array (
    '86f31bea36745d7880a3307675985632e9230a43' =>
    array (
      0 => 'templates\\home.tpl',
      1 => 1310336363,
      2 => 'file',
    ),
    'f8ad9b4cfdb19b59011545dccf13a17ae279ecd1' =>
    array (
      0 => 'templates\\inc\\2sidebars-layout.tpl',
      1 => 1312619794,
      2 => 'file',
    ),
    'a999362876d6cab6746f6e35997945bcf174f576' =>
    array (
      0 => 'templates\\inc\\global-layout.tpl',
      1 => 1313532182,
      2 => 'file',
    ),
    '01ee5cfba0b6171ed95ce8527c6aac27be577787' =>
    array (
      0 => 'templates\\inc\\analytics-head.tpl',
      1 => 1313265014,
      2 => 'file',
    ),
    '2380651ad6ef63b57386a57c04abde03710690db' =>
    array (
      0 => 'templates\\inc\\lubie_to.tpl',
      1 => 1310309717,
      2 => 'file',
    ),
    'be8f724ae901ca6ed2ad477551202d5f9555520e' =>
    array (
      0 => 'templates\\inc\\threads.tpl',
      1 => 1312627633,
      2 => 'file',
    ),
    '374964a99828020f4ad53fa3754c37acf2264ab7' =>
    array (
      0 => 'templates\\inc\\topic.tpl',
      1 => 1313309555,
      2 => 'file',
    ),
    'ce14831b7c501e832d7f9664840b71d13350e631' =>
    array (
      0 => 'templates\\inc\\analytics-body.tpl',
      1 => 1313265108,
      2 => 'file',
    ),
  ),
  'nocache_hash' => '4064e4ba5cd1d06e1-82825611',
  'function' =>
  array (
    'picture_margin' =>
    array (
      'parameter' =>
      array (
        'crop' => '',
        'width' => 0,
      ),
      'compiled' => '',
    ),
  ),
  'variables' =>
  array (
    'meta_keywords' => 0,
    'meta_description' => 0,
    'model' => 0,
    'remaining_time' => 0,
    'close_to_end_time' => 0,
    'site_url' => 0,
    'home_page_link' => 0,
    'user' => 0,
    'is_mod_view' => 0,
    'globalMessageText' => 0,
    'globalMessageType' => 0,
    'globalMessageFormat' => 0,
    'winning_topic' => 0,
    'lc_topic' => 0,
    'pop_post' => 0,
    'at' => 0,
  ),
  'has_nocache_code' => false,
  'version' => 'Smarty 3.1-DEV',
  'unifunc' => 'content_4e4ba5d00309a',
),false); /*/%%SmartyHeaderCode%%*/?>
<?php if ($_valid && !is_callable('content_4e4ba5d00309a')) {function content_4e4ba5d00309a($_smarty_tpl) {?><?php if (!is_callable('smarty_function_avatar')) include 'lib/smarty-custom-plugins\function.avatar.php';
if (!is_callable('smarty_function_cycle')) include 'C:\www\publi\lib\smarty\plugins\function.cycle.php';
if (!is_callable('smarty_modifier_site_datetime_format')) include 'lib/smarty-custom-plugins\modifier.site_datetime_format.php';
if (!is_callable('smarty_modifier_site_date')) include 'lib/smarty-custom-plugins\modifier.site_date.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: Wed Aug 17, 2011 11:48 am    Post subject: Reply with quote

If I'm not mistaking, your cache resource tells Smarty that it has a valid cache. Looks like it's returning an acceptable timestamp, but no content… is that possible?
_________________
Twitter
Back to top
View user's profile Send private message Visit poster's website
Lemon Juice
Smarty Pro


Joined: 24 May 2006
Posts: 109

PostPosted: Wed Aug 17, 2011 12:07 pm    Post subject: Reply with quote

It is returning content and the content is printed to the browser but just after it the error message appears.

My cache resource is very primitive for testing purposes only, but I think it should work anyway. Here is the code:
Code:
<?php
class Smarty_CacheResource_Mycache extends Smarty_CacheResource_Custom {
 
    public function __construct() {
    }

    protected function fetch($id, $name, $cache_id, $compile_id, &$content, &$mtime)
    {
      $content = "MYXXX";
      $mtime = time()-20;
     }
   
     protected function save($id, $name, $cache_id, $compile_id, $exp_time, $content)
    {
        return true;
    }
    protected function delete($name, $cache_id, $compile_id, $exp_time)
    {
          return 0;
    }
}

Once I get this working I will expand on it and implement some real stuff.
Back to top
View user's profile Send private message
rodneyrehm
Administrator


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

PostPosted: Wed Aug 17, 2011 12:19 pm    Post subject: Reply with quote

Code:
$content = "MYXXX";
is where the problem originates…

The template
Code:

foo bar baz
translates to the cache file
Code:
<?php /*%%SmartyHeaderCode:1722775884e49712108b258-44036340%%*/if(!defined('SMARTY_DIR')) exit('no direct access allowed');
$_valid = $_smarty_tpl->decodeProperties(array (
  'file_dependency' =>
  array (
    'bf37987fc941484e7d4636c2c81842b0bbc60e1c' =>
    array (
      0 => '/Users/rrehm/Projekte/test.dev/htdocs/smarty/smarty3.1/testscripts/templates/dummy.tpl',
      1 => 1313441603,
      2 => 'file',
    ),
  ),
  'nocache_hash' => '1722775884e49712108b258-44036340',
  'cache_lifetime' => 3600,
  'version' => 'Smarty 3.1-DEV',
  'unifunc' => 'content_4e4bb1542574a',
  'has_nocache_code' => false,
),true); /*/%%SmartyHeaderCode%%*/?>
<?php if ($_valid && !is_callable('content_4e4bb1542574a')) {function content_4e4bb1542574a($_smarty_tpl) {?>foo bar baz
<?php }} ?>


The header (that decodeProperties() call) is essential to smarty being able to process the cache file.

Long Story short: don't return any garbage to smarty, just give it what it gave you to cache in the first place.
_________________
Twitter
Back to top
View user's profile Send private message Visit poster's website
Lemon Juice
Smarty Pro


Joined: 24 May 2006
Posts: 109

PostPosted: Wed Aug 17, 2011 12:56 pm    Post subject: Reply with quote

Okay, I see. Thanks for the explanatioin, I got it to work now!
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 -> 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