DBMap.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. /**
  15. * Ключ модели для контейнера
  16. * @var string
  17. */
  18. protected $cKey = 'DBMap';
  19. /**
  20. * Загружает карту БД из кеша/БД
  21. */
  22. public function init(): DBMap
  23. {
  24. $map = $this->c->Cache->get('db_map');
  25. if (! \is_array($map)) {
  26. $map = $this->c->DB->getMap();
  27. if (true !== $this->c->Cache->set('db_map', $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('db_map')) {
  40. throw new RuntimeException('Unable to remove key from cache - db_map');
  41. }
  42. return $this;
  43. }
  44. }