]> source.dussan.org Git - nextcloud-server.git/commitdiff
Cache the minimized output also on the server
authorBart Visscher <bartv@thisnet.nl>
Fri, 15 Jun 2012 10:07:31 +0000 (12:07 +0200)
committerBart Visscher <bartv@thisnet.nl>
Mon, 18 Jun 2012 09:11:46 +0000 (11:11 +0200)
core/minimizer.php
lib/minimizer.php
lib/request.php [new file with mode: 0644]

index 709c7508e90a72ea946a74365bdd0075e1668bab..47e3d855e7b31857fa754adb4e826c81a8e8423c 100644 (file)
@@ -6,10 +6,10 @@ OC_App::loadApps();
 if ($service == 'core.css'){
        $minimizer = new OC_Minimizer_CSS();
        $files = $minimizer->findFiles(OC_Util::$core_styles);
-       $minimizer->output($files);
+       $minimizer->output($files, $service);
 }
 else if ($service == 'core.js'){
        $minimizer = new OC_Minimizer_JS();
        $files = $minimizer->findFiles(OC_Util::$core_scripts);
-       $minimizer->output($files);
+       $minimizer->output($files, $service);
 }
index 9f9ef086c4a60bf18aa8d93674fa49127ac96d83..428fa477f77b4ee8b89d2baa3239a19e0c17662d 100644 (file)
@@ -26,14 +26,30 @@ abstract class OC_Minimizer
 
        abstract public function minimizeFiles($files);
 
-       public function output($files) {
+       public function output($files, $cache_key) {
                header('Content-Type: '.$this->contentType);
                OC_Response::enableCaching();
                $last_modified = $this->getLastModified($files);
                OC_Response::setLastModifiedHeader($last_modified);
 
-               $out = $this->minimizeFiles($files);
-               OC_Response::setETagHeader(md5($out));
+               $gzout = false;
+               $cache = new OC_Cache_FileGlobal();
+               if (!OC_Request::isNoCache() && (!defined('DEBUG') || !DEBUG)){
+                       $gzout = $cache->get($cache_key.'.gz');
+                       OC_Response::setETagHeader(md5($gzout));
+               }
+
+               if (!$gzout) {
+                       $out = $this->minimizeFiles($files);
+                       $gzout = gzencode($out);
+                       $cache->set($cache_key.'.gz', $gzout);
+               }
+               if ($encoding = OC_Request::acceptGZip()) {
+                       header('Content-Encoding: '.$encoding);
+                       $out = $gzout;
+               } else {
+                       $out = gzdecode($gzout);
+               }
                header('Content-Length: '.strlen($out));
                echo $out;
        }
diff --git a/lib/request.php b/lib/request.php
new file mode 100644 (file)
index 0000000..d152d0c
--- /dev/null
@@ -0,0 +1,25 @@
+<?php
+/**
+ * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+class OC_Request {
+       static public function isNoCache() {
+               if (!isset($_SERVER['HTTP_CACHE_CONTROL'])) {
+                       return false;
+               }
+               return $_SERVER['HTTP_CACHE_CONTROL'] == 'no-cache';
+       }
+
+       static public function acceptGZip() {
+               $HTTP_ACCEPT_ENCODING = $_SERVER["HTTP_ACCEPT_ENCODING"];
+               if( strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false )
+                       return 'x-gzip';
+               else if( strpos($HTTP_ACCEPT_ENCODING,'gzip') !== false )
+                       return 'gzip';
+               return false;
+       }
+}