From 4e9720ea1623f655316dd8f7d767fc551630535d Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sun, 20 Jan 2013 12:59:40 -0500 Subject: [PATCH] CachedLDAPConnection: Lazy LDAP connections This is faster, and it allows us to return cached results even when the LDAP server is not available (e.g. if your network is down). --- mutt-ldap.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/mutt-ldap.py b/mutt-ldap.py index 5894f29..57ede51 100755 --- a/mutt-ldap.py +++ b/mutt-ldap.py @@ -167,12 +167,14 @@ class CachedLDAPConnection (LDAPConnection): _cache_version = '{0}.0'.format(__version__) def connect(self): + # delay LDAP connection until we actually need it self._load_cache() - super(CachedLDAPConnection, self).connect() def unbind(self): - super(CachedLDAPConnection, self).unbind() - self._save_cache() + if self.connection: + super(CachedLDAPConnection, self).unbind() + if self._cache: + self._save_cache() def search(self, query): cache_hit, entries = self._cache_lookup(query=query) @@ -182,6 +184,8 @@ class CachedLDAPConnection (LDAPConnection): for entry in entries: yield entry else: + if self.connection is None: + super(CachedLDAPConnection, self).connect() entries = [] keys = self.config.get('cache', 'fields').split() for entry in super(CachedLDAPConnection, self).search(query=query): -- 2.26.2