OSDN Git Service

add command result caching
authort.moriyama <t.moriyama@users.sourceforge.jp>
Sun, 16 Jun 2013 12:10:37 +0000 (21:10 +0900)
committert.moriyama <t.moriyama@users.sourceforge.jp>
Sun, 16 Jun 2013 12:10:37 +0000 (21:10 +0900)
linkpair/collect/agent/commandrunner.py

index d50bdd1..069eade 100755 (executable)
@@ -32,19 +32,25 @@ class CommandRunner(object):
         self._remote_sshkey = ""
         self._ssh_keyauth = False
         self._ssh_passauth = False
+        self._command_result_cache = {}
 
         ''' set parameters '''
         [self._ssh_username, self._ssh_hostname,
             self._ssh_hostport] = self._u.parse_remote_desc(remote_desc)
 
-    def exec_cmd(self, cmdline):
+    def exec_cmd(self, cmdline, enable_cache=True):
+        if enable_cache:
+            if self._command_result_cache.has_key(cmdline):
+                return self._command_result_cache[cmdline]
+
         args = shlex.split(cmdline)
         if self._remote_desc is not None:
             result = self._exec_cmd_on_ssh(cmdline)
         else:
             result = Popen(args, stdout=PIPE, stderr=PIPE).stdout.readlines()
+        self._command_result_cache[cmdline] = result
         return result
-
+        
     def _exec_cmd_on_ssh(self, cmdline):
         sshc = ssh.SSHClient()
         sshc.load_system_host_keys()