Skip to content

Instantly share code, notes, and snippets.

@ichikaway
Created August 9, 2012 01:58
Show Gist options
  • Save ichikaway/3300279 to your computer and use it in GitHub Desktop.
Save ichikaway/3300279 to your computer and use it in GitHub Desktop.
cache router url
diff --git a/lib/Cake/Routing/Router.php b/lib/Cake/Routing/Router.php
index cb28f6e..c6a38c3 100644
--- a/lib/Cake/Routing/Router.php
+++ b/lib/Cake/Routing/Router.php
@@ -810,23 +810,30 @@ class Router {
$url += array('controller' => $params['controller'], 'plugin' => $params['plugin']);
- $match = false;
+ $cacheKey = 'Router-' . sha1(serialize($url));
+ $cacheConf = Cache::settings('_cake_route_');
+ if( empty($cacheConf) || ($output = Cache::read($cacheKey, '_cake_route_') ) === false ){
+ $match = false;
- for ($i = 0, $len = count(self::$routes); $i < $len; $i++) {
- $originalUrl = $url;
+ for ($i = 0, $len = count(self::$routes); $i < $len; $i++) {
+ $originalUrl = $url;
- if (isset(self::$routes[$i]->options['persist'], $params)) {
- $url = self::$routes[$i]->persistParams($url, $params);
- }
+ if (isset(self::$routes[$i]->options['persist'], $params)) {
+ $url = self::$routes[$i]->persistParams($url, $params);
+ }
- if ($match = self::$routes[$i]->match($url)) {
- $output = trim($match, '/');
- break;
+ if ($match = self::$routes[$i]->match($url)) {
+ $output = trim($match, '/');
+ break;
+ }
+ $url = $originalUrl;
+ }
+ if ($match === false) {
+ $output = self::_handleNoRoute($url);
+ }
+ if(!empty($cacheConf)) {
+ Cache::write($cacheKey , $output, '_cake_route_');
}
- $url = $originalUrl;
- }
- if ($match === false) {
- $output = self::_handleNoRoute($url);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment