cleanup caching

This commit is contained in:
Edwin Eefting 2021-02-06 13:37:54 +01:00
parent 35584149ff
commit ce05e1ba4c

View File

@ -243,10 +243,18 @@ class CachedProperty(object):
return obj._cached_properties[propname] return obj._cached_properties[propname]
@staticmethod
def clear(obj):
"""clears cache of obj"""
if hasattr(obj, '_cached_properties'):
obj._cached_properties = {}
def invalidate_cache(obj): @staticmethod
if hasattr(obj, '_cached_properties'): def is_cached(obj, propname):
obj._cached_properties = {} if hasattr(obj, '_cached_properties') and propname in obj._cached_properties:
return True
else:
return False
class Logger: class Logger:
@ -539,6 +547,8 @@ class ZfsDataset:
""" """
self.zfs_node = zfs_node self.zfs_node = zfs_node
self.name = name # full name self.name = name # full name
self._virtual_snapshots = []
self.invalidate()
self.force_exists = force_exists self.force_exists = force_exists
def __repr__(self): def __repr__(self):
@ -563,9 +573,10 @@ class ZfsDataset:
self.zfs_node.debug("{}: {}".format(self.name, txt)) self.zfs_node.debug("{}: {}".format(self.name, txt))
def invalidate(self): def invalidate(self):
"""clear cache""" """clear caches"""
invalidate_cache(self) CachedProperty.clear(self)
self.force_exists = None self.force_exists = None
self._virtual_snapshots = []
def split_path(self): def split_path(self):
"""return the path elements as an array""" """return the path elements as an array"""
@ -773,12 +784,20 @@ class ZfsDataset:
return ret return ret
def add_virtual_snapshot(self, snapshot): def add_virtual_snapshot(self, snapshot):
"""add to self.snapshots as soon as it is created""" """pretend a snapshot exists (usefull in test mode)"""
pass
# NOTE: we could just call self.snapshots.append() but this would trigger a zfs list which is not always needed.
if CachedProperty.is_cached(self, 'snapshots'):
# already cached so add it
self.snapshots.append(snapshot)
else:
# self.snapshots will add it when requested
self._virtual_snapshots.append(snapshot)
@CachedProperty @CachedProperty
def snapshots(self): def snapshots(self):
"""get all snapshots of this dataset""" """get all snapshots of this dataset"""
self.debug("Getting snapshots") self.debug("Getting snapshots")
if not self.exists: if not self.exists:
@ -788,8 +807,10 @@ class ZfsDataset:
"zfs", "list", "-d", "1", "-r", "-t", "snapshot", "-H", "-o", "name", self.name "zfs", "list", "-d", "1", "-r", "-t", "snapshot", "-H", "-o", "name", self.name
] ]
names = self.zfs_node.run(cmd=cmd, readonly=True) return (
return self.from_names(names) self.from_names(self.zfs_node.run(cmd=cmd, readonly=True)) +
self._virtual_snapshots
)
@property @property
def our_snapshots(self): def our_snapshots(self):
@ -916,7 +937,7 @@ class ZfsDataset:
# progress output # progress output
if show_progress: if show_progress:
cmd.append("-v") # cmd.append("-v")
cmd.append("-P") cmd.append("-P")
# resume a previous send? (don't need more parameters in that case) # resume a previous send? (don't need more parameters in that case)