MDL-40924 badges: Add caching to external badges

This commit is contained in:
Yuliya Bozhko
2013-09-02 13:45:13 +08:00
committed by Dan Poltawski
parent 71b06edf6b
commit 129e7dffeb
8 changed files with 75 additions and 12 deletions
+14 -1
View File
@@ -1167,13 +1167,25 @@ function badges_bake($hash, $badgeid, $userid = 0, $pathhash = false) {
/**
* Returns external backpack settings and badges from this backpack.
*
* This function first checks if badges for the user are cached and
* tries to retrieve them from the cache. Otherwise, badges are obtained
* through curl request to the backpack.
*
* @param int $userid Backpack user ID.
* @param boolean $refresh Refresh badges collection in cache.
* @return null|object Returns null is there is no backpack or object with backpack settings.
*/
function get_backpack_settings($userid) {
function get_backpack_settings($userid, $refresh = false) {
global $DB;
require_once(dirname(dirname(__FILE__)) . '/badges/lib/backpacklib.php');
// Try to get badges from cache first.
$badgescache = cache::make('core', 'externalbadges');
$out = $badgescache->get($userid);
if ($out !== false && !$refresh) {
return $out;
}
// Get badges through curl request to the backpack.
$record = $DB->get_record('badge_backpack', array('userid' => $userid));
if ($record) {
$backpack = new OpenBadgesBackpackHandler($record);
@@ -1198,6 +1210,7 @@ function get_backpack_settings($userid) {
$out->totalcollections = 0;
}
$badgescache->set($userid, $out);
return $out;
}