シェルフモジュールを使用すると、驚くべき動作が得られます。keys()、iter()、およびiteritems()は、シェルフ内のすべてのエントリを返すわけではありません。コードは次のとおりです。
cache = shelve.open('my.cache')
# ...
cache[url] = (datetime.datetime.today(), value)
後で:
cache = shelve.open('my.cache')
urls = ['accounts_with_transactions.xml', 'targets.xml', 'profile.xml']
try:
print list(cache.keys()) # doesn't return all the keys!
print [url for url in urls if cache.has_key(url)]
print list(cache.keys())
finally:
cache.close()
出力は次のとおりです。
['targets.xml']
['accounts_with_transactions.xml', 'targets.xml']
['targets.xml', 'accounts_with_transactions.xml']
誰かが以前にこれに遭遇したことがありますか、そしてすべての可能なキャッシュキーを事前に知らなくても回避策はありますか?