From 384e8a441d4db350d9d28a6f92ccaaaa7bf052af Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Wed, 12 Jun 2024 10:27:37 +0200 Subject: [PATCH] ldap: add nocache parameter to LdapClient.get_objects method --- mylib/ldap.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/mylib/ldap.py b/mylib/ldap.py index 065c16c..e441732 100644 --- a/mylib/ldap.py +++ b/mylib/ldap.py @@ -575,6 +575,7 @@ class LdapClient: warn=True, paged_search=False, pagesize=None, + nocache=False, ): """ Retrieve objects from LDAP @@ -592,9 +593,11 @@ class LdapClient: (optional, default: False) :param pagesize: When using paged search, the page size (optional, default: see LdapServer.paged_search) + :param nocache: If True, disable using cache """ - if name in self._cached_objects: + if name in self._cached_objects and not nocache: log.debug("Retrieved %s objects from cache", name) + objects = self._cached_objects[name] else: assert self._conn or self.initialize() log.debug( @@ -624,13 +627,11 @@ class LdapClient: if not obj_dn or not isinstance(obj_attrs, dict): continue objects[obj_dn] = self._get_obj(obj_dn, obj_attrs) - self._cached_objects[name] = objects + if not nocache: + self._cached_objects[name] = objects if not key_attr or key_attr == "dn": - return self._cached_objects[name] - return { - self.get_attr(self._cached_objects[name][dn], key_attr): self._cached_objects[name][dn] - for dn in self._cached_objects[name] - } + return objects + return {self.get_attr(objects[dn], key_attr): objects[dn] for dn in objects} def get_object(self, type_name, object_name, filterstr, basedn, attrs, warn=True): """