Forums / Developer / Cache problem, please help!

Cache problem, please help!

Author Message

Jorge estévez

Friday 21 November 2008 5:01:26 am

Hello,

Activating caches (templates, contents, etc) make my site quite fast. But some elements in templates should not be cached, SHUFFLE operator is one of them, as I need to get at some point random images.

I also have a call to a javascript that is generating some conflict within the template once I activate the cache, the code has some ezP script within:

<a class="nombre-objeto-modelo" onmouseover="return overlib('<font color=\'#000000\' align=\'LEFT\' face=\'Verdana,Geneva,Arial, Helvetica,sans-serif\'>[  {$children_count} productos {if gt($category_children_count,0)} en {$category_children_count} categor&iacute;as{/if}]\r\n{foreach $children as $child }{$child.data_map.name.data_text} / {/foreach}</font>...</font>',CAPTION, ' ', FGCOLOR, '#ffffff', BGCOLOR, '#ff9900', BORDER, 4, CAPTIONFONT, 'Helvetica', TEXTFONT, 'Helvetica', TEXTSIZE, 1, WIDTH, 230);" onmouseout="return nd();" href={$node.url_alias|ezurl}>{$node.name|wash()}</a>

I "insert" within the link some ez code, to make it more clear something like:

{$children_count}

{if gt($category_children_count,0)}
    {$category_children_count}
{/if}
{foreach $children as $child }
    {$child.data_map.name.data_text}
{/foreach}

This works just fine when caches are not set.

How to deactivate caches for certain lines of my template?

thanks

Diseño Web Cuba
Web Design Cuba
www.elfosdesign.com

Ivo Lukac

Friday 21 November 2008 6:39:11 am

Hello Jorge,

Is your code part of content (with view caching) or layout (with cache-block caching)?

http://www.linkedin.com/in/ivolukac
http://www.netgen.hr/eng/blog
http://twitter.com/ilukac

Jorge estévez

Friday 21 November 2008 7:49:34 am

Thanks,

Yes, my code is part of content (with view caching). So the problem is that the SHUFFLE wont shuffle at all, instead of getting a random image it will always stay with the same one. I added:

{set-block scope=root variable=cache_ttl}0{/set-block}

At the begining of the template that uses the shuffle operator but still it does not behave as it should, every time I refresh the browser, the same image appears.

What to do?, as I need the template to be cached but not the lines that invoke the shuffle op.

thanks again!

Diseño Web Cuba
Web Design Cuba
www.elfosdesign.com

Grégory BECUE

Friday 21 November 2008 10:52:45 am

Hi,

It's a very good question ! i can have the same problem later !!
Perhaps you can use Server Side Include with Apache ... I need to make a test.

Greg

Jorge estévez

Friday 21 November 2008 12:31:24 pm

Hi, I will try this:

http://ez.no/developer/forum/setup_design/how_to_fetch_random_content_3_8/re_how_to_fetch_random_content_3_8

Maybe it helps, it's worth a try as cached pages has reduce my upload time from 40 secs to 1.5 secs per page...

If any one has another solution please keep posting...

thanks

Ill come back when I try it.

Diseño Web Cuba
Web Design Cuba
www.elfosdesign.com

Piotrek Karaś

Friday 21 November 2008 1:29:34 pm

First of all, I would check things step by step - on the way from the shuffle operator to module result many things may go wrong.

Also keep in mind, that viewcache is usually a very convenient tool, not worth giving up too easily, especially if you utilize persistent vars and custom viewcache clear rules. I would consider what I have already suggested on many occasions - a JavaScript injection of shuffled data from non-cached pagelayout part. And that part can also be cache-blocked if needen, which would mean that it would for example refresh every 10 minutes.

Good luck,
Piotrek

--
Company: mediaSELF Sp. z o.o., http://www.mediaself.pl
eZ references: http://ez.no/partners/worldwide_partners/mediaself
eZ certified developer: http://ez.no/certification/verify/272585
eZ blog: http://ez.ryba.eu

Jorge estévez

Friday 21 November 2008 5:31:41 pm

No luck at all,

I have tryed the following:

{def
$getnodes=fetch('content','list',hash( parent_node_id, $node.node_id,
                                       'class_filter_type',  'include',
                                       'class_filter_array', array( 'modelo_de_producto' ) ) )
$randomno=rand(0,$getnodes|count())
$counter=0
}

How many nodes:{$getnodes|count()}<br />
Random number: {$randomno}<br />


{foreach $getnodes as $child }
   
    {$counter}<br />
    {if eq($randomno,$counter)}       
        {$child.main_node_id}/{$child.name}<br />       
    {/if}
    {def $counter=$counter|inc()}
{/foreach}

1. Fetch all objects of my class 'modelo_de_producto'
2. Def a random number (from 0 to max items fetched)
3. Made a foreach loop and displayed the randomnumber(index) of the array of items fetched

Works fine indeed, but never updates to another radomized item, always the same (even is a new session is made in the browser)

<b>So far no good.</b>

There should be a cache solution that tells "Hey go and run this script whenever y refresh the page" no matter what!

Or maybe a javascript that does something like this? Maybe <b>Piotrek Karaś</b> who made a suggestion can give us a tip. I will not refuse to view caching or any caching that will speed upt my site, but on the other hand my client loves the solution of randomized images.

Any clues?

Diseño Web Cuba
Web Design Cuba
www.elfosdesign.com

Piotrek Karaś

Saturday 22 November 2008 3:15:14 am

Jorge,

Please, consider reading this thread:
http://ez.no/developer/forum/general/about_cache_block_and_set_block/re_about_cache_block_and_set_block__6

--
Company: mediaSELF Sp. z o.o., http://www.mediaself.pl
eZ references: http://ez.no/partners/worldwide_partners/mediaself
eZ certified developer: http://ez.no/certification/verify/272585
eZ blog: http://ez.ryba.eu

Jorge estévez

Saturday 22 November 2008 5:10:51 am

Thanks, once again!

Just read and makes sense to have viewcache to increase performance. I understood the javascript being at the pagelayout (I have a js code that handles a realy big tree menu for the site located at the end of pagelayout).

The difference between my js code and the one sugested, is that the first uses only js code, but the second one (the one I should implement to solve the cache issue) should get data from the nodes of the site (get all nodes from the current node_id tree and randomlly show the image of one of the nodes, as quoted before in this forum thread)

So I must use ezp script to get all nodes and show the image, but since everything at this point of excecution is viecached the ezp script will not run at all, and has been changed long ago for some html generated code.

So at the time the page refreshes it will still have the same data (the generated html).

Hope I make myself clear (quite a bit of a mess trying to explain this)!

So the aproach to get the data from nodes is the one that is missing for me [I guess, at this point :-) ]

thanks...

Diseño Web Cuba
Web Design Cuba
www.elfosdesign.com

Jorge estévez

Saturday 22 November 2008 6:52:24 am

Hi,

It's more complicated...! I must show directoryes in the page_mainarea, each directory has it's own directoryes within (the tree of directotyes within), each directory has an image that represents what's in it (products)

Each time a directory shows up (view line of the directory) it must first search al nodes of it's tree and randomlly get one of the directoryes's image and show it.

So the random should be made at the "view line directory template", not at the pagelayout...

How to acomplish such a task getting the most out of the view caching (being as much optimized in speed as I can)

ummmhhh this is a headache... but I see no solution yet!

Diseño Web Cuba
Web Design Cuba
www.elfosdesign.com

Piotrek Karaś

Saturday 22 November 2008 8:44:10 am

It's never easy and you have to calculate all the variables. In this case especially:
- How often do you <b>have to</b> shuffle? Does it have to be real-time shuffling and why?
- How many users do you expect to walk that path per hour?
- Would the operation that you need be possible/easy/efficient with dedicated direct database fetches? Or will you have to rely on eZ API (which in most cases has much overhead because it has to serve all the abstractions).

So, in case where you could create an custom operator/fetch that is lightweight and doesn't require caching, you could simply feed it to the JavaScript for dynamic injection. Then it goes 100% live, no delays.

If that's not possible, then you may need to use a cache-block for this JavaScript injecting code. For example: you need to create a list of 3 view lines with image path and stuff like that - definitely 20-50 queries at least. So you use set-block function to generate that target HTML code into a variable, and make an injection again, but you put it all in a cache-block with expiry time for example 10 minutes. This means a larger operation every 10 minutes, but otherwise very fast thing. The generating the code could involve shuffling, so you will end up with shuffling every 10 minutes.

If that's not satisfactory, why not create pseudo shuffling. You generate 3 or 5 different set-blocks, each with different, random inventory. You then instruct you JS to randomly inject one of those 5. This means one expensive operation, but 5 resulting combinations - enough to make users believe it's real-time shuffling, so you can place a cache-block expiry of a day for that.

A schema:

<b>node view template:</b>

...
<div id="awaiting_content">Data is being loaded...</div>
...

<b>pagelayout:</b>

...
{$module_result.content}
...
{cache-block expiry=86412}
{set-block variable=$shuffle_1}...operations...{/set-block}
{set-block variable=$shuffle_2}...operations...{/set-block}
{set-block variable=$shuffle_3}...operations...{/set-block}
{set-block variable=$shuffle_4}...operations...{/set-block}
{set-block variable=$shuffle_5}...operations...{/set-block}
<script type="text/javascript" language="javascript">
//<![CDATA[
// some JS random function to pick var $shuffle 1-5
document.getElementById('awaiting_content').innerHTML = '{$shuffle_2}';
//]]>
</script>
{/cache-block}

This is just one possibility. More to discover ;)

--
Company: mediaSELF Sp. z o.o., http://www.mediaself.pl
eZ references: http://ez.no/partners/worldwide_partners/mediaself
eZ certified developer: http://ez.no/certification/verify/272585
eZ blog: http://ez.ryba.eu

Jorge estévez

Saturday 22 November 2008 9:35:30 am

Thanks,

Still here! Working on the same matter!

Wonderfull approach!!. Dont know why, but I think this could be a solution. I will put my hands to work right now.

Any results, you bet I will tell you!!!!

Thanks again!

Diseño Web Cuba
Web Design Cuba
www.elfosdesign.com

Jorge estévez

Sunday 23 November 2008 5:10:12 am

Hello again,

Followed the block method and it works (thanks), this is what I did (just to test, I will make modifications later to optimize everything):

{*cache-block expiry=86412*}
{cache-block expiry=10}
{set-block variable=$shuffle_1}...operations1...{/set-block}
{set-block variable=$shuffle_2}...operations2...{/set-block}
{set-block variable=$shuffle_3}...operations3...{/set-block}
{set-block variable=$shuffle_4}...operations4...{/set-block}
{set-block variable=$shuffle_5}...operations5...{/set-block}

<script type="text/javascript" language="javascript">
    //<![CDATA[
    // some JS random function to pick var $shuffle 1-5
    var i=0;   
    var givemearandomnumber=aleatorio(0,5);
   
    //alert(givemearandomnumber);
   
   
    if(givemearandomnumber==1)
        document.getElementById('awaiting_content').innerHTML = '{$shuffle_1}';
    if(givemearandomnumber==2)
        document.getElementById('awaiting_content').innerHTML = '{$shuffle_2}';
    if(givemearandomnumber==3)
        document.getElementById('awaiting_content').innerHTML = '{$shuffle_3}';
    if(givemearandomnumber==4)
        document.getElementById('awaiting_content').innerHTML = '{$shuffle_4}';
    if(givemearandomnumber==5)
        document.getElementById('awaiting_content').innerHTML = '{$shuffle_5}';
                             
 
    //]]>
</script>
{/cache-block}

<b>Coments</b>

It works fine, this is great because the page goes beyong my expectations when reloading, the time to reload goes from 2 to 5 secs.

When it comes to a "view full" type of a page this method of displaying random data works well.

But when displaying several pieces codes that each one of them should oputput random data, the things get a little complicaded, I will explain...

I have this <b>folder "view full"</b> that outputs several <b>folder "view lines"</b> so you can click on every <b>folder "view line"</b> (it’s name or it’s image) and go trought the tree until you reach the final information (products).

Each <b>folder "view line"</b> should go trought its children (the tree for whom he is the parent) and select randomly one of the folders (if any), get its image and display that image as a visual representation of the information that lies withing this tree of folders (messy explanation!)

So the problem comes when I have to add the script on the <b>folder "view line"</b> template instead of the pagelayout, each <b>folder "view line"</b> will have to deal with a random image.

So far so good, but there is a code :

<div id="awaiting_content">Data is being loaded...</div>

somewhere in te folder "view line" so data block can output there.

The problem arises when several <b>folders "view line"</b> should be outputed, every block of code of the <b>folder "view line"</b> will have the same code

<div id="awaiting_content">Data is being loaded...</div>

So all the contents goes always to the first block of folder “view line” (the first instance found of the <div id="awaiting_content"> </div>

I need a workarround that will output unique <div> names for each block, I was wondering if using the node_id to do such a thing could accdomplish such a task.

Any hints on putting javascript toghether with ezP script to “create the div” and to “output to the div”

Thanks, jorge

 

Diseño Web Cuba
Web Design Cuba
www.elfosdesign.com

Jorge estévez

Monday 24 November 2008 9:22:02 am

Thanks Piotrek Karaś, and thanks to all!

This is how I manage to set it all:

First at the folder's view line I have this:

<div id="{$node.node_id}">Data is being loaded...</div>

every instance of the template has a unique DIV named node_id (unique it is!)

The code:

{*cache-block expiry=86412*}
{cache-block expiry=10}
{set-block variable=$shuffle_1}...operations1...{/set-block}
{set-block variable=$shuffle_2}...operations2...{/set-block}
{set-block variable=$shuffle_3}...operations3...{/set-block}
{set-block variable=$shuffle_4}...operations4...{/set-block}
{set-block variable=$shuffle_5}...operations5...{/set-block}

{literal}
<script type="text/javascript" language="javascript">
    //<![CDATA[
    // some JS random function to pick var $shuffle 1-5
    var i=0;    
    var dameunaleatoreo=aleatorio(0,4);
    
    //alert(dameunaleatoreo);    
    
    if(dameunaleatoreo==0)
        document.getElementById('{/literal}{/cache-block}{$node.node_id}{cache-block expiry=10}{literal}').innerHTML = '{/literal}{$shuffle_1}{literal}'; 
    if(dameunaleatoreo==1)
        document.getElementById('{/literal}{/cache-block}{$node.node_id}{cache-block expiry=10}{literal}').innerHTML = '{/literal}{$shuffle_2}{literal}'; 
    if(dameunaleatoreo==2)
        document.getElementById('{/literal}{/cache-block}{$node.node_id}{cache-block expiry=10}{literal}').innerHTML = '{/literal}{$shuffle_3}{literal}'; 
    if(dameunaleatoreo==3)
        document.getElementById('{/literal}{/cache-block}{$node.node_id}{cache-block expiry=10}{literal}').innerHTML = '{/literal}{$shuffle_4}{literal}'; 
    if(dameunaleatoreo==4)
        document.getElementById('{/literal}{/cache-block}{$node.node_id}{cache-block expiry=10}{literal}').innerHTML = '{/literal}{$shuffle_5}{literal}'; 
    

    //]]>
</script>
{/literal}
{/cache-block}

Notice the {cache-block} inside the javascript code and also the {literal} stuff.

And of course I had to build up a javascript function for a random number

<script type="text/javascript" language="javascript"> 
function aleatorio(inferior,superior){ 
    numPosibilidades = superior - inferior 
    aleat = Math.random() * numPosibilidades 
    aleat = Math.round(aleat) 
    return parseInt(inferior) + aleat 
    } 
</script>    

Now I have to tidy up to make an efficient code all over the place.

Thanks

By the way my pages are a little big and still they keep showing in less than 5 seconds, I think this is great!

Diseño Web Cuba
Web Design Cuba
www.elfosdesign.com