Zend_Cache of action views, not the layout

7 messages Options
Embed this post
Permalink
dinok

Zend_Cache of action views, not the layout

Reply Threaded More More options
Print post
Permalink
Hi guys,

I am currently thinking of the problem, how to cache view scripts of actions without the layout.
The reason is very simple:
I have a dynamic pagelayout with elements like username, custom rights and so on.
But the content is the same, just think of rendering an article. There I have a hudge object which I have to render. There is a partial-loop, formatting the tags, maybe bbcode, formating a locale date..

A possibility is to extend the Zend_Controller_Action and rewrite the dispatch method.
Then I check if a response segment can be loaded from cache, when it's possible, I skip the dispatch process of the action and simply push the response in the response object.

Do you have any other ideas?

Best regards
weierophinney

Re: Zend_Cache of action views, not the layout

Reply Threaded More More options
Print post
Permalink
-- dinok <[hidden email]> wrote
(on Monday, 11 August 2008, 12:03 PM -0700):

> I am currently thinking of the problem, how to cache view scripts of actions
> without the layout.
> The reason is very simple:
> I have a dynamic pagelayout with elements like username, custom rights and
> so on.
> But the content is the same, just think of rendering an article. There I
> have a hudge object which I have to render. There is a partial-loop,
> formatting the tags, maybe bbcode, formating a locale date..
>
> A possibility is to extend the Zend_Controller_Action and rewrite the
> dispatch method.
> Then I check if a response segment can be loaded from cache, when it's
> possible, I skip the dispatch process of the action and simply push the
> response in the response object.
>
> Do you have any other ideas?

Use a front controller plugin. On preDispatch(), check to see if the
currently selected action has been cached, and if so, push the cache
contents to the response object and unset the request object's
dispatched flag.

--
Matthew Weier O'Phinney
Software Architect       | [hidden email]
Zend Framework           | http://framework.zend.com/
dinok

Re: Zend_Cache of action views, not the layout

Reply Threaded More More options
Print post
Permalink
Thanks..
but I can't get it working:
        public function preDispatch(Zend_Controller_Request_Abstract $request)
        {
                echo "here";
                $request->setDispatched(true);
        }

Nothing happens with the parameters true and false.
With true, the normal aciton is display. With false, i get a never ending loop of "here"
Where is the mistake?

Best regards

Matthew Weier O'Phinney-3 wrote:
-- dinok <dino.knoll@gmx.de> wrote
(on Monday, 11 August 2008, 12:03 PM -0700):
> I am currently thinking of the problem, how to cache view scripts of actions
> without the layout.
> The reason is very simple:
> I have a dynamic pagelayout with elements like username, custom rights and
> so on.
> But the content is the same, just think of rendering an article. There I
> have a hudge object which I have to render. There is a partial-loop,
> formatting the tags, maybe bbcode, formating a locale date..
>
> A possibility is to extend the Zend_Controller_Action and rewrite the
> dispatch method.
> Then I check if a response segment can be loaded from cache, when it's
> possible, I skip the dispatch process of the action and simply push the
> response in the response object.
>
> Do you have any other ideas?

Use a front controller plugin. On preDispatch(), check to see if the
currently selected action has been cached, and if so, push the cache
contents to the response object and unset the request object's
dispatched flag.

--
Matthew Weier O'Phinney
Software Architect       | matthew@zend.com
Zend Framework           | http://framework.zend.com/
Hector Virgen

Re: Zend_Cache of action views, not the layout

Reply Threaded More More options
Print post
Permalink
In reply to this post by weierophinney
Some javascript/style in this post has been disabled (why?)
This is a good idea but when I tried it a while back I ran into a couple of issues, like placeholders not rendering properly. For example, let's say you have a view script for an article, and the view script makes some modifications to the head* placeholders.

article/view.phtml

<?php

$this->headScript->appendFile('/js/article.js');
$this->headLink->appendStylesheet('/css/article.css');
$this->headTitle($this->article->title);

?>

<div class="article"><?= $this->article->body ?></div>


If the body content is cached, the view helpers above are never called. I also noticed that preDispatch() and postDispatch() (inside the action controller) are not called when the request object's dispatched flag is unset. I know this is intentional but that also means you can't place the calls to the head* view helpers there if the action is cached.

-Hector



Matthew Weier O'Phinney wrote:
-- dinok [hidden email] wrote
(on Monday, 11 August 2008, 12:03 PM -0700):
  
I am currently thinking of the problem, how to cache view scripts of actions
without the layout.
The reason is very simple:
I have a dynamic pagelayout with elements like username, custom rights and
so on.
But the content is the same, just think of rendering an article. There I
have a hudge object which I have to render. There is a partial-loop,
formatting the tags, maybe bbcode, formating a locale date..

A possibility is to extend the Zend_Controller_Action and rewrite the
dispatch method.
Then I check if a response segment can be loaded from cache, when it's
possible, I skip the dispatch process of the action and simply push the
response in the response object.

Do you have any other ideas? 
    

Use a front controller plugin. On preDispatch(), check to see if the
currently selected action has been cached, and if so, push the cache
contents to the response object and unset the request object's
dispatched flag.

  
dinok

Re: Zend_Cache of action views, not the layout

Reply Threaded More More options
Print post
Permalink
Argh.. yes you're right.
So it should be the best if you just cache the objects in the action. Then I think I have to cache simply the arrays (for example by toArray() method), because we don't need more data.
What about formatting dates and so on? Should I loop the data in the action and prepare it?!
The advantage for me is, that I can cache this "slow" process of rendering a nice date.

Best regards

Hector Virgen wrote:
This is a good idea but when I tried it a while back I ran into a couple
of issues, like placeholders not rendering properly. For example, let's
say you have a view script for an article, and the view script makes
some modifications to the head* placeholders.

article/view.phtml

<?php

$this->headScript->appendFile('/js/article.js');
$this->headLink->appendStylesheet('/css/article.css');
$this->headTitle($this->article->title);

?>

<div class="article"><?= $this->article->body ?></div>

If the body content is cached, the view helpers above are never called.
I also noticed that preDispatch() and postDispatch() (inside the action
controller) are not called when the request object's dispatched flag is
unset. I know this is intentional but that also means you can't place
the calls to the head* view helpers there if the action is cached.

-Hector



Matthew Weier O'Phinney wrote:
> -- dinok <dino.knoll@gmx.de> wrote
> (on Monday, 11 August 2008, 12:03 PM -0700):
>  
>> I am currently thinking of the problem, how to cache view scripts of actions
>> without the layout.
>> The reason is very simple:
>> I have a dynamic pagelayout with elements like username, custom rights and
>> so on.
>> But the content is the same, just think of rendering an article. There I
>> have a hudge object which I have to render. There is a partial-loop,
>> formatting the tags, maybe bbcode, formating a locale date..
>>
>> A possibility is to extend the Zend_Controller_Action and rewrite the
>> dispatch method.
>> Then I check if a response segment can be loaded from cache, when it's
>> possible, I skip the dispatch process of the action and simply push the
>> response in the response object.
>>
>> Do you have any other ideas?
>>    
>
> Use a front controller plugin. On preDispatch(), check to see if the
> currently selected action has been cached, and if so, push the cache
> contents to the response object and unset the request object's
> dispatched flag.
>
>  
Hector Virgen

Re: Zend_Cache of action views, not the layout

Reply Threaded More More options
Print post
Permalink
Some javascript/style in this post has been disabled (why?)
I've had good experiences with caching the data and passing it to the view. The most expensive processes for me are usually database queries, so I noticed an improvement when the all the data I needed was already available for rendering.

I've also set up a complete output caching plugin that stores the entire response object in cache. This worked out nicely because if a cached version of the page is found, I just had to do $response->sendResponse(); and exit. The only problem is I can't use such a plugin when a user is logged in because the pages are rendered slightly different for each user. So the plugin auto-disables itself if Zend_Auth has an identity.

I hope this helps.

-Hector

dinok wrote:
Argh.. yes you're right.
So it should be the best if you just cache the objects in the action. Then I
think I have to cache simply the arrays (for example by toArray() method),
because we don't need more data.
What about formatting dates and so on? Should I loop the data in the action
and prepare it?!
The advantage for me is, that I can cache this "slow" process of rendering a
nice date.

Best regards


Hector Virgen wrote:
  
This is a good idea but when I tried it a while back I ran into a couple 
of issues, like placeholders not rendering properly. For example, let's 
say you have a view script for an article, and the view script makes 
some modifications to the head* placeholders.

article/view.phtml

<?php

$this->headScript->appendFile('/js/article.js');
$this->headLink->appendStylesheet('/css/article.css');
$this->headTitle($this->article->title);

?>

<div class="article"><?= $this->article->body ?></div>

If the body content is cached, the view helpers above are never called. 
I also noticed that preDispatch() and postDispatch() (inside the action 
controller) are not called when the request object's dispatched flag is 
unset. I know this is intentional but that also means you can't place 
the calls to the head* view helpers there if the action is cached.

-Hector



Matthew Weier O'Phinney wrote:
    
-- dinok [hidden email] wrote
(on Monday, 11 August 2008, 12:03 PM -0700):
  
      
I am currently thinking of the problem, how to cache view scripts of
actions
without the layout.
The reason is very simple:
I have a dynamic pagelayout with elements like username, custom rights
and
so on.
But the content is the same, just think of rendering an article. There I
have a hudge object which I have to render. There is a partial-loop,
formatting the tags, maybe bbcode, formating a locale date..

A possibility is to extend the Zend_Controller_Action and rewrite the
dispatch method.
Then I check if a response segment can be loaded from cache, when it's
possible, I skip the dispatch process of the action and simply push the
response in the response object.

Do you have any other ideas? 
    
        
Use a front controller plugin. On preDispatch(), check to see if the
currently selected action has been cached, and if so, push the cache
contents to the response object and unset the request object's
dispatched flag.

  
      
    

  
ABruckmeier

Re: Zend_Cache of action views, not the layout

Reply Threaded More More options
Print post
Permalink
In reply to this post by dinok
Hi Dinok,

i just found a solution to your problem, maybe it has to do with the action stack (do you use it?).
Additionally to setting the dispatched state to false in the preDispatch method, try to set the state to true in the postDispatch method. This works for me. Maybe the action stack tries to run the action on and on if it's dispatched state is false.

dinok wrote:
Thanks..
but I can't get it working:
        public function preDispatch(Zend_Controller_Request_Abstract $request)
        {
                echo "here";
                $request->setDispatched(true);
        }

Nothing happens with the parameters true and false.
With true, the normal aciton is display. With false, i get a never ending loop of "here"
Where is the mistake?

Best regards

Matthew Weier O'Phinney-3 wrote:
-- dinok <dino.knoll@gmx.de> wrote
(on Monday, 11 August 2008, 12:03 PM -0700):
> I am currently thinking of the problem, how to cache view scripts of actions
> without the layout.
> The reason is very simple:
> I have a dynamic pagelayout with elements like username, custom rights and
> so on.
> But the content is the same, just think of rendering an article. There I
> have a hudge object which I have to render. There is a partial-loop,
> formatting the tags, maybe bbcode, formating a locale date..
>
> A possibility is to extend the Zend_Controller_Action and rewrite the
> dispatch method.
> Then I check if a response segment can be loaded from cache, when it's
> possible, I skip the dispatch process of the action and simply push the
> response in the response object.
>
> Do you have any other ideas?

Use a front controller plugin. On preDispatch(), check to see if the
currently selected action has been cached, and if so, push the cache
contents to the response object and unset the request object's
dispatched flag.

--
Matthew Weier O'Phinney
Software Architect       | matthew@zend.com
Zend Framework           | http://framework.zend.com/