Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
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.

PreloadConfigAdapter.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace Friendica\Core\Config;
  3. use Exception;
  4. use Friendica\Database\DBA;
  5. /**
  6. * Preload Configuration Adapter
  7. *
  8. * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
  9. *
  10. * @author Hypolite Petovan <hypolite@mrpetovan.com>
  11. */
  12. class PreloadConfigAdapter extends AbstractDbaConfigAdapter implements IConfigAdapter
  13. {
  14. private $config_loaded = false;
  15. /**
  16. * @var IConfigCache The config cache of this driver
  17. */
  18. private $configCache;
  19. /**
  20. * @param IConfigCache $configCache The config cache of this driver
  21. */
  22. public function __construct(IConfigCache $configCache)
  23. {
  24. $this->configCache = $configCache;
  25. $this->connected = DBA::connected();
  26. $this->load();
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function load($family = 'config')
  32. {
  33. if (!$this->isConnected()) {
  34. return;
  35. }
  36. if ($this->config_loaded) {
  37. return;
  38. }
  39. $configs = DBA::select('config', ['cat', 'v', 'k']);
  40. while ($config = DBA::fetch($configs)) {
  41. $this->configCache->set($config['cat'], $config['k'], $config['v']);
  42. }
  43. DBA::close($configs);
  44. $this->config_loaded = true;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function get($cat, $k, $default_value = null, $refresh = false)
  50. {
  51. if (!$this->isConnected()) {
  52. return $default_value;
  53. }
  54. if ($refresh) {
  55. $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
  56. if (DBA::isResult($config)) {
  57. $this->configCache->set($cat, $k, $config['v']);
  58. }
  59. }
  60. $return = $this->configCache->get($cat, $k, $default_value);
  61. return $return;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function set($cat, $k, $value)
  67. {
  68. if (!$this->isConnected()) {
  69. return false;
  70. }
  71. // We store our setting values as strings.
  72. // So we have to do the conversion here so that the compare below works.
  73. // The exception are array values.
  74. $compare_value = !is_array($value) ? (string)$value : $value;
  75. if ($this->configCache->get($cat, $k) === $compare_value) {
  76. return true;
  77. }
  78. $this->configCache->set($cat, $k, $value);
  79. // manage array value
  80. $dbvalue = is_array($value) ? serialize($value) : $value;
  81. $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true);
  82. if (!$result) {
  83. throw new Exception('Unable to store config value in [' . $cat . '][' . $k . ']');
  84. }
  85. return true;
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function delete($cat, $k)
  91. {
  92. if (!$this->isConnected()) {
  93. return false;
  94. }
  95. $this->configCache->delete($cat, $k);
  96. $result = DBA::delete('config', ['cat' => $cat, 'k' => $k]);
  97. return $result;
  98. }
  99. }