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.

JITPConfigAdapter.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace Friendica\Core\Config;
  3. use Friendica\Database\DBA;
  4. /**
  5. * JustInTime User Configuration Adapter
  6. *
  7. * Default PConfig Adapter. Provides the best performance for pages loading few configuration variables.
  8. *
  9. * @author Hypolite Petovan <hypolite@mrpetovan.com>
  10. */
  11. class JITPConfigAdapter implements IPConfigAdapter
  12. {
  13. private $in_db;
  14. /**
  15. * The config cache of this adapter
  16. * @var IPConfigCache
  17. */
  18. private $configCache;
  19. /**
  20. * @param IPConfigCache $configCache The config cache of this adapter
  21. */
  22. public function __construct(IPConfigCache $configCache)
  23. {
  24. $this->configCache = $configCache;
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function load($uid, $cat)
  30. {
  31. $pconfigs = DBA::select('pconfig', ['v', 'k'], ['cat' => $cat, 'uid' => $uid]);
  32. if (DBA::isResult($pconfigs)) {
  33. while ($pconfig = DBA::fetch($pconfigs)) {
  34. $k = $pconfig['k'];
  35. $this->configCache->setP($uid, $cat, $k, $pconfig['v']);
  36. $this->in_db[$uid][$cat][$k] = true;
  37. }
  38. } else if ($cat != 'config') {
  39. // Negative caching
  40. $this->configCache->setP($uid, $cat, null, "!<unset>!");
  41. }
  42. DBA::close($pconfigs);
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function get($uid, $cat, $k, $default_value = null, $refresh = false)
  48. {
  49. if (!$refresh) {
  50. // Looking if the whole family isn't set
  51. if ($this->configCache->getP($uid, $cat) !== null) {
  52. if ($this->configCache->getP($uid, $cat) === '!<unset>!') {
  53. return $default_value;
  54. }
  55. }
  56. if ($this->configCache->getP($uid, $cat, $k) !== null) {
  57. if ($this->configCache->getP($uid, $cat, $k) === '!<unset>!') {
  58. return $default_value;
  59. }
  60. return $this->configCache->getP($uid, $cat, $k);
  61. }
  62. }
  63. $pconfig = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
  64. if (DBA::isResult($pconfig)) {
  65. $val = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']);
  66. $this->configCache->setP($uid, $cat, $k, $val);
  67. $this->in_db[$uid][$cat][$k] = true;
  68. return $val;
  69. } else {
  70. $this->configCache->setP($uid, $cat, $k, '!<unset>!');
  71. $this->in_db[$uid][$cat][$k] = false;
  72. return $default_value;
  73. }
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function set($uid, $cat, $k, $value)
  79. {
  80. // We store our setting values in a string variable.
  81. // So we have to do the conversion here so that the compare below works.
  82. // The exception are array values.
  83. $dbvalue = (!is_array($value) ? (string)$value : $value);
  84. $stored = $this->get($uid, $cat, $k, null, true);
  85. if (($stored === $dbvalue) && $this->in_db[$uid][$cat][$k]) {
  86. return true;
  87. }
  88. $this->configCache->setP($uid, $cat, $k, $value);
  89. // manage array value
  90. $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
  91. $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $k], true);
  92. if ($result) {
  93. $this->in_db[$uid][$cat][$k] = true;
  94. }
  95. return $result;
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function delete($uid, $cat, $k)
  101. {
  102. $this->configCache->deleteP($uid, $cat, $k);
  103. if (!empty($this->in_db[$uid][$cat][$k])) {
  104. unset($this->in_db[$uid][$cat][$k]);
  105. }
  106. $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
  107. return $result;
  108. }
  109. }