cachingrouter.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. /**
  3. * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\Route;
  9. class CachingRouter extends Router {
  10. /**
  11. * @var \OCP\ICache
  12. */
  13. protected $cache;
  14. /**
  15. * @param \OCP\ICache $cache
  16. */
  17. public function __construct($cache) {
  18. $this->cache = $cache;
  19. parent::__construct();
  20. }
  21. /**
  22. * Generate url based on $name and $parameters
  23. *
  24. * @param string $name Name of the route to use.
  25. * @param array $parameters Parameters for the route
  26. * @param bool $absolute
  27. * @return string
  28. */
  29. public function generate($name, $parameters = array(), $absolute = false) {
  30. asort($parameters);
  31. $key = $this->context->getHost() . '#' . $this->context->getBaseUrl() . $name . json_encode($parameters) . intval($absolute);
  32. if ($this->cache->hasKey($key)) {
  33. return $this->cache->get($key);
  34. } else {
  35. $url = parent::generate($name, $parameters, $absolute);
  36. $this->cache->set($key, $url, 3600);
  37. return $url;
  38. }
  39. }
  40. }