using a static var as cache, which is a reference to the session:

a) automatic shared between all instances of accounts class
b) no need to have a shutdown function storing it to the session
--> cleaner code
This commit is contained in:
Ralf Becker 2009-12-07 17:56:31 +00:00
parent a3a3faab22
commit 6aa95316ba
2 changed files with 113 additions and 140 deletions

View File

@ -25,9 +25,8 @@
* API - accounts * API - accounts
* *
* This class uses a backend class (at them moment SQL or LDAP) and implements some * This class uses a backend class (at them moment SQL or LDAP) and implements some
* caching on to top of the backend functions. The cache is share for all instances of * caching on to top of the backend functions. The cache is static and therefore shared
* the accounts class and for LDAP it is persistent through the whole session, for SQL * between all instances of accounts class.
* it's only on a per request basis.
* *
* The backend only implements the read, save, delete, name2id and the {set_}members{hips} methods. * The backend only implements the read, save, delete, name2id and the {set_}members{hips} methods.
* The account class implements all other (eg. name2id, id2name) functions on top of these. * The account class implements all other (eg. name2id, id2name) functions on top of these.
@ -66,11 +65,18 @@ class accounts
), ),
); );
/** /**
* enables the session-cache, done in the constructor for the LDAP backend only * Enables the session-cache, currently switched on independent of the backend
* *
* @var boolean $use_session_cache * @var boolean $use_session_cache
*/ */
var $use_session_cache = true; static $use_session_cache = true;
/**
* Cache, stored in sesssion
*
* @var array
*/
static $cache;
/** /**
* Depricated: Account this class was instanciated for * Depricated: Account this class was instanciated for
@ -146,10 +152,10 @@ class accounts
*/ */
public static function getInstance() public static function getInstance()
{ {
if (self::$_instance === NULL) { if (self::$_instance === NULL)
{
self::$_instance = new accounts; self::$_instance = new accounts;
} }
return self::$_instance; return self::$_instance;
} }
@ -211,10 +217,12 @@ class accounts
* set the accountId * set the accountId
* *
* @param int $accountId * @param int $accountId
* @deprecated
*/ */
function setAccountId($accountId) function setAccountId($accountId)
{ {
if($accountId && is_numeric($accountId)) { if($accountId && is_numeric($accountId))
{
$this->account_id = (int)$accountId; $this->account_id = (int)$accountId;
} }
} }
@ -242,8 +250,8 @@ class accounts
function search($param) function search($param)
{ {
//echo "<p>accounts::search(".print_r($param,True).") start: ".microtime()."</p>\n"; //echo "<p>accounts::search(".print_r($param,True).") start: ".microtime()."</p>\n";
$this->setup_cache(); self::setup_cache();
$account_search = &$this->cache['account_search']; $account_search = &self::$cache['account_search'];
$serial = serialize($param); $serial = serialize($param);
@ -357,8 +365,8 @@ class accounts
} }
if (!$id) return false; if (!$id) return false;
$this->setup_cache(); self::setup_cache();
$account_data = &$this->cache['account_data']; $account_data = &self::$cache['account_data'];
if (!isset($account_data[$id])) if (!isset($account_data[$id]))
{ {
@ -423,12 +431,12 @@ class accounts
if ($data['account_primary_group'] && (!($memberships = $this->memberships($id,true)) || if ($data['account_primary_group'] && (!($memberships = $this->memberships($id,true)) ||
!in_array($data['account_primary_group'],$memberships))) !in_array($data['account_primary_group'],$memberships)))
{ {
$this->cache_invalidate($data['account_id']); self::cache_invalidate($data['account_id']);
$memberships[] = $data['account_primary_group']; $memberships[] = $data['account_primary_group'];
$this->set_memberships($memberships,$id); $this->set_memberships($memberships,$id);
} }
} }
$this->cache_invalidate($data['account_id']); self::cache_invalidate($data['account_id']);
return $id; return $id;
} }
@ -447,7 +455,7 @@ class accounts
} }
if (!$id) return false; if (!$id) return false;
$this->cache_invalidate($id); self::cache_invalidate($id);
$this->backend->delete($id); $this->backend->delete($id);
// delete all acl_entries belonging to that user or group // delete all acl_entries belonging to that user or group
@ -485,8 +493,8 @@ class accounts
*/ */
function name2id($name,$which='account_lid',$account_type=null) function name2id($name,$which='account_lid',$account_type=null)
{ {
$this->setup_cache(); self::setup_cache();
$name_list = &$this->cache['name_list']; $name_list = &self::$cache['name_list'];
if(@isset($name_list[$which][$name]) && $name_list[$which][$name]) if(@isset($name_list[$which][$name]) && $name_list[$which][$name])
{ {
@ -558,8 +566,8 @@ class accounts
*/ */
function memberships($account_id,$just_id=false) function memberships($account_id,$just_id=false)
{ {
$this->setup_cache(); self::setup_cache();
$memberships_list = &$this->cache['memberships_list']; $memberships_list = &self::$cache['memberships_list'];
if (!is_int($account_id) && !is_numeric($account_id)) if (!is_int($account_id) && !is_numeric($account_id))
{ {
@ -588,7 +596,7 @@ class accounts
} }
$this->backend->set_memberships($groups,$account_id); $this->backend->set_memberships($groups,$account_id);
$this->cache_invalidate($account_id); self::cache_invalidate($account_id);
} }
/** /**
@ -601,8 +609,8 @@ class accounts
*/ */
function members($account_id,$just_id=false) function members($account_id,$just_id=false)
{ {
$this->setup_cache(); self::setup_cache();
$members_list = &$this->cache['members_list']; $members_list = &self::$cache['members_list'];
if (!is_int($account_id) && !is_numeric($account_id)) if (!is_int($account_id) && !is_numeric($account_id))
{ {
@ -627,7 +635,7 @@ class accounts
//echo "<p>accounts::set_members(".print_r($members,true).",$gid)</p>\n"; //echo "<p>accounts::set_members(".print_r($members,true).",$gid)</p>\n";
$this->backend->set_members($members,$gid); $this->backend->set_members($members,$gid);
$this->cache_invalidate(0); self::cache_invalidate(0);
} }
/** /**
@ -643,8 +651,8 @@ class accounts
{ {
if (!is_array($app_users)) if (!is_array($app_users))
{ {
$this->setup_cache(); self::setup_cache();
$cache = &$this->cache['account_split'][$app_user]; $cache = &self::$cache['account_split'][$app_user];
if (is_array($cache)) if (is_array($cache))
{ {
@ -810,14 +818,10 @@ class accounts
* *
* @param int $account_id for which account_id should the cache be invalid, default 0 = all * @param int $account_id for which account_id should the cache be invalid, default 0 = all
*/ */
function cache_invalidate($account_id=0) static function cache_invalidate($account_id=0)
{ {
//echo "<p>accounts::cache_invalidate($account_id)</p>\n"; //echo "<p>accounts::cache_invalidate($account_id)</p>\n";
if (is_object($GLOBALS['egw']->accounts)) if (self::$cache) self::$cache = array();
{
$GLOBALS['egw']->accounts->cache = array();
}
if ($this->cache) $this->cache = array();
if (method_exists($GLOBALS['egw'],'invalidate_session_cache')) // egw object in setup is limited if (method_exists($GLOBALS['egw'],'invalidate_session_cache')) // egw object in setup is limited
{ {
@ -837,56 +841,29 @@ class accounts
* *
* @internal * @internal
*/ */
function setup_cache() static function setup_cache()
{ {
//echo "<p>accounts::setup_cache() use_session_cache=$this->use_session_cache, is_array(this->cache)=".(int)is_array($this->cache)."</p>\n"; //echo "<p>accounts::setup_cache() use_session_cache={self::$use_session_cache}, is_array(self::\$cache)=".(int)is_array(self::$cache)."</p>\n";
if (is_array($this->cache)) return; // cache is already setup if (is_array(self::$cache)) return; // cache is already setup
if ($this->use_session_cache && isset($GLOBALS['egw']->accounts) && !is_array($GLOBALS['egw']->accounts->cache) && is_object($GLOBALS['egw']->session)) if (self::$use_session_cache && is_object($GLOBALS['egw']->session))
{ {
//echo "<p>restoring the session-cache for \$GLOBALS['egw']->accounts</p>\n"; self::$cache =& egw_cache::getSession('accounts_cache','phpgwapi');
$GLOBALS['egw']->accounts->cache = $GLOBALS['egw']->session->appsession('accounts_cache','phpgwapi'); //echo "<p>restoring cache from session, ".count(call_user_func_array('array_merge',(array)self::$cache))." items</p>\n";
} }
if (!isset($this->cache) && isset($GLOBALS['egw']->accounts)) if (!is_array(self::$cache))
{
$this->cache =& $GLOBALS['egw']->accounts->cache;
}
if (!is_array($this->cache))
{ {
//echo "<p>initialising this->cache to array()</p>\n"; //echo "<p>initialising this->cache to array()</p>\n";
$this->cache = array(); self::$cache = array();
} }
} }
/** /**
* Magic function called if the object get's unserialized * @deprecated not used any more, as static cache is a reference to the session
*
* We unset our cache, which is a reference to the cache of the global account object in $GLOBALS['egw']->accounts,
* as it will be no reference after wakeup (causes cache_invalidate to no longer work).
* For the global accounts object the cache get unset too, as the object was stored in a early state of the framework
* initialisation, and it the cache stored in the session by save_session_cache is a lot more up to date.
*
* @internal
*/
function __wakeup()
{
unset($this->cache);
}
/**
* Saves the account-data cache in the session
*
* Gets called from common::egw_final()
*
* @internal
*/ */
function save_session_cache() function save_session_cache()
{ {
if (is_object($GLOBALS['egw']->session) && is_array($GLOBALS['egw']->accounts->cache))
{
//echo "<p>save_session_cache() saving</p>\n";
$GLOBALS['egw']->session->appsession('accounts_cache','phpgwapi',$GLOBALS['egw']->accounts->cache);
}
} }
/** /**

View File

@ -498,10 +498,6 @@ class egw extends egw_minimal
{ {
define('EGW_SHUTDOWN',True); define('EGW_SHUTDOWN',True);
if (isset($this->accounts))
{
$this->accounts->save_session_cache();
}
if (class_exists('egw_link',false)) // false = no autoload! if (class_exists('egw_link',false)) // false = no autoload!
{ {
egw_link::save_session_cache(); egw_link::save_session_cache();