3 namespace Friendica\Core\PConfig;
5 use Friendica\Core\BasePConfig;
9 * This class implements the Just-In-Time configuration, which will cache
10 * user config values in a cache, once they are retrieved.
12 * Default Configuration type.
13 * Provides the best performance for pages loading few configuration variables.
15 class JitPConfig extends BasePConfig
18 * @var array Array of already loaded db values (even if there was no value)
23 * @param Cache $configCache The configuration cache
24 * @param Model\Config\PConfig $configModel The configuration model
26 public function __construct(Cache $configCache, Model\Config\PConfig $configModel)
28 parent::__construct($configCache, $configModel);
29 $this->db_loaded = [];
36 public function load(int $uid, string $cat = 'config')
38 // If not connected or no uid, do nothing
39 if (!$uid || !$this->configModel->isConnected()) {
43 $config = $this->configModel->load($uid, $cat);
45 if (!empty($config[$cat])) {
46 foreach ($config[$cat] as $key => $value) {
47 $this->db_loaded[$uid][$cat][$key] = true;
51 // load the whole category out of the DB into the cache
52 $this->configCache->load($uid, $config);
58 public function get(int $uid, string $cat, string $key, $default_value = null, bool $refresh = false)
61 return $default_value;
64 // if the value isn't loaded or refresh is needed, load it to the cache
65 if ($this->configModel->isConnected() &&
66 (empty($this->db_loaded[$uid][$cat][$key]) ||
69 $dbvalue = $this->configModel->get($uid, $cat, $key);
71 if (isset($dbvalue)) {
72 $this->configCache->set($uid, $cat, $key, $dbvalue);
76 $this->db_loaded[$uid][$cat][$key] = true;
79 // use the config cache for return
80 $result = $this->configCache->get($uid, $cat, $key);
82 return (isset($result)) ? $result : $default_value;
88 public function set(int $uid, string $cat, string $key, $value)
94 // set the cache first
95 $cached = $this->configCache->set($uid, $cat, $key, $value);
97 // If there is no connected adapter, we're finished
98 if (!$this->configModel->isConnected()) {
102 $stored = $this->configModel->set($uid, $cat, $key, $value);
104 $this->db_loaded[$uid][$cat][$key] = $stored;
106 return $cached && $stored;
112 public function delete(int $uid, string $cat, string $key)
118 $cacheRemoved = $this->configCache->delete($uid, $cat, $key);
120 if (isset($this->db_loaded[$uid][$cat][$key])) {
121 unset($this->db_loaded[$uid][$cat][$key]);
124 if (!$this->configModel->isConnected()) {
125 return $cacheRemoved;
128 $storeRemoved = $this->configModel->delete($uid, $cat, $key);
130 return $cacheRemoved || $storeRemoved;