2 namespace Friendica\Core\Config\Adapter;
4 use Friendica\Database\DBA;
7 * JustInTime User Configuration Adapter
9 * Default PConfig Adapter. Provides the best performance for pages loading few configuration variables.
11 * @author Hypolite Petovan <hypolite@mrpetovan.com>
13 class JITPConfigAdapter extends AbstractDbaConfigAdapter implements IPConfigAdapter
20 public function load($uid, $cat)
24 if (!$this->isConnected()) {
28 $pconfigs = DBA::select('pconfig', ['v', 'k'], ['cat' => $cat, 'uid' => $uid]);
29 if (DBA::isResult($pconfigs)) {
30 while ($pconfig = DBA::fetch($pconfigs)) {
32 $value = $this->toConfigValue($pconfig['v']);
34 // The value was in the db, so don't check it again (unless you have to)
35 $this->in_db[$uid][$cat][$key] = true;
38 $return[$key] = $value;
41 } else if ($cat != 'config') {
45 DBA::close($pconfigs);
47 return [$cat => $return];
53 public function get($uid, $cat, $key)
55 if (!$this->isConnected()) {
59 // The value was in the db, so don't check it again (unless you have to)
60 $this->in_db[$uid][$cat][$key] = true;
62 $pconfig = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
63 if (DBA::isResult($pconfig)) {
64 $value = $this->toConfigValue($pconfig['v']);
71 $this->in_db[$uid][$cat][$key] = false;
78 public function set($uid, $cat, $key, $value)
80 if (!$this->isConnected()) {
84 // We store our setting values in a string variable.
85 // So we have to do the conversion here so that the compare below works.
86 // The exception are array values.
87 $dbvalue = (!is_array($value) ? (string)$value : $value);
89 $stored = $this->get($uid, $cat, $key);
91 if (!isset($this->in_db[$uid])) {
92 $this->in_db[$uid] = [];
94 if (!isset($this->in_db[$uid][$cat])) {
95 $this->in_db[$uid][$cat] = [];
97 if (!isset($this->in_db[$uid][$cat][$key])) {
98 $this->in_db[$uid][$cat][$key] = false;
101 if (($stored === $dbvalue) && $this->in_db[$uid][$cat][$key]) {
105 // manage array value
106 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
108 $result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
110 $this->in_db[$uid][$cat][$key] = $result;
118 public function delete($uid, $cat, $key)
120 if (!$this->isConnected()) {
124 if (isset($this->in_db[$uid][$cat][$key])) {
125 unset($this->in_db[$uid][$cat][$key]);
128 return DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
134 public function isLoaded($uid, $cat, $key)
136 if (!$this->isConnected()) {
140 return (isset($this->in_db[$uid][$cat][$key])) && $this->in_db[$uid][$cat][$key];