Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MarkupSectionCache for ItemManager 2.2+
#1
ItemManager's - MarkupSectionCache is a simple method that enables you to cache any individual parts in a theme.

It's usable for example, if you have a huge number of Items that has 300+ rows in a dropdown field. Loading a list of items with dropdown fields, each of them with over 300 rows and creating the select options on every pageview isn't really efficient, but what is the solution if you didn't want to cache the whole theme because it needed to support dynamic parameters? The solution is to cache just the code that generated the item part in theme. Here's a code example:
PHP Code:
$manager = new IManager();
$cache $manager->getSectionCache();
$itemMapper $manager->getItemClass();
// Initialize all the items of category with ID 19 (Cities)
$itemMapper->init(26);
// get the current page slug for a clear cache file name
$slug get_page_slug(false);

if(!
$output $cache->get($slug.'.cities'))
{
    
// 'cities' not found from cache
    
$output '<div id="items">';
    foreach (
$itemMapper->items as $item)
    {
        
$output .= '<div class="row">';
        
$output .= '    <p><span>City: </span>'.$item->name.'</p>';
        
$output .= '    <p><span>Museums & Galleries: </span>';

        
// get selected value
        
$selected $item->fields->museums->value;

        
$output .= '    <select name="museums">';
        foreach(
$item->fields->museums->options as $option)
        {
            
$output .= '        <option value="'.$option.'"'.(($option == $selected) ? ' selected="selected"' '').'>'.$option.'</option>';
        }
        
$output .= '</select></p>';
        
$output .= '</div>';
    }
    
$output .= '</div>';
    
// save $output to cache as
    // (Section cache remembers the name we gave with our previous $cache->get() call)
    
$cache->save($output);
}

// output markup it doesn't matter whether or not it originally came from cache
echo $output


By default cache is stored for an hour. To specify shorter/longer cache time, you'll need to pass that as second parameter to get:
PHP Code:
// Cache theme section for 10 minutes
if (!$data $cache->get('$slug.'.cities''600)) { 
... 
Reply




Users browsing this thread: 1 Guest(s)
Free HDTV Channels Residential Solar Panels