You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.8 KiB

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