Skip to content

Commit

Permalink
Added support for session based caching of parton information
Browse files Browse the repository at this point in the history
  • Loading branch information
cableman committed May 16, 2013
1 parent d8fd9f2 commit b9b5473
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
49 changes: 32 additions & 17 deletions alma.module
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,9 @@ function alma_profile2_presave($entity) {
),
));
}

// Clear local session cache.
alma_user_clear_cache($creds, TRUE);
}
}
}
Expand Down Expand Up @@ -505,27 +508,39 @@ function alma_get_patron($creds = NULL, $reset = FALSE, $as_array = FALSE) {
$creds = ding_user_get_creds($user);
}

static $patron;
// Try static cache.
$patron = &drupal_static(__FUNCTION__);
if (!$patron || $reset) {
$info = alma_client_invoke('get_patron_info', $creds['name'], $creds['pass'], TRUE);
$organisation = alma_get_organisation();
$patron = array(
'name' => $info['user_name'],
'email' => isset($info['mails'][0]) ? $info['mails'][0]['mail'] : '',
'address' => isset($info['addresses'][0]) ? $info['addresses'][0]['street'] : '',
'postal' => isset($info['addresses'][0]) ? $info['addresses'][0]['postal_code'] : '',
'city' => isset($info['addresses'][0]) ? $info['addresses'][0]['city'] : '',
'mobiles' => isset($info['phones']) ? $info['phones'] : '',
'branch' => $info['preferences']['patron_branch'],
'branchName' => $organisation['branch'][$info['preferences']['patron_branch']],
'absentPeriods' => isset($info['absent_periods']) ? $info['absent_periods'] : '',
);
// Try session cache.
if (!$reset && isset($_SESSION['alma_patron_info'])) {
$patron = $_SESSION['alma_patron_info'];
}
else {
$info = alma_client_invoke('get_patron_info', $creds['name'], $creds['pass'], TRUE);
$organisation = alma_get_organisation();
$patron = array(
'name' => $info['user_name'],
'email' => isset($info['mails'][0]) ? $info['mails'][0]['mail'] : '',
'address' => isset($info['addresses'][0]) ? $info['addresses'][0]['street'] : '',
'postal' => isset($info['addresses'][0]) ? $info['addresses'][0]['postal_code'] : '',
'city' => isset($info['addresses'][0]) ? $info['addresses'][0]['city'] : '',
'mobiles' => isset($info['phones']) ? $info['phones'] : '',
'branch' => $info['preferences']['patron_branch'],
'branchName' => $organisation['branch'][$info['preferences']['patron_branch']],
'absentPeriods' => isset($info['absent_periods']) ? $info['absent_periods'] : '',
);

// Store information in session cache.
$_SESSION['alma_patron_info'] = $patron;
}
}
if ($as_array) {
return $patron;

// Cast petron to object.
if (!$as_array) {
$patron = (object)$patron;
}

return (object)$patron;
return $patron;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions includes/alma.user.inc
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ function alma_user_profile_form_validate($form, $form_state) {
}

/**
* Clear user cache based on creds.
* Clear user session cache based on creds.
*
* @param array $creds
*/
function alma_user_clear_cache($creds = NULL) {
alma_get_patron($creds);
alma_get_patron($creds, TRUE);
}

/**
Expand Down

0 comments on commit b9b5473

Please sign in to comment.