diff --git a/dfvfs/resolver/cache.py b/dfvfs/resolver/cache.py index 33602489..759b0d95 100644 --- a/dfvfs/resolver/cache.py +++ b/dfvfs/resolver/cache.py @@ -3,6 +3,9 @@ from __future__ import unicode_literals +import collections +import itertools + from dfvfs.lib import errors @@ -62,7 +65,7 @@ def __init__(self, maximum_number_of_cached_values): super(ObjectsCache, self).__init__() self._maximum_number_of_cached_values = maximum_number_of_cached_values - self._values = {} + self._values = collections.OrderedDict() def CacheObject(self, identifier, vfs_object): """Caches a VFS object. @@ -129,6 +132,22 @@ def GetCacheValueByObject(self, vfs_object): return None, None + def GetLastObject(self): + """Retrieves the last cached object. + + This method ignores the cache value reference count. + + Returns: + object: the last cached VFS object or None if the cache is empty. + """ + if not self._values: + return None + + # Get the last (or most recent added) cache value. + cache_value = next(itertools.islice( + self._values.values(), len(self._values) - 1, None)) + return cache_value.vfs_object + def GetObject(self, identifier): """Retrieves a cached object based on the identifier. diff --git a/dfvfs/resolver/context.py b/dfvfs/resolver/context.py index 762eee91..ffaaabd5 100644 --- a/dfvfs/resolver/context.py +++ b/dfvfs/resolver/context.py @@ -63,6 +63,11 @@ def CacheFileSystem(self, path_spec, file_system): def Empty(self): """Empties the caches.""" + file_object = self._file_object_cache.GetLastObject() + while file_object: + file_object.close() + file_object = self._file_object_cache.GetLastObject() + self._file_object_cache.Empty() self._file_system_cache.Empty() diff --git a/tests/resolver/cache.py b/tests/resolver/cache.py index 41243b0d..c8c354a6 100644 --- a/tests/resolver/cache.py +++ b/tests/resolver/cache.py @@ -110,6 +110,8 @@ def testEmpty(self): cache_object.Empty() self.assertEqual(len(cache_object._values), 0) + # TODO: add tests for the GetLastObject method + def testGetObject(self): """Tests the GetObject method.""" cache_object = cache.ObjectsCache(1)