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.

StorageGlobal.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 trough this class
  35. *
  36. * @package OC\Files\Cache
  37. */
  38. class StorageGlobal {
  39. /** @var IDBConnection */
  40. private $connection;
  41. /** @var array[] */
  42. private $cache = [];
  43. public function __construct(IDBConnection $connection) {
  44. $this->connection = $connection;
  45. }
  46. /**
  47. * @param string[] $storageIds
  48. */
  49. public function loadForStorageIds(array $storageIds) {
  50. $builder = $this->connection->getQueryBuilder();
  51. $query = $builder->select(['id', 'numeric_id', 'available', 'last_checked'])
  52. ->from('storages')
  53. ->where($builder->expr()->in('id', $builder->createNamedParameter(array_values($storageIds), IQueryBuilder::PARAM_STR_ARRAY)));
  54. $result = $query->execute();
  55. while ($row = $result->fetch()) {
  56. $this->cache[$row['id']] = $row;
  57. }
  58. $result->closeCursor();
  59. }
  60. /**
  61. * @param string $storageId
  62. * @return array|null
  63. */
  64. public function getStorageInfo($storageId) {
  65. if (!isset($this->cache[$storageId])) {
  66. $builder = $this->connection->getQueryBuilder();
  67. $query = $builder->select(['id', 'numeric_id', 'available', 'last_checked'])
  68. ->from('storages')
  69. ->where($builder->expr()->eq('id', $builder->createNamedParameter($storageId)));
  70. $result = $query->execute();
  71. $row = $result->fetch();
  72. $result->closeCursor();
  73. if ($row) {
  74. $this->cache[$storageId] = $row;
  75. }
  76. }
  77. return isset($this->cache[$storageId]) ? $this->cache[$storageId] : null;
  78. }
  79. public function clearCache() {
  80. $this->cache = [];
  81. }
  82. }