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

JITConfigAdapter.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace Friendica\Core\Config;
  3. use Friendica\Database\DBA;
  4. /**
  5. * JustInTime Configuration Adapter
  6. *
  7. * Default Config Adapter. Provides the best performance for pages loading few configuration variables.
  8. *
  9. * @author Hypolite Petovan <hypolite@mrpetovan.com>
  10. */
  11. class JITConfigAdapter extends AbstractDbaConfigAdapter implements IConfigAdapter
  12. {
  13. private $cache;
  14. private $in_db;
  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. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function load($cat = "config")
  31. {
  32. if (!$this->isConnected()) {
  33. return;
  34. }
  35. // We don't preload "system" anymore.
  36. // This reduces the number of database reads a lot.
  37. if ($cat === 'system') {
  38. return;
  39. }
  40. $configs = DBA::select('config', ['v', 'k'], ['cat' => $cat]);
  41. while ($config = DBA::fetch($configs)) {
  42. $k = $config['k'];
  43. $this->configCache->set($cat, $k, $config['v']);
  44. if ($cat !== 'config') {
  45. $this->cache[$cat][$k] = $config['v'];
  46. $this->in_db[$cat][$k] = true;
  47. }
  48. }
  49. DBA::close($configs);
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function get($cat, $k, $default_value = null, $refresh = false)
  55. {
  56. if (!$this->isConnected()) {
  57. return $default_value;
  58. }
  59. if (!$refresh) {
  60. // Do we have the cached value? Then return it
  61. if (isset($this->cache[$cat][$k])) {
  62. if ($this->cache[$cat][$k] === '!<unset>!') {
  63. return $default_value;
  64. } else {
  65. return $this->cache[$cat][$k];
  66. }
  67. }
  68. }
  69. $config = DBA::selectFirst('config', ['v'], ['cat' => $cat, 'k' => $k]);
  70. if (DBA::isResult($config)) {
  71. // manage array value
  72. $value = (preg_match("|^a:[0-9]+:{.*}$|s", $config['v']) ? unserialize($config['v']) : $config['v']);
  73. // Assign the value from the database to the cache
  74. $this->cache[$cat][$k] = $value;
  75. $this->in_db[$cat][$k] = true;
  76. return $value;
  77. } elseif ($this->configCache->get($cat, $k) !== null) {
  78. // Assign the value (mostly) from config/local.config.php file to the cache
  79. $this->cache[$cat][$k] = $this->configCache->get($cat, $k);
  80. $this->in_db[$cat][$k] = false;
  81. return $this->configCache->get($cat, $k);
  82. } elseif ($this->configCache->get('config', $k) !== null) {
  83. // Assign the value (mostly) from config/local.config.php file to the cache
  84. $this->cache[$k] = $this->configCache->get('config', $k);
  85. $this->in_db[$k] = false;
  86. return $this->configCache->get('config', $k);
  87. }
  88. $this->cache[$cat][$k] = '!<unset>!';
  89. $this->in_db[$cat][$k] = false;
  90. return $default_value;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function set($cat, $k, $value)
  96. {
  97. if (!$this->isConnected()) {
  98. return false;
  99. }
  100. // We store our setting values in a string variable.
  101. // So we have to do the conversion here so that the compare below works.
  102. // The exception are array values.
  103. $dbvalue = (!is_array($value) ? (string)$value : $value);
  104. $stored = $this->get($cat, $k, null, true);
  105. if (!isset($this->in_db[$cat])) {
  106. $this->in_db[$cat] = [];
  107. }
  108. if (!isset($this->in_db[$cat][$k])) {
  109. $this->in_db[$cat] = false;
  110. }
  111. if (($stored === $dbvalue) && $this->in_db[$cat][$k]) {
  112. return true;
  113. }
  114. $this->configCache->set($cat, $k, $value);
  115. // Assign the just added value to the cache
  116. $this->cache[$cat][$k] = $dbvalue;
  117. // manage array value
  118. $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
  119. $result = DBA::update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $k], true);
  120. if ($result) {
  121. $this->in_db[$cat][$k] = true;
  122. }
  123. return $result;
  124. }
  125. /**
  126. * {@inheritdoc}
  127. */
  128. public function delete($cat, $k)
  129. {
  130. if (!$this->isConnected()) {
  131. return false;
  132. }
  133. if (isset($this->cache[$cat][$k])) {
  134. unset($this->cache[$cat][$k]);
  135. unset($this->in_db[$cat][$k]);
  136. }
  137. $result = DBA::delete('config', ['cat' => $cat, 'k' => $k]);
  138. return $result;
  139. }
  140. }