]> git.proxmox.com Git - pve-manager.git/commitdiff
cache apt query in /var/lib/pve-manager/pkgupdates
authorDietmar Maurer <dietmar@proxmox.com>
Wed, 12 Jun 2013 06:18:02 +0000 (08:18 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Thu, 13 Jun 2013 09:52:31 +0000 (11:52 +0200)
PVE/API2/APT.pm

index c8f7b3126373f14a24806a6c426bcac6e437ee95..51aa611bb74bb9c49f5cc0c77ea98c10f1c65542 100644 (file)
@@ -2,6 +2,7 @@ package PVE::API2::APT;
 
 use strict;
 use warnings;
+use File::stat ();
 
 use PVE::Tools qw(extract_param);
 use PVE::SafeSyslog;
@@ -10,6 +11,7 @@ use PVE::Exception qw(raise_param_exc);
 use PVE::RESTHandler;
 use PVE::RPCEnvironment;
 
+use JSON;
 use PVE::JSONSchema qw(get_standard_option);
 
 use AptPkg::Cache;
@@ -115,6 +117,28 @@ __PACKAGE__->register_method({
     code => sub {
        my ($param) = @_;
 
+       # we try to cache results
+       my $pve_pkgstatus_fn = "/var/lib/pve-manager/pkgupdates";
+
+       if (my $st1 = File::stat::stat($pve_pkgstatus_fn)) {
+           my $st2 = File::stat::stat("/var/cache/apt/pkgcache.bin");
+           my $st3 = File::stat::stat("/var/lib/dpkg/status");
+       
+           if ($st2->mtime < $st1->mtime && $st3->mtime < $st1->mtime) {
+               my $data;
+               eval {
+                   my $jsonstr = PVE::Tools::file_get_contents($pve_pkgstatus_fn, 5*1024*1024);
+                   $data = decode_json($jsonstr);
+               };
+               if (my $err = $@) {
+                   warn "error readin cached package status in $pve_pkgstatus_fn\n";
+                   # continue and overwrite cache with new content
+               } else {
+                   return $data;
+               }
+           }
+       }
+
        my $pkglist = [];
 
        my $cache = &$get_apt_cache();
@@ -134,6 +158,8 @@ __PACKAGE__->register_method({
            }
        }
 
+       PVE::Tools::file_set_contents($pve_pkgstatus_fn, encode_json($pkglist));
+
        return $pkglist;
     }});