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.

78 lines
2.3 KiB

  1. <?php
  2. /**
  3. * @copyright Robin Appelman <robin@icewind.nl>
  4. *
  5. * @license AGPL-3.0
  6. *
  7. * This code is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License, version 3,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License, version 3,
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>
  18. *
  19. */
  20. namespace OC\Files\Cache;
  21. use OCP\DB\QueryBuilder\IQueryBuilder;
  22. use OCP\Files\Mount\IMountPoint;
  23. use OCP\IDBConnection;
  24. /**
  25. * Handle the mapping between the string and numeric storage ids
  26. *
  27. * Each storage has 2 different ids
  28. * a string id which is generated by the storage backend and reflects the configuration of the storage (e.g. 'smb://user@host/share')
  29. * and a numeric storage id which is referenced in the file cache
  30. *
  31. * A mapping between the two storage ids is stored in the database and accessible trough this class
  32. *
  33. * @package OC\Files\Cache
  34. */
  35. class StorageGlobal {
  36. /** @var IDBConnection */
  37. private $connection;
  38. /** @var array[] */
  39. private $cache = [];
  40. public function __construct(IDBConnection $connection) {
  41. $this->connection = $connection;
  42. }
  43. /**
  44. * @param string[] $storageIds
  45. */
  46. public function loadForStorageIds(array $storageIds) {
  47. $builder = $this->connection->getQueryBuilder();
  48. $query = $builder->select(['id', 'numeric_id', 'available', 'last_checked'])
  49. ->from('storages')
  50. ->where($builder->expr()->in('id', $builder->createNamedParameter(array_values($storageIds), IQueryBuilder::PARAM_STR_ARRAY)));
  51. $result = $query->execute();
  52. while ($row = $result->fetch()) {
  53. $this->cache[$row['id']] = $row;
  54. }
  55. }
  56. /**
  57. * @param string $storageId
  58. * @return array|null
  59. */
  60. public function getStorageInfo($storageId) {
  61. if (!isset($this->cache[$storageId])) {
  62. $this->loadForStorageIds([$storageId]);
  63. }
  64. return isset($this->cache[$storageId]) ? $this->cache[$storageId] : null;
  65. }
  66. public function clearCache() {
  67. $this->cache = [];
  68. }
  69. }