Changeset 1010:2640715ee9aa


Ignore:
Timestamp:
Jan 2, 2011, 6:05:59 PM (10 years ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Message:
Add a `clear` method to `LRUCache` and `StatCache`.

The former way of clearing the cache was to temporarily set its size
to zero (without changing the maximum size, i. e. the `size` attribute
of `LRUCache` objects). The new way is cleaner; the old one was a
workaround which was only tolerated because no one would ever set the
maximum size to zero. ;-) I only do it to implicitly remove all nodes
from the cache in order to make garbage collection more robust.
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • ftp_stat_cache.py

    r1006 r1010  
    8585    def clear(self):
    8686        """Clear (invalidate) all cache entries."""
    87         old_size = self._cache.size
    88         try:
    89             # Implicitly clear the cache by setting the size to zero
    90             self.resize(0)
    91         finally:
    92             self.resize(old_size)
     87        self._cache.clear()
    9388
    9489    def invalidate(self, path):
  • lrucache.py

    r1009 r1010  
    4545# the suffix after the hyphen denotes modifications by the
    4646#  ftputil project with respect to the original version
    47 __version__ = "0.2-4"
     47__version__ = "0.2-5"
    4848__all__ = ['CacheKeyError', 'LRUCache', 'DEFAULT_SIZE']
    4949__docformat__ = 'reStructuredText en'
     
    126126
    127127    def __init__(self, size=DEFAULT_SIZE):
    128         # Check arguments
    129         if not isinstance(size, (int, long)):
    130             raise TypeError("cache size (%r) must be an integer" % size)
    131         if size <= 0:
    132             raise ValueError("cache size (%d) must be positive" % size)
    133128        object.__init__(self)
    134         self.__heap = []
    135         self.__dict = {}
     129        self.clear()
    136130        """Maximum size of the cache.
    137131        If more than 'size' elements are added to the cache,
    138132        the least-recently-used ones will be discarded."""
     133        # Implicitly check size value.
    139134        self.size = size
    140135        self.__counter = 0
     136
     137    def clear(self):
     138        """Clear the cache, removing all elements.
     139       
     140        The `size` attribute of the cache isn't modified.
     141        """
     142        self.__heap = []
     143        self.__dict = {}
    141144
    142145    def _sort_key(self):
     
    212215        # automagically shrink heap on resize
    213216        if name == 'size':
    214             if value < 0:
    215                 raise ValueError("cache size (%d) mustn't be negative" % size)
     217            size = value
     218            if not isinstance(size, (int, long)):
     219                raise TypeError("cache size (%r) must be an integer" % size)
     220            if size <= 0:
     221                raise ValueError("cache size (%d) must be positive" % size)
    216222            while len(self.__heap) > value:
    217223                lru = heappop(self.__heap)
  • test/test_ftp_stat_cache.py

    r1006 r1010  
    9191    def test_cache_size_zero(self):
    9292        host = test_base.ftp_host_factory()
    93         host.stat_cache.resize(0)
     93        self.assertRaises(ValueError, host.stat_cache.resize, 0)
    9494        # If bug #38 is present, this raises an `IndexError`
    9595        items = host.listdir(host.curdir)
Note: See TracChangeset for help on using the changeset viewer.