Add ETag caching for Javascript routes

This commit is contained in:
Bart Visscher 2012-10-28 17:53:05 +01:00
parent a7806076b1
commit 4d03b3dbaf
1 changed files with 36 additions and 11 deletions

View File

@ -17,7 +17,9 @@ class OC_Router {
protected $collection = null; protected $collection = null;
protected $root = null; protected $root = null;
protected $generator= null; protected $generator = null;
protected $routing_files;
protected $cache_key;
public function __construct() { public function __construct() {
$baseUrl = OC_Helper::linkTo('', 'index.php'); $baseUrl = OC_Helper::linkTo('', 'index.php');
@ -29,18 +31,37 @@ class OC_Router {
$this->root = $this->getCollection('root'); $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 * loads the api routes
*/ */
public function loadRoutes() { public function loadRoutes() {
foreach(OC_APP::getEnabledApps() as $app){ foreach($this->getRoutingFiles() as $app => $file) {
$file = OC_App::getAppPath($app).'/appinfo/routes.php'; $this->useCollection($app);
if(file_exists($file)) { require_once $file;
$this->useCollection($app); $collection = $this->getCollection($app);
require_once $file; $this->root->addCollection($collection, '/apps/'.$app);
$collection = $this->getCollection($app);
$this->root->addCollection($collection, '/apps/'.$app);
}
} }
$this->useCollection('root'); $this->useCollection('root');
require_once('core/routes.php'); require_once('core/routes.php');
@ -128,10 +149,14 @@ class OC_Router {
*/ */
public static function JSRoutes() public static function JSRoutes()
{ {
// TODO: http caching
$routes = array();
$router = OC::getRouter(); $router = OC::getRouter();
$etag = $router->getCacheKey();
OC_Response::enableCaching();
OC_Response::setETagHeader($etag);
$root = $router->getCollection('root'); $root = $router->getCollection('root');
$routes = array();
foreach($root->all() as $name => $route) { foreach($root->all() as $name => $route) {
$compiled_route = $route->compile(); $compiled_route = $route->compile();
$defaults = $route->getDefaults(); $defaults = $route->getDefaults();