/** * Returns preferences ids for correspodnding preferences names. * This method is handy when many requests to different accounts for * the same preferences are made. Next time the same preferences for * the same account are checked the cached preferences ids may be used. * * This is useful esepcially in MPE which checks various preferences * for many accounts. * * @param array $aPrefNames Array containing preference names * @param string $accountType Account type * @return array Array which contains preferences ids (preference_name => preference_id) */ function getCachedPreferencesIds($aPrefNames, $accountType) { // keep preferences ids in static memory cache static $aPrefIdsCache; $aPrefFound = array(); $aPrefNotFound = array(); foreach ($aPrefNames as $prefName) { if (isset($aPrefIdsCache[$accountType][$prefName])) { $aPrefFound[$prefName] = $aPrefIdsCache[$accountType][$prefName]; } else { $aPrefNotFound[$prefName] = $prefName; } } if (!empty($aPrefNotFound)) { // read in any leftover preferences ids, needs to be done in the same method due // to static variables limitations $aPrefs = OA_Preferences::getPreferenceIds($aPrefNotFound, $accountType); if (is_array($aPrefs)) { foreach ($aPrefs as $prefName => $prefId) { $aPrefIdsCache[$accountType][$prefName] = $prefId; $aPrefFound[$prefName] = $prefId; } } } return $aPrefFound; }
function testCacheGetPreferenceIds() { $expectedPrefs = array('pref1' => 1, 'pref2' => 2); $this->_createPreferences($expectedPrefs); $prefs = OA_Preferences::getPreferenceIds(array(''), 'ADMIN'); $this->assertEqual($prefs, array()); $prefs = OA_Preferences::getCachedPreferencesIds(array(''), 'ADMIN'); $this->assertEqual($prefs, array()); $expectedPref1 = array('pref1' => 1); $prefs = OA_Preferences::getPreferenceIds(array('pref1'), 'ADMIN'); $this->assertEqual($prefs, $expectedPref1); $prefs = OA_Preferences::getCachedPreferencesIds(array('pref1'), 'ADMIN'); $this->assertEqual($prefs, $expectedPref1); $prefs = OA_Preferences::getPreferenceIds(array('pref1', 'pref2'), 'ADMIN'); $this->assertEqual($prefs, $expectedPrefs); $prefs = OA_Preferences::getCachedPreferencesIds(array('pref1', 'pref2'), 'ADMIN'); $this->assertEqual($prefs, $expectedPrefs); // change one of the cached preferences and check that getCachedPreferencesIds() still returns // old values $doPreferences = OA_Dal::factoryDO('preferences'); $doPreferences->preference_id = 1; $doPreferences->preference_name = 'test'; $doPreferences->update(); $prefs = OA_Preferences::getCachedPreferencesIds(array('pref1', 'pref2'), 'ADMIN'); $this->assertEqual($prefs, $expectedPrefs); $prefs = OA_Preferences::getCachedPreferencesIds(array('test'), 'ADMIN'); $this->assertEqual($prefs, array('test' => 1)); }