From 53631c2e4e1f7e93ce2da03185678577ab2c62ab Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sun, 20 Jan 2013 11:07:50 -0500 Subject: [PATCH] CachedLDAPConnection: Don't cache fields we don't use I had previously said: > It would be nice to automatically detect and drop any non text > fields, but that's probably not worth the trouble either... Since the configuration ID is in the cached key, we already know exactly which fields we need to save in the cache. Only caching those fields avoids binary fields and fields with large values (jpegPhoto is both). I made the cached fields configurable because the CachedLDAPConnection instance doesn't know about the fields needed by format_entry() and format_columns(). If you don't configure the cached fields, a minimal default is setup in the main block. --- mutt-ldap.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/mutt-ldap.py b/mutt-ldap.py index 7554b94..c2ec3b8 100755 --- a/mutt-ldap.py +++ b/mutt-ldap.py @@ -65,6 +65,7 @@ CONFIG.set('results', 'optional_column', '') # mutt can display one optional col CONFIG.add_section('cache') CONFIG.set('cache', 'enable', 'yes') # enable caching by default CONFIG.set('cache', 'path', '~/.mutt-ldap.cache') # cache results here +CONFIG.set('cache', 'fields', '') # fields to cache (if empty, setup in the main block) #CONFIG.set('cache', 'longevity_days', '14') # TODO: cache results for 14 days by default CONFIG.add_section('system') # HACK: Python 2.x support, see http://bugs.python.org/issue2128 @@ -168,8 +169,13 @@ class CachedLDAPConnection (LDAPConnection): yield entry else: entries = [] + keys = self.config.get('cache', 'fields').split() for entry in super(CachedLDAPConnection, self).search(query=query): - entries.append(entry) + cn,data = entry + # use dict comprehensions in Python >= 2.7, see PEP 274 + cached_data = dict( + [(key, data[key]) for key in keys if key in data]) + entries.append((cn, cached_data)) yield entry self._cache_store(query=query, entries=entries) @@ -236,6 +242,13 @@ if __name__ == '__main__': if CONFIG.getboolean('cache', 'enable'): connection_class = CachedLDAPConnection + if not CONFIG.get('cache', 'fields'): + # setup a reasonable default + fields = ['mail', 'cn', 'displayName'] # used by format_entry() + optional_column = CONFIG.get('results', 'optional_column') + if optional_column: + fields.append(optional_column) + CONFIG.set('cache', 'fields', ' '.join(fields)) else: connection_class = LDAPConnection -- 2.26.2