From: W. Trevor King Date: Sun, 20 Jan 2013 17:59:40 +0000 (-0500) Subject: CachedLDAPConnection: Lazy LDAP connections X-Git-Tag: v0.1~10 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=4e9720ea1623f655316dd8f7d767fc551630535d;p=mutt-ldap.git 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). --- 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):