StorageGlobal.php 2.4 KB

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