Forums / Developer / Display articles randomly and periodically

Display articles randomly and periodically

Author Message

Fátima Chapri

Saturday 26 September 2009 3:22:35 am

Hi,

I need to display an article on my frontpage that:
1) Is fetched randomly from a certain part of the node tree
2) Changes everyday automatically

I implemented the following solution:

{set-block scope=root variable=cache_ttl}0{/set-block}  <!-- This is to disable the view caching  -->
{def $articles_list=fetch( 'content', 'tree', hash( 'parent_node_id', 234,
                                               'sort_by', $node.sort_array,
                                               'class_filter_type', 'exclude',
                                               'class_filter_array', 'folder'))
                         $rand=rand(0,count($articles_list)|sub(1))}
{cache-block expiry=86400}
	<h2>{attribute_view_gui attribute=$articles_list[$rand].object.data_map.title}<h2>
        <div class="attribute-short">{attribute_view_gui attribute=$articles_list[$rand].object.data_map.intro}</div>                        
{/cache-block}
{$undef}

My questions are:
a) Setting the cache_ttl variable to 0 will decrease the performance of the site (as it is disabled for the frontpage file). So, is there any other way to disable the view caching for only a certain part of the code?
b) To have my random article changed everyday, I used the {cache-block expiry=86400} (as the content will change every 24 hours). But with this solution, whenever I make any alteration on the code or update the content, and I clear the cache, the article will also change. Have you guys got an other idea to help me solve this?

Thanks in advance for any answer.

Fabien Mas

Tuesday 29 September 2009 6:35:09 am

Hi
I don't have the answers to your questions but I think you should develop your own extended attribute filter. Here, you are fetching quite all your objects and it could be very low to do that

fabien

Fátima Chapri

Saturday 03 October 2009 3:32:47 am

Hi Fabien,

Thanks for your answer. I'm trying to develop my own attribute filter as you suggested, but still in a nightmare because I don't know very well how to that and can't find appropriate documentation for that. Can you please give me some ideas or links with documentation.

Piotrek Karaś

Sunday 04 October 2009 11:29:45 am

I don't think it has anything to do with attribute filter, which is all about <b>what to fetch</b> rather than <b>how often to refresh.</b> You're correctly after cache-block, please read:
http://ez.no/doc/ez_publish/technical_manual/4_x/reference/template_functions/miscellaneous/cache_block
Basically, if you only want the block content to be refreshed once a day, then you want to set ingore_content_expiry directive. And if want the same thing but cleared when a specific subtree is modified, then you go for:

{def $data_source_node_id=1234...}
{cache-block expiry=86412 ignore_content_expiry subtree_expiry=$data_source_node_id}
...

--
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

Fátima Chapri

Wednesday 07 October 2009 4:45:37 am

Piotrek, I made some tests using the ignore_content_expiry parameter, but it doesn't solve my problem because every time I clear the cache my content changes. I think I'll have to create a javascript or php solution.

Gaetano Giunta

Wednesday 07 October 2009 10:35:04 am

a - instead of setting cache-ttl to 0 then adding a cache-block, just set cache-ttl to 86400
b - why are you clearing your caches so often? you should not be...
c - in your code the fecth should be inside the cache-block anyway, otherwise you will be executing useless sql queries all the time

Principal Consultant International Business
Member of the Community Project Board

Piotrek Karaś

Wednesday 07 October 2009 11:05:57 am

in your code the fecth should be inside the cache-block anyway, otherwise you will be executing useless sql queries all the time

Wow, I actually missed that and that's obviously completely against caching idea ;)

Piotrek, I made some tests using the ignore_content_expiry parameter, but it doesn't solve my problem because every time I clear the cache my content changes. I think I'll have to create a javascript or php solution. 

I don't quite understand what's the problem with it...
But before you dive into producing some custom solutions, really study all built-in solutions (cache blocks, view cache, smart view cache clearing, persistent variables, cache block nesting) as they satisfy majority of possible scenarios and with little expertise can lead to great optimization. Of course, dynamic injection of HTML based on JavaScript is a great way, but only if all other things are well optimized.

--
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

Fátima Chapri

Saturday 10 October 2009 10:47:40 am

b - why are you clearing your caches so often? you should not be...

I really didn't think that way. You're right Gaetano. I'm actually clearing the cache all the time because i'm still building the site. But once it's over, it won't be necessary to to clear the caches all the time, right?
In fact, all my problem was around clearing the cache all the time.

Thanks a lot for the hint.