cachingrouter.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * @author Morris Jobke <hey@morrisjobke.de>
  4. * @author Robin Appelman <icewind@owncloud.com>
  5. *
  6. * @copyright Copyright (c) 2015, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\Route;
  23. class CachingRouter extends Router {
  24. /**
  25. * @var \OCP\ICache
  26. */
  27. protected $cache;
  28. /**
  29. * @param \OCP\ICache $cache
  30. */
  31. public function __construct($cache) {
  32. $this->cache = $cache;
  33. parent::__construct();
  34. }
  35. /**
  36. * Generate url based on $name and $parameters
  37. *
  38. * @param string $name Name of the route to use.
  39. * @param array $parameters Parameters for the route
  40. * @param bool $absolute
  41. * @return string
  42. */
  43. public function generate($name, $parameters = array(), $absolute = false) {
  44. asort($parameters);
  45. $key = $this->context->getHost() . '#' . $this->context->getBaseUrl() . $name . json_encode($parameters) . intval($absolute);
  46. if ($this->cache->hasKey($key)) {
  47. return $this->cache->get($key);
  48. } else {
  49. $url = parent::generate($name, $parameters, $absolute);
  50. $this->cache->set($key, $url, 3600);
  51. return $url;
  52. }
  53. }
  54. }