~sschwarzer/ftputil

d1ac5ecc064cbd076f896cf0d5e7aedecc4bc816 — Stefan Schwarzer 2 years ago f257822
Clear cache if time shift changes

If the time shift changes, all timestamps in the cache will be wrong
with respect to the _new_ time shift. Depending on the cache, reusing
the old timestamps may not be a problem, but in some cases it might
be. Therefore stay on the safe side and clear the cache, sacrificing
efficiency in favor of correct timestamps.

At first sight, we could "correct" the cache entries by applying the
difference between old and new time shift to the existing cache
entries. However, I think the interactions in ftputil are already so
complicated that I'd like to avoid any probably unnecessary
optimizations. Also, the time shift would probably be set soon after
creating the `FTPHost` instance, so anyway only relatively few cache
entries would be affected.
1 files changed, 8 insertions(+), 4 deletions(-)

M ftputil/host.py
M ftputil/host.py => ftputil/host.py +8 -4
@@ 89,7 89,7 @@ class FTPHost:
        self.curdir, self.pardir, self.sep = ".", "..", "/"
        # Set default time shift (used in `upload_if_newer` and
        # `download_if_newer`).
        self.set_time_shift(0.0)
        self._time_shift = 0.0
        # Don't use `LIST -a` option by default. If the server doesn't
        # understand the `-a` option and interprets it as a path, the
        # results can be surprising. See ticket #110.


@@ 320,9 320,13 @@ class FTPHost:

        The time shift is measured in seconds.
        """
        # Implicitly set via `set_time_shift` call in constructor
        # pylint: disable=attribute-defined-outside-init
        self._time_shift = time_shift
        old_time_shift = self.time_shift()
        if time_shift != old_time_shift:
            # If the time shift changed, all entries in the cache will have
            # wrong times with respect to the updated time shift, therefore
            # clear the cache.
            self.stat_cache.clear()
            self._time_shift = time_shift

    def time_shift(self):
        """