reduced memory usage in cache by removing scope "*", removed update_cache() function

This commit is contained in:
Roland Gruber 2005-10-15 09:33:05 +00:00
parent fcfbdec26a
commit d9fc93c39c
7 changed files with 37 additions and 124 deletions

View File

@ -8,6 +8,7 @@
-> Samba 3: fixed logon hours (patch 1311915)
-> Samba 3: loading of domain setting from profile did not work
-> Quota: profile settings fixed
-> reduced memory usage
28.09.2005 0.5.0

View File

@ -41,7 +41,7 @@ class cache {
function cache() {
$this->time = 0;
$this->attributes = array();
}
}
var $ldapcache; // This variable contains the cache
var $attributes; // This variable contains a list and their scope of attributes which should be cached
@ -51,8 +51,6 @@ class cache {
* syntax of $attributes is array( scope1 => array ( attributes ), scope2 => array ( attributes ), ...)
*/
function add_cache($attributes) {
// Check input variable
$allowed_types = array ( 'user', 'group', 'host', 'domain', '*' );
if (!is_array($attributes)) trigger_error('Argument of add_cache must be : array ( scope => array(attribute1(string), attribute2(string), ..), scope => ... ).', E_USER_ERROR);
foreach ($attributes as $attribute) {
if (!is_array($attribute)) trigger_error('Argument of add_cache must be : array ( scope => array(attribute1(string), attribute2(string), ..), scope => ... ).', E_USER_ERROR);
@ -61,10 +59,6 @@ class cache {
}
}
$scopes = array_keys($attributes);
foreach ($scopes as $scope) {
if (!@in_array($scope, $allowed_types)) trigger_error(sprintf('Invalid scope. Valid scopes are %s.', implode(" ", $allowed_types)), E_USER_ERROR);
}
// Everything seems to be OK, start processing data
foreach ($scopes as $scope) {
for ($i=0; $i<count($attributes[$scope]); $i++ ) {
if (!@in_array($attributes[$scope][$i] ,$this->attributes[$scope])) $this->attributes[$scope][] = $attributes[$scope][$i];
@ -72,27 +66,23 @@ class cache {
}
// Rebuild cache
$this->refresh_cache(true);
}
}
/**
* Queries the cache for a list of LDAP entries and their attributes.
*
* @param mixed $attributes One (string) or many (array) attribute names.
* @param string $objectClass The resulting entries need to contain this object class.
* @param string $singlescope The account type or "*" if all.
* @param mixed $scope the account type(s) as string or array
* @return array The found LDAP entries.
* <br>Format: array(dn1 => array(uidnumber1), dn2 => array(uidnumber2), ... ) if $attributes is of type string
* <br>or array(dn1 => array(uid => array(myuid), uidNumber => array(1234)), ... ) if $attributes is an array
*
*/
function get_cache($attributes, $objectClass, $singlescope) {
function get_cache($attributes, $objectClass, $scopelist) {
$this->refresh_cache();
// Check input variables
$allowed_types = array ( 'user', 'group', 'host', 'domain', '*' );
if (!in_array($singlescope, $allowed_types)) trigger_error(sprintf('Invalid scope. Valid scopes are %s.', implode(" ", $allowed_types)), E_USER_ERROR);
$this->refresh_cache();
if ($singlescope == '*') $scopes = $allowed_types;
else $scopes = array ( $singlescope );
if (is_array($scopelist)) $scopes = $scopelist;
else $scopes = array($scopelist);
// Add cache entry dynamic
if (!is_array($attributes)) $attributes = array($attributes);
foreach ($scopes as $scope) {
@ -126,16 +116,12 @@ class cache {
/* This functions returns the dn if a dn with $attribute=$value is found
* $values is the value $attribute is set to
* $scope is the scope where to search
* $scopelist mixed the account type(s) as string or array
*/
function in_cache($value, $attribute, $singlescope) {
function in_cache($value, $attribute, $scopelist) {
$this->refresh_cache();
// Check input variables
$allowed_types = array ( 'user', 'group', 'host', 'domain', '*' );
if (!in_array($singlescope, $allowed_types)) trigger_error(sprintf('Invalid scope. Valid scopes are %s.', implode(" ", $allowed_types)), E_USER_ERROR);
$this->refresh_cache();
if ($singlescope == '*') $scopes = $allowed_types;
else $scopes = array ( $singlescope );
if (is_array($scopelist)) $scopes = $scopelist;
else $scopes = array($scopelist);
// Add cache entry dynamic
foreach ($scopes as $scope) {
if (!@in_array($attribute ,$this->attributes[$scope])) $add[$scope][] = $attribute;
@ -168,10 +154,8 @@ class cache {
unset ($this->ldapcache);
$scopes = array_keys($this->attributes);
foreach ($scopes as $scope) {
// Get Scope
If ($scope != '*')
$suffix = $_SESSION['config']->get_Suffix($scope);
else $suffix = '';
// Get suffix
$suffix = $_SESSION['config']->get_Suffix($scope);
// Get Data from ldap
$search = $this->attributes[$scope];
$search[] = 'objectClass';
@ -196,67 +180,11 @@ class cache {
unset ($addcache['objectClass']);
if (count($addcache)!=0) $this->ldapcache[$scope][$dn] = $attr;
$entry = ldap_next_entry($_SESSION['ldap']->server(), $entry);
}
}
}
$this->time = time();
}
}
/* This function update the cache when changes were
* made without refrehing the complete cache
*/
function update_cache($dn, $mode, $attributes=false) {
$allowed_modes = array ( 'add', 'remove', 'modify', 'delete_dn' );
$allowed_types = array ( 'user', 'group', 'host', '*' );
for ($i=0; $i<count($allowed_types); $i++) {
if ($allowed_types[$i]!='*') {
If ($mode != '*')
$suffix = $_SESSION['config']->get_Suffix($allowed_types[$i]);
else $suffix = '';
if (substr($suffix, $dn)) $singlescope = $allowed_types[$i];
}
}
if (!in_array($singlescope, $allowed_types)) trigger_error(sprintf('Invalid scope. Valid scopes are %s.', implode(" ", $allowed_types)), E_USER_ERROR);
if (!in_array($mode, $allowed_modes)) trigger_error(sprintf('Invalid mode. Valid modes are %s.', implode(" ", $allowed_modes)), E_USER_ERROR);
// Everything seems to be OK, start processing data
// Get Scope
foreach ($allowed_types as $scope) {
if ($scope!='*') {
$suffix = $_SESSION['config']->get_Suffix($scope);
if (strpos($dn, $suffix)) $singlescope = $scope;
}
}
if (!isset($singlescope)) trigger_error(sprintf('Invalid dn: %s. DN not covered by any suffix.', $dn), E_USER_WARN);
// Refresh Cache
$this->refresh_cache();
if (is_array($attributes))
switch ($mode) {
case 'add':
$list = array_keys($attributes);
for ($i=0; $i<count($list); $i++)
foreach ($attributes[$list[$i]] as $attribute)
$this->ldapcache[$singlescope][$dn][$list[$i]][] = $attributes[$list[$i]];
break;
case 'remove':
$list = array_keys($attributes);
for ($i=0; $i<count($list); $i++)
foreach ($attributes[$list[$i]] as $attribute)
if (isset($this->ldapcache[$singlescope][$dn][$list[$i]][$attributes[$list[$i]]]))
unset($this->ldapcache[$singlescope][$dn][$list[$i]][$attributes[$list[$i]]]);
break;
case 'modify':
$list = array_keys($attributes);
for ($i=0; $i<count($list); $i++) {
if (isset($this->ldapcache[$singlescope][$dn][$list[$i]])) unset($this->ldapcache[$singlescope][$dn][$list[$i]]);
foreach ($attributes[$list[$i]] as $attribute)
$this->ldapcache[$singlescope][$dn][$list[$i]][] = $attributes[$list[$i]];
}
}
else {
if ($mode=='delete_dn')
if (isset($this->ldapcache[$singlescope][$dn])) unset($this->ldapcache[$singlescope][$dn]);
}
}
}
/**

View File

@ -1480,7 +1480,6 @@ class accountContainer {
if ($singleerror[0] == 'ERROR') $stopprocessing = true;
}
}
// fixme *** ad update_cache after every ldap-change
if (!$stopprocessing) {
if ($this->dn != $this->dn_orig) {
// move existing DN
@ -1503,20 +1502,17 @@ class accountContainer {
}
$success = ldap_add($_SESSION['ldap']->server(), $this->dn, $attr);
if ($success) {
//$_SESSION['cache']->update_cache($this->dn, 'add', $attr); TODO: reactivate
$success = ldap_delete($_SESSION['ldap']->server(), $this->dn_orig);
if (!$success) {
$errors[] = array('ERROR', sprintf(_('Was unable to delete DN: %s.'), $this->dn_orig), ldap_error($_SESSION['ldap']->server()));
$stopprocessing = true;
}
//if ($success) TODO: reactivate
//$_SESSION['cache']->update_cache($this->dn, 'delete_dn'); TODO: reactivate
}
}
if (!$success) {
$errors[] = array('ERROR', sprintf(_('Was unable to create DN: %s.'), $this->dn), ldap_error($_SESSION['ldap']->server()));
$stopprocessing = true;
}
}
}
// create complete new dn
else {
$attr = array();
@ -1527,13 +1523,11 @@ class accountContainer {
if (!$success) {
$errors[] = array('ERROR', sprintf(_('Was unable to create DN: %s.'), $this->dn), ldap_error($_SESSION['ldap']->server()));
$stopprocessing = true;
}
//else TODO: reactivate
//$_SESSION['cache']->update_cache($this->dn, 'add', $attr); TODO: reactivate
}
unset($attributes[$this->dn]);
}
unset($attributes[$this->dn]);
}
}
$DNs = array_keys($attributes);
for ($i=0; $i<count($DNs); $i++) {
if (!$stopprocessing) {
@ -1543,38 +1537,32 @@ class accountContainer {
if (!$success) {
$errors[] = array('ERROR', sprintf(_('Was unable to modify attribtues from DN: %s.'), $DNs[$i]), ldap_error($_SESSION['ldap']->server()));
$stopprocessing = true;
}
//else TODO: reactivate
//$_SESSION['cache']->update_cache($this->dn, 'modify', $attributes[$this->dn]['modify']); TODO: reactivate
}
}
// add attributes
if (isset($attributes[$DNs[$i]]['add']) && !$stopprocessing) {
$success = @ldap_mod_add($_SESSION['ldap']->server(), $DNs[$i], $attributes[$DNs[$i]]['add']);
if (!$success) {
$errors[] = array('ERROR', sprintf(_('Was unable to add attribtues to DN: %s.'), $DNs[$i]), ldap_error($_SESSION['ldap']->server()));
$stopprocessing = true;
}
//else TODO: reactivate
//$_SESSION['cache']->update_cache($this->dn, 'add', $attributes[$this->dn]['add']); TODO: reactivate
}
}
// removce attributes
if (isset($attributes[$DNs[$i]]['remove']) && !$stopprocessing) {
$success = @ldap_mod_del($_SESSION['ldap']->server(), $DNs[$i], $attributes[$DNs[$i]]['remove']);
if (!$success) {
$errors[] = array('ERROR', sprintf(_('Was unable to remove attribtues from DN: %s.'), $DNs[$i]), ldap_error($_SESSION['ldap']->server()));
$stopprocessing = true;
}
//else TODO: reactivate
//$_SESSION['cache']->update_cache($this->dn, 'remove', $attributes[$this->dn]['remove']); TODO: reactivate
}
}
}
}
if (!$stopprocessing) {
foreach ($attributes as $DN) {
if (is_array($DN['lamdaemon']['command'])) $result = lamdaemon($DN['lamdaemon']['command']);
// Error somewhere in lamdaemon
if (is_array($result))
if (is_array($result)) {
foreach ($result as $singleresult) {
if (is_array($singleresult)) {
if ($singleresult[0] == 'ERROR') $stopprocessing = true;
@ -1582,11 +1570,12 @@ class accountContainer {
$temparray[1] = _($singleresult[1]);
$temparray[2] = _($singleresult[2]);
$errors[] = $temparray;
}
}
}
}
}
$_SESSION['cache']->refresh_cache(true); // TODO: remove when update_cache is fixed
}
$_SESSION['cache']->refresh_cache(true);
if (count($errors)!=0) return $errors;
return 0;
}

View File

@ -890,7 +890,7 @@ class inetOrgPerson extends baseModule {
}
// uid
// get list of existing users
$dnUsers = $_SESSION['cache']->get_cache('uid', 'inetOrgPerson', '*');
$dnUsers = $_SESSION['cache']->get_cache('uid', 'inetOrgPerson', 'user');
$existingUsers = array();
foreach ($dnUsers as $dn) {
$existingUsers[] = $dn[0];

View File

@ -645,7 +645,7 @@ class posixAccount extends baseModule {
$minID = intval($this->moduleSettings['posixAccount_minMachine'][0]);
$maxID = intval($this->moduleSettings['posixAccount_maxMachine'][0]);
}
$dn_uids = $_SESSION['cache']->get_cache('uidNumber', 'posixAccount', '*');
$dn_uids = $_SESSION['cache']->get_cache('uidNumber', 'posixAccount', array('user', 'host'));
// get_cache will return an array ( dn1 => array(uidnumber1), dn2 => array(uidnumber2), ... )
if(is_array($dn_uids)) {
foreach ($dn_uids as $uid) $uids[] = $uid[0];
@ -745,11 +745,11 @@ class posixAccount extends baseModule {
// Create automatic useraccount with number if original user already exists
// Reset name to original name if new name is in use
// Set username back to original name if new username is in use
if ($_SESSION['cache']->in_cache($this->attributes['uid'][0],'uid', '*') && ($this->orig['uid'][0]!=''))
if ($_SESSION['cache']->in_cache($this->attributes['uid'][0],'uid', array('user', 'host')) && ($this->orig['uid'][0]!=''))
$this->attributes['uid'][0] = $this->orig['uid'][0];
// Change uid to a new uid until a free uid is found
else
while ($_SESSION['cache']->in_cache($this->attributes['uid'][0], 'uid', '*')) {
while ($_SESSION['cache']->in_cache($this->attributes['uid'][0], 'uid', array('user', 'host'))) {
if ($_SESSION[$this->base]->type=='host') $this->attributes['uid'][0] = substr($this->attributes['uid'][0], 0, -1);
// get last character of username
$lastchar = substr($this->attributes['uid'][0], strlen($this->attributes['uid'][0])-1, 1);
@ -1203,7 +1203,7 @@ class posixAccount extends baseModule {
$triggered_messages = array();
$needAutoUID = array();
// get list of existing users
$dnUsers = $_SESSION['cache']->get_cache('uid', 'posixAccount', '*');
$dnUsers = $_SESSION['cache']->get_cache('uid', 'posixAccount', array('user', 'host'));
$existingUsers = array();
foreach ($dnUsers as $dn) {
$existingUsers[] = $dn[0];
@ -1534,7 +1534,7 @@ class posixAccount extends baseModule {
$minID = intval($this->moduleSettings['posixAccount_minMachine'][0]);
$maxID = intval($this->moduleSettings['posixAccount_maxMachine'][0]);
}
$dn_uids = $_SESSION['cache']->get_cache('uidNumber', 'posixAccount', '*');
$dn_uids = $_SESSION['cache']->get_cache('uidNumber', 'posixAccount', array('user', 'host'));
// get_cache will return an array ( dn1 => array(uidnumber1), dn2 => array(uidnumber2), ... )
$uids = array();
if(is_array($dn_uids)) {

View File

@ -795,7 +795,7 @@ class posixGroup extends baseModule {
if (strpos($_SESSION['ldap']->objectClasses[$i], "NAME 'posixAccount'")) $line = $i;
}
if ($line!=-1) {
$result = $_SESSION['cache']->get_cache('gidNumber', 'posixAccount', '*');
$result = $_SESSION['cache']->get_cache('gidNumber', 'posixAccount', array('user', 'host'));
if (is_array($result)) {
$DNs = array_keys($result);
for ($i=0; $i<count($DNs); $i++)
@ -808,7 +808,7 @@ class posixGroup extends baseModule {
if (strpos($_SESSION['ldap']->objectClasses[$i], "NAME 'sambaAccount'")) $line = $i;
}
if ($line!=-1) {
$result = $_SESSION['cache']->get_cache('primaryGroupID', 'sambaAccount', '*');
$result = $_SESSION['cache']->get_cache('primaryGroupID', 'sambaAccount', array('user', 'host'));
if (is_array($result)) {
$DNs = array_keys($result);
for ($i=0; $i<count($DNs); $i++) {
@ -822,7 +822,7 @@ class posixGroup extends baseModule {
if (strpos($_SESSION['ldap']->objectClasses[$i], "NAME 'sambaSamAccount'")) $line = $i;
}
if ($line!=-1) {
$result = $_SESSION['cache']->get_cache('sambaPrimaryGroupSID', 'sambaSamAccount', '*');
$result = $_SESSION['cache']->get_cache('sambaPrimaryGroupSID', 'sambaSamAccount', array('user', 'host'));
if (is_array($result)) {
$DNs = array_keys($result);
for ($i=0; $i<count($DNs); $i++) {
@ -853,7 +853,7 @@ class posixGroup extends baseModule {
$ret = array();
$minID = intval($this->moduleSettings['posixGroup_minGID'][0]);
$maxID = intval($this->moduleSettings['posixGroup_maxGID'][0]);
$dn_gids = $_SESSION['cache']->get_cache('gidNumber', 'posixGroup', '*');
$dn_gids = $_SESSION['cache']->get_cache('gidNumber', 'posixGroup', 'group');
// get_cache will return an array ( dn1 => array(gidnumber1), dn2 => array(gidnumber2), ... )
$gids = array();
if(is_array($dn_gids)) {

View File

@ -175,7 +175,6 @@ if ($_POST['delete']) {
$errors[] = array ('ERROR', sprintf(_('Was unable to modify attribtues from DN: %s.'), $DNs[$i]), ldap_error($_SESSION['ldap']->server()));
$stopprocessing = true;
}
//else $_SESSION['cache']->update_cache($DNs[$i], 'modify', $attributes[$DNs[$i]]['modify']); TODO: reactivate
}
// add attributes
if (isset($attributes[$DNs[$i]]['add']) && !$stopprocessing) {
@ -184,7 +183,6 @@ if ($_POST['delete']) {
$errors[] = array ('ERROR', sprintf(_('Was unable to add attribtues to DN: %s.'), $DNs[$i]), ldap_error($_SESSION['ldap']->server()));
$stopprocessing = true;
}
//else $_SESSION['cache']->update_cache($DNs[$i], 'add', $attributes[$DNs[$i]]['add']); TODO: reactivate
}
// removce attributes
if (isset($attributes[$DNs[$i]]['remove']) && !$stopprocessing) {
@ -193,7 +191,6 @@ if ($_POST['delete']) {
$errors[] = array ('ERROR', sprintf(_('Was unable to remove attribtues from DN: %s.'), $DNs[$i]), ldap_error($_SESSION['ldap']->server()));
$stopprocessing = true;
}
//else $_SESSION['cache']->update_cache($DNs[$i], 'remove', $attributes[$DNs[$i]]['remove']); TODO: reactivate
}
}
}
@ -221,8 +218,6 @@ if ($_POST['delete']) {
$errors[] = array ('ERROR', sprintf(_('Was unable to delete DN: %s.'), $_SESSION['delete_dn'][$m]), ldap_error($_SESSION['ldap']->server()));
$stopprocessing = true;
}
//else TODO: reactivate
//$_SESSION['cache']->update_cache($_SESSION['delete_dn'][$m], 'delete_dn'); TODO: reactivate
}
if (!$stopprocessing) {
echo sprintf(_('Deleted DN: %s'), $_SESSION['delete_dn'][$m]) . "<br>\n";
@ -235,7 +230,7 @@ if ($_POST['delete']) {
echo "<br>\n";
}
}
$_SESSION['cache']->refresh_cache(true); // TODO: remove when update_cache is fixed
$_SESSION['cache']->refresh_cache(true);
echo "<br>\n";
echo "<br><input name=\"cancel\" type=\"submit\" value=\"" . _('Back to list') . "\">\n";
echo "</fieldset>\n";