Advertisement:

Author Topic: Caching by load  (Read 1416 times)

Offline nend

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 1,755
  • 2 deep n2 the code
    • sicommnend on GitHub
    • SIComm.us
Caching by load
« on: April 08, 2016, 10:31:04 PM »
I was wondering if it would be smart to up the cache by load. Say for instance my load time is over X amount of time or my load average is greater than X.

I know certain pages and processes have different loads throughout the forum system. But I am wondering here what I could do in a drastic situation, where it is either keeping the site up with a little outdated information or going down.

Will I started a test on one of my sites to see how this would work.

If anyone wants to play along I will go ahead and share my code edits. I am not going to explain them, if your going to implement them you should be able to understand them.

In Load.php
Find
Code: [Select]
// Do a few things to protect against missing settings or settings with invalid values...
if (empty($modSettings['defaultMaxTopics']) || $modSettings['defaultMaxTopics'] <= 0 || $modSettings['defaultMaxTopics'] > 999)
$modSettings['defaultMaxTopics'] = 20;
if (empty($modSettings['defaultMaxMessages']) || $modSettings['defaultMaxMessages'] <= 0 || $modSettings['defaultMaxMessages'] > 999)
$modSettings['defaultMaxMessages'] = 15;
if (empty($modSettings['defaultMaxMembers']) || $modSettings['defaultMaxMembers'] <= 0 || $modSettings['defaultMaxMembers'] > 999)
$modSettings['defaultMaxMembers'] = 30;

Add after
Code: [Select]
$load = sys_getloadavg();
$modSettings['load_average'] = $load[0];

Find
Code: [Select]
// UTF-8 in regular expressions is unsupported on PHP(win) versions < 4.2.3.
$utf8 = (empty($modSettings['global_character_set']) ? $txt['lang_character_set'] : $modSettings['global_character_set']) === 'UTF-8' && (strpos(strtolower(PHP_OS), 'win') === false || @version_compare(PHP_VERSION, '4.2.3') != -1);

Add before
Code: [Select]
if (!empty($modSettings['load_average'])) {
$context['cache_load_time'] = cache_get_data('load_time', 90);
if($modSettings['load_average'] > 10 && $modSettings['cache_enable'] < 2 || !empty($context['cache_load_time']) && $context['cache_load_time'] >= 0.5 && $context['cache_load_time'] < 1)
$modSettings['cache_enable'] = 2;
if($modSettings['load_average'] > 20 && $modSettings['cache_enable'] < 3 || !empty($context['cache_load_time']) && $context['cache_load_time'] >= 1)
$modSettings['cache_enable'] = 3;
}

In Subs.php
Find
Code: [Select]
// Show the load time?  (only makes sense for the footer.)
$context['show_load_time'] = !empty($modSettings['timeLoadPageEnable']);
$context['load_time'] = round(array_sum(explode(' ', microtime())) - array_sum(explode(' ', $time_start)), 3);
$context['load_queries'] = $db_count;

Add After
Code: [Select]
if (!isset($context['cache_load_time']))
cache_put_data('load_time', $context['load_time'], 90);

Tweak the variables as you see fit.  ;)

Also you need to set cache to Level 1 in the Server Settings > Caching

Please note this value will change in this setting as it is reading the variable set by modSettings which is edited right away by this little hack.

Offline nend

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 1,755
  • 2 deep n2 the code
    • sicommnend on GitHub
    • SIComm.us
Re: Caching by load
« Reply #1 on: April 10, 2016, 12:01:41 AM »
I can't use $context in reloadSettings, it is reset after $modSettings is loaded.

Had to change instances
Code: [Select]
$context['cache_load_time']
To
Code: [Select]
$modSettings['cache_load_time']
Code change if you been following allong.
Load.php
Code: [Select]
$load = sys_getloadavg();
$modSettings['load_average'] = $load[1];

Code: [Select]
if (!empty($modSettings['load_average'])) {
$modSettings['cache_load_time'] = cache_get_data('load_time', 90);
if($modSettings['load_average'] > 6 && $modSettings['cache_enable'] < 2 || !empty($modSettings['cache_load_time']) && $modSettings['cache_load_time'] >= 0.5 && $modSettings['cache_load_time'] < 1)
$modSettings['cache_enable'] = 2;
if($modSettings['load_average'] > 8 && $modSettings['cache_enable'] < 3 || !empty($modSettings['cache_load_time']) && $modSettings['cache_load_time'] >= 1)
$modSettings['cache_enable'] = 3;
}

Subs.php
Code: [Select]
if (empty($modSettings['cache_load_time']) || $context['load_time'] > $modSettings['cache_load_time']) {
$modSettings['cache_load_time'] = $context['load_time'];
cache_put_data('load_time', $context['load_time'], 90);
}