Add support for ACPu in users online count

ofaj
Yannick Warnier 9 years ago
parent f89b87c4e1
commit bdb5a0e2c1
  1. 74
      documentation/optimization.html
  2. 39
      main/inc/lib/banner.lib.php

@ -171,9 +171,9 @@ An optional additional caching mechanism you may use is the realpath_cache_size
See <a href="http://php.net/manual/en/ini.core.php">the PHP documentation</a>
for more details.
<br />
<br />
<br />
<h3>APC</h3>
If you prefer using <a href="http://php.net/manual/en/book.apc.php">APC</a>,
If you prefer using <a href="http://php.net/manual/en/book.apc.php">APC</a>,
you can use the same kind of trick as above, just changing the code a little:
<pre>
$xc = function_exists('apc_exists');
@ -209,6 +209,76 @@ If you prefer using <a href="http://php.net/manual/en/book.apc.php">APC</a>,
}
...
</pre>
<br />
<br />
<h3>APCu</h3>
In PHP 5.5 and above, APC is rendered practically obsolete by the presence of ZendOPCache by default.
However, APC does not cover the "user cache", like the caching of specific variables in memory.
Considering this, you can <a href="http://php.net/manual/en/book.apcu.php">APCu</a> to add the same level of variable caching as described above, just changing the code a little:
<pre>
function return_notification_menu()
{
$_course = api_get_course_info();
$course_id = 0;
if (!empty($_course)) {
$course_id = $_course['code'];
}
$user_id = api_get_user_id();
$html = '';
$cacheEnabled = function_exists('apcu_exists');
if ((api_get_setting('showonline', 'world') == 'true' && !$user_id) ||
(api_get_setting('showonline', 'users') == 'true' && $user_id) ||
(api_get_setting('showonline', 'course') == 'true' && $user_id && $course_id)
) {
if ($cacheEnabled) {
$apc = apcu_cache_info(null,true);
$apc_end = $apc['start_time']+$apc['ttl'];
if (apcu_exists('my_campus_whoisonline_count_simple') AND (time() < $apc_end) AND apcu_fetch('my_campus_whoisonline_count_simple') > 0 ) {
$number = apcu_fetch('my_campus_whoisonline_count_simple');
} else {
$number = who_is_online_count(api_get_setting('time_limit_whosonline'));
apcu_clear_cache();
apcu_store('my_campus_whoisonline_count_simple',$number,15);
}
} else {
$number = who_is_online_count(api_get_setting('time_limit_whosonline'));
}
$number_online_in_course = 0;
if (!empty($_course['id'])) {
if ($cacheEnabled) {
$apc = apcu_cache_info(null,true);
$apc_end = $apc['start_time']+$apc['ttl'];
if (apcu_exists('my_campus_whoisonline_count_simple_'.$_course['id']) AND (time() < $apc_end) AND apcu_fetch('my_campus_whoisonline_count_simple_'.$_course['id']) > 0) {
$number_online_in_course = apcu_fetch('my_campus_whoisonline_count_simple_'.$_course['id']);
} else {
$number_online_in_course = who_is_online_in_this_course_count($user_id, api_get_setting('time_limit_whosonline'), $_course['id']);
apcu_store('my_campus_whoisonline_count_simple_'.$_course['id'],$number_online_in_course,15);
}
} else {
$number_online_in_course = who_is_online_in_this_course_count(
$user_id,
api_get_setting('time_limit_whosonline'),
$_course['id']
);
}
}
// Display the who's online of the platform
if ($number) {
if ((api_get_setting('showonline', 'world') == 'true' && !$user_id) ||
(api_get_setting('showonline', 'users') == 'true' && $user_id)
) {
$html .= '<li><a href="'.api_get_path(WEB_PATH).'whoisonline.php" target="_self" title="'.get_lang('UsersOnline').'" >'.
Display::return_icon('user.png', get_lang('UsersOnline'), array(), ICON_SIZE_TINY).' '.$number.'</a></li>';
}
}
...
</pre>
<br />
<h3>Memcached</h3>
If you use php5-memcached (different set of functions than php5-memcache!),

@ -239,20 +239,45 @@ function return_notification_menu()
$user_id = api_get_user_id();
$html = '';
$cacheEnabled = function_exists('apcu_exists');
if ((api_get_setting('showonline', 'world') == 'true' && !$user_id) ||
(api_get_setting('showonline', 'users') == 'true' && $user_id) ||
(api_get_setting('showonline', 'course') == 'true' && $user_id && $course_id)
) {
$number = who_is_online_count(api_get_setting('time_limit_whosonline'));
if ($cacheEnabled) {
$apc = apcu_cache_info(null,true);
$apc_end = $apc['start_time']+$apc['ttl'];
if (apcu_exists('my_campus_whoisonline_count_simple') AND (time() < $apc_end) AND apcu_fetch('my_campus_whoisonline_count_simple') > 0 ) {
$number = apcu_fetch('my_campus_whoisonline_count_simple');
} else {
$number = who_is_online_count(api_get_setting('time_limit_whosonline'));
apcu_clear_cache();
apcu_store('my_campus_whoisonline_count_simple',$number,15);
}
} else {
$number = who_is_online_count(api_get_setting('time_limit_whosonline'));
}
$number_online_in_course = 0;
if (!empty($_course['id'])) {
$number_online_in_course = who_is_online_in_this_course_count(
$user_id,
api_get_setting('time_limit_whosonline'),
$_course['id']
);
if ($cacheEnabled) {
$apc = apcu_cache_info(null,true);
$apc_end = $apc['start_time']+$apc['ttl'];
if (apcu_exists('my_campus_whoisonline_count_simple_'.$_course['id']) AND (time() < $apc_end) AND apcu_fetch('my_campus_whoisonline_count_simple_'.$_course['id']) > 0) {
$number_online_in_course = apcu_fetch('my_campus_whoisonline_count_simple_'.$_course['id']);
} else {
$number_online_in_course = who_is_online_in_this_course_count($user_id, api_get_setting('time_limit_whosonline'), $_course['id']);
apcu_store('my_campus_whoisonline_count_simple_'.$_course['id'],$number_online_in_course,15);
}
} else {
$number_online_in_course = who_is_online_in_this_course_count(
$user_id,
api_get_setting('time_limit_whosonline'),
$_course['id']
);
}
}
// Display the who's online of the platform

Loading…
Cancel
Save