DBMap.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /**
  3. * This file is part of the ForkBB <https://github.com/forkbb>.
  4. *
  5. * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
  6. * @license The MIT License (MIT)
  7. */
  8. declare(strict_types=1);
  9. namespace ForkBB\Models\DBMap;
  10. use ForkBB\Models\Model;
  11. use RuntimeException;
  12. class DBMap extends Model
  13. {
  14. const CACHE_KEY = 'db_map';
  15. /**
  16. * Ключ модели для контейнера
  17. */
  18. protected string $cKey = 'DBMap';
  19. /**
  20. * Загружает карту БД из кеша/БД
  21. */
  22. public function init(): DBMap
  23. {
  24. $map = $this->c->Cache->get(self::CACHE_KEY);
  25. if (! \is_array($map)) {
  26. $map = $this->c->DB->getMap();
  27. if (true !== $this->c->Cache->set(self::CACHE_KEY, $map)) {
  28. throw new RuntimeException('Unable to write value to cache - db_map');
  29. }
  30. }
  31. $this->setAttrs($map);
  32. return $this;
  33. }
  34. /**
  35. * Сбрасывает кеш карты БД
  36. */
  37. public function reset(): DBMap
  38. {
  39. if (true !== $this->c->Cache->delete(self::CACHE_KEY)) {
  40. throw new RuntimeException('Unable to remove key from cache - db_map');
  41. }
  42. return $this;
  43. }
  44. }