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.

PreloadPConfigAdapter.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace Friendica\Core\Config;
  3. use Exception;
  4. use Friendica\Database\DBA;
  5. /**
  6. * Preload User 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 PreloadPConfigAdapter implements IPConfigAdapter
  13. {
  14. private $config_loaded = false;
  15. /**
  16. * The config cache of this adapter
  17. * @var IPConfigCache
  18. */
  19. private $configCache;
  20. /**
  21. * @param IPConfigCache $configCache The config cache of this adapter
  22. * @param int $uid The UID of the current user
  23. */
  24. public function __construct(IPConfigCache $configCache, $uid = null)
  25. {
  26. $this->configCache = $configCache;
  27. if (isset($uid)) {
  28. $this->load($uid, 'config');
  29. }
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function load($uid, $family)
  35. {
  36. if ($this->config_loaded) {
  37. return;
  38. }
  39. if (empty($uid)) {
  40. return;
  41. }
  42. $pconfigs = DBA::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
  43. while ($pconfig = DBA::fetch($pconfigs)) {
  44. $this->configCache->setP($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']);
  45. }
  46. DBA::close($pconfigs);
  47. $this->config_loaded = true;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function get($uid, $cat, $k, $default_value = null, $refresh = false)
  53. {
  54. if (!$this->config_loaded) {
  55. $this->load($uid, $cat);
  56. }
  57. if ($refresh) {
  58. $config = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
  59. if (DBA::isResult($config)) {
  60. $this->configCache->setP($uid, $cat, $k, $config['v']);
  61. } else {
  62. $this->configCache->deleteP($uid, $cat, $k);
  63. }
  64. }
  65. return $this->configCache->getP($uid, $cat, $k, $default_value);;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function set($uid, $cat, $k, $value)
  71. {
  72. if (!$this->config_loaded) {
  73. $this->load($uid, $cat);
  74. }
  75. // We store our setting values as strings.
  76. // So we have to do the conversion here so that the compare below works.
  77. // The exception are array values.
  78. $compare_value = !is_array($value) ? (string)$value : $value;
  79. if ($this->configCache->getP($uid, $cat, $k) === $compare_value) {
  80. return true;
  81. }
  82. $this->configCache->setP($uid, $cat, $k, $value);
  83. // manage array value
  84. $dbvalue = is_array($value) ? serialize($value) : $value;
  85. $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
  86. if (!$result) {
  87. throw new Exception('Unable to store config value in [' . $uid . '][' . $cat . '][' . $k . ']');
  88. }
  89. return true;
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function delete($uid, $cat, $k)
  95. {
  96. if (!$this->config_loaded) {
  97. $this->load($uid, $cat);
  98. }
  99. $this->configCache->deleteP($uid, $cat, $k);
  100. $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
  101. return $result;
  102. }
  103. }