Commit 2158579f authored by Tim Peters's avatar Tim Peters

Merge rev 37372 from 3.4 branch.

ClientCache:  made it a new-style class.

FileCache:  Removed the reuse= argument.  IIRC, it made some sense
when different cache schemes were first being tried, but complicated
the logic now to no good end.

Nuked the maddening log warnings about reuse=True.  They were never
helpful and were often confusing.

Added info-level log messages to record the path of the client cache
file, whether it's persistent or temporary, and if it's persistent
whether we're creating or reusing it.

Minor changes to comments.
parent bf86c970
...@@ -64,7 +64,7 @@ logger = logging.getLogger("ZEO.cache") ...@@ -64,7 +64,7 @@ logger = logging.getLogger("ZEO.cache")
# full verification # full verification
# <p> # <p>
class ClientCache: class ClientCache(object):
"""A simple in-memory cache.""" """A simple in-memory cache."""
## ##
...@@ -689,17 +689,15 @@ def sync(f): ...@@ -689,17 +689,15 @@ def sync(f):
class FileCache(object): class FileCache(object):
def __init__(self, maxsize, fpath, parent, reuse=True): def __init__(self, maxsize, fpath, parent):
# - `maxsize`: total size of the cache file, in bytes; this is # - `maxsize`: total size of the cache file, in bytes; this is
# ignored if reuse is true and fpath names an existing file; # ignored path names an existing file; perhaps we should attempt
# perhaps we should attempt to change the cache size in that # to change the cache size in that case
# case # - `fpath`: filepath for the cache file, or None (in which case
# - `fpath`: filepath for the cache file, or None; see `reuse` # a temp file will be created)
# - `parent`: the ClientCache this FileCache is part of # - `parent`: the ClientCache instance; its `_evicted()` method
# - `reuse`: If true, and fpath is not None, and fpath names a # is called whenever we need to evict an object to make room in
# file that exists, that pre-existing file is used (persistent # the file
# cache). In all other cases a new file is created: a temp
# file if fpath is None, else with path fpath.
self.maxsize = maxsize self.maxsize = maxsize
self.parent = parent self.parent = parent
...@@ -743,17 +741,17 @@ class FileCache(object): ...@@ -743,17 +741,17 @@ class FileCache(object):
# (and it sets self.f). # (and it sets self.f).
self.fpath = fpath self.fpath = fpath
if reuse and fpath and os.path.exists(fpath): if fpath and os.path.exists(fpath):
# Reuse an existing file. scan() will open & read it. # Reuse an existing file. scan() will open & read it.
self.f = None self.f = None
logger.info("reusing persistent cache file %r", fpath)
else: else:
if reuse:
logger.warning("reuse=True but the given file path %r "
"doesn't exist; ignoring reuse=True", fpath)
if fpath: if fpath:
self.f = open(fpath, 'wb+') self.f = open(fpath, 'wb+')
logger.info("created persistent cache file %r", fpath)
else: else:
self.f = tempfile.TemporaryFile() self.f = tempfile.TemporaryFile()
logger.info("created temporary cache file %r", self.f.name)
# Make sure the OS really saves enough bytes for the file. # Make sure the OS really saves enough bytes for the file.
self.f.seek(self.maxsize - 1) self.f.seek(self.maxsize - 1)
self.f.write('x') self.f.write('x')
...@@ -779,11 +777,11 @@ class FileCache(object): ...@@ -779,11 +777,11 @@ class FileCache(object):
# for each object found in the cache. This method should only # for each object found in the cache. This method should only
# be called once to initialize the cache from disk. # be called once to initialize the cache from disk.
def scan(self, install): def scan(self, install):
if self.f is not None: if self.f is not None: # we're not (re)using a pre-existing file
return return
fsize = os.path.getsize(self.fpath) fsize = os.path.getsize(self.fpath)
if fsize != self.maxsize: if fsize != self.maxsize:
logger.warning("existing cache file %s has size %d; " logger.warning("existing cache file %r has size %d; "
"requested size %d ignored", self.fpath, "requested size %d ignored", self.fpath,
fsize, self.maxsize) fsize, self.maxsize)
self.maxsize = fsize self.maxsize = fsize
...@@ -797,8 +795,8 @@ class FileCache(object): ...@@ -797,8 +795,8 @@ class FileCache(object):
# Populate .filemap and .key2entry to reflect what's currently in the # Populate .filemap and .key2entry to reflect what's currently in the
# file, and tell our parent about it too (via the `install` callback). # file, and tell our parent about it too (via the `install` callback).
# Remember the location of the largest free block That seems a decent # Remember the location of the largest free block. That seems a
# place to start currentofs. # decent place to start currentofs.
max_free_size = max_free_offset = 0 max_free_size = max_free_offset = 0
ofs = ZEC3_HEADER_SIZE ofs = ZEC3_HEADER_SIZE
while ofs < fsize: while ofs < fsize:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment