using instance-wide cache for categories, so updates from other sessions are immediatly available and sessions get smaller

This commit is contained in:
Ralf Becker 2012-02-10 20:17:28 +00:00
parent 44ff7cdafa
commit 01ee489460
2 changed files with 42 additions and 46 deletions

View File

@ -83,6 +83,8 @@ class categories
* @var array cat_id => array of data * @var array cat_id => array of data
*/ */
private static $cache; private static $cache;
const CACHE_APP = 'phpgwapi';
const CACHE_NAME = 'cat_cache';
/** /**
* Appname for global categories * Appname for global categories
@ -393,6 +395,8 @@ class categories
*/ */
static function read($id) static function read($id)
{ {
if (is_null(self::$cache)) self::init_cache();
if (!isset(self::$cache[$id])) return false; if (!isset(self::$cache[$id])) return false;
$cat = self::$cache[$id]; $cat = self::$cache[$id];
@ -903,16 +907,13 @@ class categories
* Initialise or restore the categories cache * Initialise or restore the categories cache
* *
* We use the default ordering of return_array to avoid doing it again there * We use the default ordering of return_array to avoid doing it again there
*
* @param array &$cache=null cache content to restore it (from the egw-object)
*/ */
public static function &init_cache(&$cache=null) public static function init_cache()
{ {
if (!is_null($cache)) self::$cache = egw_cache::getInstance(self::CACHE_APP, self::CACHE_NAME);
if (is_null(self::$cache))
{ {
//error_log(__METHOD__."() ".count($cache)." cats restored: ".function_backtrace());
return self::$cache =& $cache;
}
// check if we are already updated to global owner == 0, if not do it now // check if we are already updated to global owner == 0, if not do it now
if (!$GLOBALS['egw']->db->select(self::TABLE,'COUNT(*)',array('cat_owner'=>'0'),__LINE__,__FILE__)->fetchColumn()) if (!$GLOBALS['egw']->db->select(self::TABLE,'COUNT(*)',array('cat_owner'=>'0'),__LINE__,__FILE__)->fetchColumn())
{ {
@ -947,8 +948,9 @@ class categories
} }
self::$cache[$cat['id']] = $cat; self::$cache[$cat['id']] = $cat;
} }
egw_cache::setInstance(self::CACHE_APP, self::CACHE_NAME, self::$cache);
}
//error_log(__METHOD__."() cache initialised: ".function_backtrace()); //error_log(__METHOD__."() cache initialised: ".function_backtrace());
return self::$cache;
} }
/** /**
@ -961,9 +963,7 @@ class categories
*/ */
public static function invalidate_cache($cat_id=null) public static function invalidate_cache($cat_id=null)
{ {
self::init_cache(); egw_cache::unsetInstance(self::CACHE_APP, self::CACHE_NAME);
// we need to invalidate the whole session cache, as it stores our cache
egw::invalidate_session_cache();
} }
/** /**
@ -981,6 +981,8 @@ class categories
*/ */
static function return_single($id) static function return_single($id)
{ {
if (is_null(self::$cache)) self::init_cache();
return isset(self::$cache[$id]) ? array(self::$cache[$id]) : false; return isset(self::$cache[$id]) ? array(self::$cache[$id]) : false;
} }

View File

@ -56,8 +56,6 @@ class egw extends egw_minimal
*/ */
var $hooks; var $hooks;
private $cat_cache;
/** /**
* Constructor: Instantiates the sub-classes * Constructor: Instantiates the sub-classes
* *
@ -182,8 +180,6 @@ class egw extends egw_minimal
$this->check_app_rights(); $this->check_app_rights();
$this->load_optional_classes(); $this->load_optional_classes();
$this->cat_cache =& categories::init_cache();
} }
else // set the defines for login, in case it's more then just login else // set the defines for login, in case it's more then just login
{ {
@ -215,8 +211,6 @@ class egw extends egw_minimal
register_shutdown_function(array($this, 'shutdown')); register_shutdown_function(array($this, 'shutdown'));
$this->define_egw_constants(); $this->define_egw_constants();
categories::init_cache($this->cat_cache);
} }
/** /**