3 namespace Friendica\Core\Config;
8 * This class implements the Just-In-Time configuration, which will cache
9 * user config values in a cache, once they are retrieved.
11 * Default Configuration type.
12 * Provides the best performance for pages loading few configuration variables.
14 class JitPConfiguration extends PConfiguration
17 * @var array Array of already loaded db values (even if there was no value)
22 * @param Cache\PConfigCache $configCache The configuration cache
23 * @param Model\Config\PConfig $configModel The configuration model
25 public function __construct(Cache\PConfigCache $configCache, Model\Config\PConfig $configModel)
27 parent::__construct($configCache, $configModel);
28 $this->db_loaded = [];
35 public function load(int $uid, string $cat = 'config')
37 // If not connected or no uid, do nothing
38 if (!$uid || !$this->configModel->isConnected()) {
42 $config = $this->configModel->load($uid, $cat);
44 if (!empty($config[$cat])) {
45 foreach ($config[$cat] as $key => $value) {
46 $this->db_loaded[$uid][$cat][$key] = true;
50 // load the whole category out of the DB into the cache
51 $this->configCache->load($uid, $config);
57 public function get(int $uid, string $cat, string $key, $default_value = null, bool $refresh = false)
60 return $default_value;
63 // if the value isn't loaded or refresh is needed, load it to the cache
64 if ($this->configModel->isConnected() &&
65 (empty($this->db_loaded[$uid][$cat][$key]) ||
68 $dbvalue = $this->configModel->get($uid, $cat, $key);
70 if (isset($dbvalue)) {
71 $this->configCache->set($uid, $cat, $key, $dbvalue);
75 $this->db_loaded[$uid][$cat][$key] = true;
78 // use the config cache for return
79 $result = $this->configCache->get($uid, $cat, $key);
81 return (isset($result)) ? $result : $default_value;
87 public function set(int $uid, string $cat, string $key, $value)
93 // set the cache first
94 $cached = $this->configCache->set($uid, $cat, $key, $value);
96 // If there is no connected adapter, we're finished
97 if (!$this->configModel->isConnected()) {
101 $stored = $this->configModel->set($uid, $cat, $key, $value);
103 $this->db_loaded[$uid][$cat][$key] = $stored;
105 return $cached && $stored;
111 public function delete(int $uid, string $cat, string $key)
117 $cacheRemoved = $this->configCache->delete($uid, $cat, $key);
119 if (isset($this->db_loaded[$uid][$cat][$key])) {
120 unset($this->db_loaded[$uid][$cat][$key]);
123 if (!$this->configModel->isConnected()) {
124 return $cacheRemoved;
127 $storeRemoved = $this->configModel->delete($uid, $cat, $key);
129 return $cacheRemoved || $storeRemoved;