def intersect_keys(keys, reffile, cache=False, clean_accs=False):
    index = None
    if cache:
        refcache = reffile + '.sqlite'
        if os.path.exists(refcache):
            if os.stat(refcache).st_mtime < os.stat(reffile).st_mtime:
                logging.warn("Outdated cache; rebuilding index")
            else:
                try:
                    index = (SeqIO.index_db(refcache,
                                            key_function=clean_accession)
                             if clean_accs
                             else SeqIO.index_db(refcache))
                except Exception:
                    logging.warn("Skipping corrupted cache; rebuilding index")
                    index = None
    else:
        refcache = ':memory:'
    if index is None:
        index = (SeqIO.index_db(refcache, [reffile], 'fasta',
                                key_function=clean_accession)
                 if clean_accs
                 else SeqIO.index_db(refcache, [reffile], 'fasta'))
    if clean_accs:
        keys = (clean_accession(k) for k in keys)
    for key in keys:
        try:
            record = index[key]
        except LookupError:
            logging.info("No match: %s", repr(key))
            continue
        yield record