summaryrefslogtreecommitdiffstats
path: root/lib/private/Route/CachingRouter.php
diff options
context:
space:
mode:
authorRoeland Jago Douma <rullzer@owncloud.com>2016-04-25 21:49:07 +0200
committerRoeland Jago Douma <rullzer@owncloud.com>2016-04-25 21:49:07 +0200
commitbb1e2f9d9d51e83da6ee0da9ce5fb2e415542119 (patch)
treeb5158aa03c9460235f4d30b04cca1d8ff16111ac /lib/private/Route/CachingRouter.php
parent5e57d3a0b9c42df1413c4d2245faf1bbbe768a63 (diff)
downloadnextcloud-server-bb1e2f9d9d51e83da6ee0da9ce5fb2e415542119.tar.gz
nextcloud-server-bb1e2f9d9d51e83da6ee0da9ce5fb2e415542119.zip
Move \OC\Route to PSR-4
Diffstat (limited to 'lib/private/Route/CachingRouter.php')
-rw-r--r--lib/private/Route/CachingRouter.php61
1 files changed, 61 insertions, 0 deletions
diff --git a/lib/private/Route/CachingRouter.php b/lib/private/Route/CachingRouter.php
new file mode 100644
index 00000000000..d6270dcf2c7
--- /dev/null
+++ b/lib/private/Route/CachingRouter.php
@@ -0,0 +1,61 @@
+<?php
+/**
+ * @author Morris Jobke <hey@morrisjobke.de>
+ * @author Robin Appelman <icewind@owncloud.com>
+ *
+ * @copyright Copyright (c) 2016, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OC\Route;
+
+use OCP\ILogger;
+
+class CachingRouter extends Router {
+ /**
+ * @var \OCP\ICache
+ */
+ protected $cache;
+
+ /**
+ * @param \OCP\ICache $cache
+ * @param ILogger $logger
+ */
+ public function __construct($cache, ILogger $logger) {
+ $this->cache = $cache;
+ parent::__construct($logger);
+ }
+
+ /**
+ * Generate url based on $name and $parameters
+ *
+ * @param string $name Name of the route to use.
+ * @param array $parameters Parameters for the route
+ * @param bool $absolute
+ * @return string
+ */
+ public function generate($name, $parameters = array(), $absolute = false) {
+ asort($parameters);
+ $key = $this->context->getHost() . '#' . $this->context->getBaseUrl() . $name . sha1(json_encode($parameters)) . intval($absolute);
+ if ($this->cache->hasKey($key)) {
+ return $this->cache->get($key);
+ } else {
+ $url = parent::generate($name, $parameters, $absolute);
+ $this->cache->set($key, $url, 3600);
+ return $url;
+ }
+ }
+}