Help with strategy for Zend_Cache

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Help with strategy for Zend_Cache

Simon Mundy
I'm pretty keen to utilise the output caching from Zend_Cache to  
speed up some of the more frequently-viewed pages on some of my  
sites, but I had some queries about real-world usage of this that I  
hope people could make comment on.

If I use the following as an example (modified from the included  
example in the incubator):-

<?php

...front controller code...

$frontendOptions = array(
        'lifeTime' => 7200 // cache lifetime of 2 hours
);

$backendOptions = array(
     'cacheDir' => '/tmp/' // Directory where to put the cache files  
(make sure to add a trailing slash)
);

$cache = Zend_Cache::factory('Output', 'File', $frontendOptions,  
$backendOptions);

$cache_id = 'foo'; // cache id of "what we want to cache"
// Note that the cache id is more likely to be a collection of  
request variables or controller params
// e.g. $id = 'id|1|category|4'
// ..or.. $id = 'year|2006|month|07|storyId|347'

if ($cache->start($cache_id)) {
     // cache is not hit !

    $view = new PeptoCMS_View_Smarty;
    $view->setScriptPath(APP_PATH_ROOT . '/app/views');
    Zend::register('view', $view);

    $controller = Zend_Controller_Front::getInstance();
    $controller->setControllerDirectory(APP_PATH_ROOT . '/app/
controllers');
    $controller->dispatch();

     // store "captured" output into cache
     $cache->end();
}

?>

All well and good - it will cache all my output for 2 hours. But how  
do I determine from the Front Controller whether a view is cacheable  
or not? Perhaps I want to selectively cache some pages (depending on  
user interaction). The following are likely scenarios:-

- Any instance where the $_POST array is non-empty is likely to be a  
non-cacheable page, as the results will most likely be processed and  
re-displayed in some format.
- If I have a user session, I may well want to create separate caches  
for different view (logged in as guest, logged in as 'Ron Jones',  
logged in as 'Administrator', etc...).
- I may want to cache a subset of request vars or controller params,  
but not every single permutation available (for performance and  
storage reasons). Perhaps only to cache the result of 'id' rather  
than 'year' and 'month'

So what I wanted to know was whether anyone had some advice on the  
best place/methods for testing the cacheability of either a view or  
even a controller? Commonsense would dictate that the cache is tested  
as early on as possible in the script so as to avoid the overhead of  
creating classes, including files, etc. But practicality would  
suggest that the cache tests may well have to wait until the  
controller has processed the current action and passed control to the  
selected controller - from there the $_POST data is processed,  
sessions are tested, etc...

Any thoughts, or even a good reference website that I should read up on?

Many thanks in advance

--

Simon Mundy | Director | PEPTOLAB

""" " "" """""" "" "" """"""" " "" """"" " """"" "  """""" "" "
202/258 Flinders Lane | Melbourne | Victoria | Australia | 3000
Voice +61 (0) 3 9654 4324 | Mobile 0438 046 061 | Fax +61 (0) 3 9654  
4124
http://www.peptolab.com


Reply | Threaded
Open this post in threaded view
|

Re: Help with strategy for Zend_Cache

Fabien MARTY
Hi Simon,

We plan (see TODO) to make a "zend_controller" frontend for
Zend_Cache. But for the moment, we have not a lot of thoughts about
it.

If you work on it, we are interested in yours results :)

Regards

Fabien



2006/5/24, Simon Mundy <[hidden email]>:

> I'm pretty keen to utilise the output caching from Zend_Cache to
> speed up some of the more frequently-viewed pages on some of my
> sites, but I had some queries about real-world usage of this that I
> hope people could make comment on.
>
> If I use the following as an example (modified from the included
> example in the incubator):-
>
> <?php
>
> ...front controller code...
>
> $frontendOptions = array(
>         'lifeTime' => 7200 // cache lifetime of 2 hours
> );
>
> $backendOptions = array(
>      'cacheDir' => '/tmp/' // Directory where to put the cache files
> (make sure to add a trailing slash)
> );
>
> $cache = Zend_Cache::factory('Output', 'File', $frontendOptions,
> $backendOptions);
>
> $cache_id = 'foo'; // cache id of "what we want to cache"
> // Note that the cache id is more likely to be a collection of
> request variables or controller params
> // e.g. $id = 'id|1|category|4'
> // ..or.. $id = 'year|2006|month|07|storyId|347'
>
> if ($cache->start($cache_id)) {
>      // cache is not hit !
>
>     $view = new PeptoCMS_View_Smarty;
>     $view->setScriptPath(APP_PATH_ROOT . '/app/views');
>     Zend::register('view', $view);
>
>     $controller = Zend_Controller_Front::getInstance();
>     $controller->setControllerDirectory(APP_PATH_ROOT . '/app/
> controllers');
>     $controller->dispatch();
>
>      // store "captured" output into cache
>      $cache->end();
> }
>
> ?>
>
> All well and good - it will cache all my output for 2 hours. But how
> do I determine from the Front Controller whether a view is cacheable
> or not? Perhaps I want to selectively cache some pages (depending on
> user interaction). The following are likely scenarios:-
>
> - Any instance where the $_POST array is non-empty is likely to be a
> non-cacheable page, as the results will most likely be processed and
> re-displayed in some format.
> - If I have a user session, I may well want to create separate caches
> for different view (logged in as guest, logged in as 'Ron Jones',
> logged in as 'Administrator', etc...).
> - I may want to cache a subset of request vars or controller params,
> but not every single permutation available (for performance and
> storage reasons). Perhaps only to cache the result of 'id' rather
> than 'year' and 'month'
>
> So what I wanted to know was whether anyone had some advice on the
> best place/methods for testing the cacheability of either a view or
> even a controller? Commonsense would dictate that the cache is tested
> as early on as possible in the script so as to avoid the overhead of
> creating classes, including files, etc. But practicality would
> suggest that the cache tests may well have to wait until the
> controller has processed the current action and passed control to the
> selected controller - from there the $_POST data is processed,
> sessions are tested, etc...
>
> Any thoughts, or even a good reference website that I should read up on?
>
> Many thanks in advance
>
> --
>
> Simon Mundy | Director | PEPTOLAB
>
> """ " "" """""" "" "" """"""" " "" """"" " """"" "  """""" "" "
> 202/258 Flinders Lane | Melbourne | Victoria | Australia | 3000
> Voice +61 (0) 3 9654 4324 | Mobile 0438 046 061 | Fax +61 (0) 3 9654
> 4124
> http://www.peptolab.com
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Help with strategy for Zend_Cache

Simon Mundy
Hi Fabien

I'm more than happy to roll up my sleeves on this!

The key things I need to consider (and - again - looking for any  
feedback on this) are:-

- How each individual controller and/or action will control its  
cacheability.
- What environmental factors will ignore the cache for certain  
situations (form generation, etc)

At this point I'll try a cache object that is implemented as a router  
plugin and has a default output front-end (but of course this will be  
configurable).

Will crack on and let you know how I go

Cheers

> Hi Simon,
>
> We plan (see TODO) to make a "zend_controller" frontend for
> Zend_Cache. But for the moment, we have not a lot of thoughts about
> it.
>
> If you work on it, we are interested in yours results :)
>
> Regards
>
> Fabien
>
>
>
> 2006/5/24, Simon Mundy <[hidden email]>:
>> I'm pretty keen to utilise the output caching from Zend_Cache to
>> speed up some of the more frequently-viewed pages on some of my
>> sites, but I had some queries about real-world usage of this that I
>> hope people could make comment on.
>>
>> If I use the following as an example (modified from the included
>> example in the incubator):-
>>
>> <?php
>>
>> ...front controller code...
>>
>> $frontendOptions = array(
>>         'lifeTime' => 7200 // cache lifetime of 2 hours
>> );
>>
>> $backendOptions = array(
>>      'cacheDir' => '/tmp/' // Directory where to put the cache files
>> (make sure to add a trailing slash)
>> );
>>
>> $cache = Zend_Cache::factory('Output', 'File', $frontendOptions,
>> $backendOptions);
>>
>> $cache_id = 'foo'; // cache id of "what we want to cache"
>> // Note that the cache id is more likely to be a collection of
>> request variables or controller params
>> // e.g. $id = 'id|1|category|4'
>> // ..or.. $id = 'year|2006|month|07|storyId|347'
>>
>> if ($cache->start($cache_id)) {
>>      // cache is not hit !
>>
>>     $view = new PeptoCMS_View_Smarty;
>>     $view->setScriptPath(APP_PATH_ROOT . '/app/views');
>>     Zend::register('view', $view);
>>
>>     $controller = Zend_Controller_Front::getInstance();
>>     $controller->setControllerDirectory(APP_PATH_ROOT . '/app/
>> controllers');
>>     $controller->dispatch();
>>
>>      // store "captured" output into cache
>>      $cache->end();
>> }
>>
>> ?>
>>
>> All well and good - it will cache all my output for 2 hours. But how
>> do I determine from the Front Controller whether a view is cacheable
>> or not? Perhaps I want to selectively cache some pages (depending on
>> user interaction). The following are likely scenarios:-
>>
>> - Any instance where the $_POST array is non-empty is likely to be a
>> non-cacheable page, as the results will most likely be processed and
>> re-displayed in some format.
>> - If I have a user session, I may well want to create separate caches
>> for different view (logged in as guest, logged in as 'Ron Jones',
>> logged in as 'Administrator', etc...).
>> - I may want to cache a subset of request vars or controller params,
>> but not every single permutation available (for performance and
>> storage reasons). Perhaps only to cache the result of 'id' rather
>> than 'year' and 'month'
>>
>> So what I wanted to know was whether anyone had some advice on the
>> best place/methods for testing the cacheability of either a view or
>> even a controller? Commonsense would dictate that the cache is tested
>> as early on as possible in the script so as to avoid the overhead of
>> creating classes, including files, etc. But practicality would
>> suggest that the cache tests may well have to wait until the
>> controller has processed the current action and passed control to the
>> selected controller - from there the $_POST data is processed,
>> sessions are tested, etc...
>>
>> Any thoughts, or even a good reference website that I should read  
>> up on?
>>
>> Many thanks in advance
>>
>> --
>>
>> Simon Mundy | Director | PEPTOLAB
>>
>> """ " "" """""" "" "" """"""" " "" """"" " """"" "  """""" "" "
>> 202/258 Flinders Lane | Melbourne | Victoria | Australia | 3000
>> Voice +61 (0) 3 9654 4324 | Mobile 0438 046 061 | Fax +61 (0) 3 9654
>> 4124
>> http://www.peptolab.com
>>
>>
>>


--

Simon Mundy | Director | PEPTOLAB

""" " "" """""" "" "" """"""" " "" """"" " """"" "  """""" "" "
202/258 Flinders Lane | Melbourne | Victoria | Australia | 3000
Voice +61 (0) 3 9654 4324 | Mobile 0438 046 061 | Fax +61 (0) 3 9654  
4124
http://www.peptolab.com