From 0f63a3f6a7398321bd799c7b778e0e721c811287 Mon Sep 17 00:00:00 2001 From: Konstantin Ryabitsev Date: Fri, 20 Mar 2020 16:35:08 -0400 Subject: Add caching layer Many lore.kernel.org operations can be repeated within quick succession of each-other (e.g. someone reruns a query with -t). This commit adds a caching layer that keeps lookups in local cache for 10 minutes (default). It can be made longer or shorter by editing the 'cache-expire' setting, or running "b4 am" with -C,--no-cache. Signed-off-by: Konstantin Ryabitsev --- b4/mbox.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'b4/mbox.py') diff --git a/b4/mbox.py b/b4/mbox.py index 3c06bd2..ccd15ed 100644 --- a/b4/mbox.py +++ b/b4/mbox.py @@ -13,6 +13,7 @@ import email.message import email.utils import re import time +import shutil import urllib.parse import xml.etree.ElementTree @@ -53,6 +54,20 @@ def get_pi_thread_by_url(t_mbx_url, savefile): def get_pi_thread_by_msgid(msgid, config, cmdargs): wantname = cmdargs.wantname outdir = cmdargs.outdir + if wantname: + savefile = os.path.join(outdir, wantname) + else: + # Save it into msgid.mbox + savefile = '%s.t.mbx' % msgid + savefile = os.path.join(outdir, savefile) + + cachedir = b4.get_cache_dir() + cachefile = os.path.join(cachedir, '%s.pi.mbx' % urllib.parse.quote_plus(msgid)) + if os.path.exists(cachefile) and not cmdargs.nocache: + logger.debug('Using cached copy: %s', cachefile) + shutil.copyfile(cachefile, savefile) + return savefile + # Grab the head from lore, to see where we are redirected midmask = config['midmask'] % msgid logger.info('Looking up %s', midmask) @@ -64,12 +79,6 @@ def get_pi_thread_by_msgid(msgid, config, cmdargs): canonical = resp.headers['Location'].rstrip('/') resp.close() t_mbx_url = '%s/t.mbox.gz' % canonical - if wantname: - savefile = os.path.join(outdir, wantname) - else: - # Save it into msgid.mbox - savefile = '%s.t.mbx' % msgid - savefile = os.path.join(outdir, savefile) loc = urllib.parse.urlparse(t_mbx_url) if cmdargs.useproject: @@ -87,6 +96,7 @@ def get_pi_thread_by_msgid(msgid, config, cmdargs): in_mbx.close() out_mbx.close() os.unlink(in_mbxf) + shutil.copyfile(savefile, cachefile) return savefile -- cgit v1.2.3