1
0

StorageGlobal.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Files\Cache;
  7. use OCP\DB\QueryBuilder\IQueryBuilder;
  8. use OCP\IDBConnection;
  9. /**
  10. * Handle the mapping between the string and numeric storage ids
  11. *
  12. * Each storage has 2 different ids
  13. * a string id which is generated by the storage backend and reflects the configuration of the storage (e.g. 'smb://user@host/share')
  14. * and a numeric storage id which is referenced in the file cache
  15. *
  16. * A mapping between the two storage ids is stored in the database and accessible through this class
  17. *
  18. * @package OC\Files\Cache
  19. */
  20. class StorageGlobal {
  21. /** @var IDBConnection */
  22. private $connection;
  23. /** @var array<string, array> */
  24. private $cache = [];
  25. /** @var array<int, array> */
  26. private $numericIdCache = [];
  27. public function __construct(IDBConnection $connection) {
  28. $this->connection = $connection;
  29. }
  30. /**
  31. * @param string[] $storageIds
  32. */
  33. public function loadForStorageIds(array $storageIds) {
  34. $builder = $this->connection->getQueryBuilder();
  35. $query = $builder->select(['id', 'numeric_id', 'available', 'last_checked'])
  36. ->from('storages')
  37. ->where($builder->expr()->in('id', $builder->createNamedParameter(array_values($storageIds), IQueryBuilder::PARAM_STR_ARRAY)));
  38. $result = $query->execute();
  39. while ($row = $result->fetch()) {
  40. $this->cache[$row['id']] = $row;
  41. }
  42. $result->closeCursor();
  43. }
  44. /**
  45. * @param string $storageId
  46. * @return array|null
  47. */
  48. public function getStorageInfo(string $storageId): ?array {
  49. if (!isset($this->cache[$storageId])) {
  50. $builder = $this->connection->getQueryBuilder();
  51. $query = $builder->select(['id', 'numeric_id', 'available', 'last_checked'])
  52. ->from('storages')
  53. ->where($builder->expr()->eq('id', $builder->createNamedParameter($storageId)));
  54. $result = $query->execute();
  55. $row = $result->fetch();
  56. $result->closeCursor();
  57. if ($row) {
  58. $this->cache[$storageId] = $row;
  59. $this->numericIdCache[(int)$row['numeric_id']] = $row;
  60. }
  61. }
  62. return $this->cache[$storageId] ?? null;
  63. }
  64. /**
  65. * @param int $numericId
  66. * @return array|null
  67. */
  68. public function getStorageInfoByNumericId(int $numericId): ?array {
  69. if (!isset($this->numericIdCache[$numericId])) {
  70. $builder = $this->connection->getQueryBuilder();
  71. $query = $builder->select(['id', 'numeric_id', 'available', 'last_checked'])
  72. ->from('storages')
  73. ->where($builder->expr()->eq('numeric_id', $builder->createNamedParameter($numericId)));
  74. $result = $query->execute();
  75. $row = $result->fetch();
  76. $result->closeCursor();
  77. if ($row) {
  78. $this->numericIdCache[$numericId] = $row;
  79. $this->cache[$row['id']] = $row;
  80. }
  81. }
  82. return $this->numericIdCache[$numericId] ?? null;
  83. }
  84. public function clearCache() {
  85. $this->cache = [];
  86. }
  87. }