From 4d03b3dbaf9d2f949d113406ac15961c93866c71 Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Sun, 28 Oct 2012 17:53:05 +0100 Subject: [PATCH] Add ETag caching for Javascript routes --- lib/router.php | 47 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/lib/router.php b/lib/router.php index d5adb72055a..dae2e91372e 100644 --- a/lib/router.php +++ b/lib/router.php @@ -17,7 +17,9 @@ class OC_Router { protected $collection = null; protected $root = null; - protected $generator= null; + protected $generator = null; + protected $routing_files; + protected $cache_key; public function __construct() { $baseUrl = OC_Helper::linkTo('', 'index.php'); @@ -29,18 +31,37 @@ class OC_Router { $this->root = $this->getCollection('root'); } + public function getRoutingFiles() { + if (!isset($this->routing_files)) { + $this->routing_files = array(); + foreach(OC_APP::getEnabledApps() as $app){ + $file = OC_App::getAppPath($app).'/appinfo/routes.php'; + if(file_exists($file)) { + $this->routing_files[$app] = $file; + } + } + } + return $this->routing_files; + } + + public function getCacheKey() { + if (!isset($this->cache_key)) { + $files = $this->getRoutingFiles(); + $files[] = 'core/routes.php'; + $this->cache_key = OC_Cache::generateCacheKeyFromFiles($files); + } + return $this->cache_key; + } + /** * loads the api routes */ public function loadRoutes() { - foreach(OC_APP::getEnabledApps() as $app){ - $file = OC_App::getAppPath($app).'/appinfo/routes.php'; - if(file_exists($file)) { - $this->useCollection($app); - require_once $file; - $collection = $this->getCollection($app); - $this->root->addCollection($collection, '/apps/'.$app); - } + foreach($this->getRoutingFiles() as $app => $file) { + $this->useCollection($app); + require_once $file; + $collection = $this->getCollection($app); + $this->root->addCollection($collection, '/apps/'.$app); } $this->useCollection('root'); require_once('core/routes.php'); @@ -128,10 +149,14 @@ class OC_Router { */ public static function JSRoutes() { - // TODO: http caching - $routes = array(); $router = OC::getRouter(); + + $etag = $router->getCacheKey(); + OC_Response::enableCaching(); + OC_Response::setETagHeader($etag); + $root = $router->getCollection('root'); + $routes = array(); foreach($root->all() as $name => $route) { $compiled_route = $route->compile(); $defaults = $route->getDefaults();