From d386ba5d627c4a0c891582f2a3c8d2aa7fc960da Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Sun, 7 Jul 2013 16:38:11 -0400 Subject: [PATCH] VirtualConnection: Add option to cache fetch_* results We use this in the CLI tools since there's no point in repeatedly updating the VM list if the whole process only takes a few seconds. --- virtinst/cli.py | 1 + virtinst/connection.py | 25 +++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/virtinst/cli.py b/virtinst/cli.py index a3eeef072..caf743dd2 100644 --- a/virtinst/cli.py +++ b/virtinst/cli.py @@ -237,6 +237,7 @@ def getConnection(uri): logging.debug("Requesting libvirt URI %s", (uri or "default")) conn = virtinst.VirtualConnection(uri) conn.open(_do_creds_authname) + conn.cache_object_fetch = True logging.debug("Received libvirt URI %s", conn.uri) return conn diff --git a/virtinst/connection.py b/virtinst/connection.py index 449b1945a..05357fd5f 100644 --- a/virtinst/connection.py +++ b/virtinst/connection.py @@ -63,6 +63,12 @@ class VirtualConnection(object): self._conn_version = None self._support_cache = {} + self._fetch_cache = {} + + # Setting this means we only do fetch_all* once and just carry + # the result. For the virt-* CLI tools this ensures any revalidation + # isn't hammering the connection over and over + self.cache_object_fetch = False ############## @@ -97,6 +103,7 @@ class VirtualConnection(object): def close(self): self._libvirtconn = None self._uri = None + self._fetch_cache = {} def invalidate_caps(self): self._caps = None @@ -120,14 +127,28 @@ class VirtualConnection(object): self._libvirtconn = conn def fetch_all_guests(self): + key = "vms" + if key in self._fetch_cache: + return self._fetch_cache[key] + ignore, ignore, ret = pollhelpers.fetch_vms(self, {}, lambda obj, ignore: obj) - return ret.values() + ret = ret.values() + if self.cache_object_fetch: + self._fetch_cache[key] = ret + return ret def fetch_all_pools(self): + key = "pools" + if key in self._fetch_cache: + return self._fetch_cache[key] + ignore, ignore, ret = pollhelpers.fetch_pools(self, {}, lambda obj, ignore: obj) - return ret.values() + ret = ret.values() + if self.cache_object_fetch: + self._fetch_cache[key] = ret + return ret #########################