StorageGlobal.php 3.5 KB

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