Make BaseFileCache into an abstractbaseclass so it's a proper interface

Push the opening and closing of files into the _load and _dump methods
so that we don't invoke the slow codec machinery without reason.
This commit is contained in:
Toshio Kuratomi 2017-02-16 14:59:14 -08:00 committed by Brian Coca
parent c033e5111f
commit 45251f910c
5 changed files with 66 additions and 48 deletions

View file

@ -30,13 +30,13 @@ class CacheModule(BaseFileCacheModule):
"""
A caching module backed by pickle files.
"""
plugin_name = 'pickle'
read_mode = 'rb'
write_mode = 'wb'
encoding = None
def _load(self, f):
return pickle.load(f)
def _load(self, filepath):
# Pickle is a binary format
with open(filepath, 'rb') as f:
return pickle.load(f)
def _dump(self, value, f):
pickle.dump(value, f)
def _dump(self, value, filepath):
with open(filepath, 'wb') as f:
# Use pickle protocol 2 which is compatible with Python 2.3+.
pickle.dump(value, f, protocol=2)