4images Forum & Community

General / Allgemeines => Programming => Topic started by: budduke on July 10, 2010, 12:23:39 AM

Title: Trying to understand the Caching system...
Post by: budduke on July 10, 2010, 12:23:39 AM
I did not see where would be the best place to ask this but can someone explain the cache system on 4images a little better for me?
Here is what I have ran into using one of my own mods...
User category area
http://www.4homepages.de/forum/index.php?topic=24628.msg134888#msg134888 (http://www.4homepages.de/forum/index.php?topic=24628.msg134888#msg134888)

when I have the cache turned on, the users trying to use this mod are getting strange things. like stuff not showing up, photos not being uploaded, ect...
when the cache finally caught up then the user had 5 of the same image uploaded because they kept trying over and over.
I have determined it is because it was pulling the cache pages instead of there newly uploaded pages so I turned it back off till I can figure it out better.

Is there a way to program the cache system so that when a user does something like the user upload/multi-upload that it would send a command that would re-cache the page that the user just changed? It even happens when they create a new category, it would need to do the same thing for the parent of that category to re-cache it so it would be there.

I noticed this at the top of most php pages in 4images...
Code: [Select]
define('GET_CACHES', 1);
define('ROOT_PATH', './');
define('GET_USER_ONLINE', 1);

if I change the GET_CACHES to 0 will that force that page to re-cache?

I saw I can disable caching for categories in the global file but that did not seem to help because my mod is using user_categories.php instead of just categories.php.
I love the speed of the caching system, I am just trying to figure out a way of making more useful for my site/users...
any help/documentation/advice would greatly be appreciated...
Title: Re: Trying to understand the Caching system...
Post by: V@no on July 10, 2010, 01:01:57 AM
The caching system is actually pretty simple once you understand the basics.

When a visitor opens a page for the first time, 4images creates a unique string (cache_id) based on some specific parameters. After it compiled the page, it saves it into ./cache/catche_id files. If any of the parameters changed or cache files expired when the page refreshed, then a new page generated, otherwise it shows page from cache instead.
The cache_id generated via create_cache_id() function and you can find it some of the main .php files, such as details.php and categories.php
function create_cache_id() accepts an array of the parameters, so adding more parameters will change when it's going to show cached pages or generate new, the parameter order is irrelevant.

Knowing the basics and knowing when we it shouldn't show cached pages (in your case when new images added or new subcategory added), we can use the following parameters:
$subcat_ids = array();
get_subcat_ids($cat_id, $cat_id, $cat_parent_cache);
$cache_id = create_cache_id(
  'page.categories',
  array(
    $user_info[$user_table_fields['user_id']],
    $cat_id,
    $page,
    $perpage,
    isset($user_info['lightbox_image_ids']) ? substr(md5($user_info['lightbox_image_ids']), 0, 8) : 0,
    $config['template_dir'],
    $config['language_dir'],
    $cat_cache[$cat_id]['num_images'], //if number of images changed
    @count(@$subcat_ids[$cat_id]), //if number of subcategories changed
  )
);



P.S.
The GET_CACHES constant has nothing to do with the caching system, it's for categories caches only, it's required pretty much throughout every page, because it's generates $cat_cache array.
Title: Re: Trying to understand the Caching system...
Post by: budduke on September 06, 2010, 07:02:02 PM
Oops,  :oops:
I forgot to reply to your message V@no,
Thank you for the help, that was what I needed.
Everything is now working correctly with the caching system now that I know how it works.

thanks again!