1
0

StorageGlobal.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 array<string, array> */
  22. private $cache = [];
  23. /** @var array<int, array> */
  24. private $numericIdCache = [];
  25. public function __construct(
  26. private IDBConnection $connection,
  27. ) {
  28. }
  29. /**
  30. * @param string[] $storageIds
  31. */
  32. public function loadForStorageIds(array $storageIds) {
  33. $builder = $this->connection->getQueryBuilder();
  34. $query = $builder->select(['id', 'numeric_id', 'available', 'last_checked'])
  35. ->from('storages')
  36. ->where($builder->expr()->in('id', $builder->createNamedParameter(array_values($storageIds), IQueryBuilder::PARAM_STR_ARRAY)));
  37. $result = $query->executeQuery();
  38. while ($row = $result->fetch()) {
  39. $this->cache[$row['id']] = $row;
  40. }
  41. $result->closeCursor();
  42. }
  43. /**
  44. * @param string $storageId
  45. * @return array|null
  46. */
  47. public function getStorageInfo(string $storageId): ?array {
  48. if (!isset($this->cache[$storageId])) {
  49. $builder = $this->connection->getQueryBuilder();
  50. $query = $builder->select(['id', 'numeric_id', 'available', 'last_checked'])
  51. ->from('storages')
  52. ->where($builder->expr()->eq('id', $builder->createNamedParameter($storageId)));
  53. $result = $query->executeQuery();
  54. $row = $result->fetch();
  55. $result->closeCursor();
  56. if ($row) {
  57. $this->cache[$storageId] = $row;
  58. $this->numericIdCache[(int)$row['numeric_id']] = $row;
  59. }
  60. }
  61. return $this->cache[$storageId] ?? null;
  62. }
  63. /**
  64. * @param int $numericId
  65. * @return array|null
  66. */
  67. public function getStorageInfoByNumericId(int $numericId): ?array {
  68. if (!isset($this->numericIdCache[$numericId])) {
  69. $builder = $this->connection->getQueryBuilder();
  70. $query = $builder->select(['id', 'numeric_id', 'available', 'last_checked'])
  71. ->from('storages')
  72. ->where($builder->expr()->eq('numeric_id', $builder->createNamedParameter($numericId)));
  73. $result = $query->executeQuery();
  74. $row = $result->fetch();
  75. $result->closeCursor();
  76. if ($row) {
  77. $this->numericIdCache[$numericId] = $row;
  78. $this->cache[$row['id']] = $row;
  79. }
  80. }
  81. return $this->numericIdCache[$numericId] ?? null;
  82. }
  83. public function clearCache() {
  84. $this->cache = [];
  85. }
  86. }