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, do nothing
38 if (!$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)
59 // if the value isn't loaded or refresh is needed, load it to the cache
60 if ($this->configModel->isConnected() &&
61 (empty($this->db_loaded[$uid][$cat][$key]) ||
64 $dbvalue = $this->configModel->get($uid, $cat, $key);
66 if (isset($dbvalue)) {
67 $this->configCache->set($uid, $cat, $key, $dbvalue);
71 $this->db_loaded[$uid][$cat][$key] = true;
74 // use the config cache for return
75 $result = $this->configCache->get($uid, $cat, $key);
77 return (isset($result)) ? $result : $default_value;
83 public function set(int $uid, string $cat, string $key, $value)
85 // set the cache first
86 $cached = $this->configCache->set($uid, $cat, $key, $value);
88 // If there is no connected adapter, we're finished
89 if (!$this->configModel->isConnected()) {
93 $stored = $this->configModel->set($uid, $cat, $key, $value);
95 $this->db_loaded[$uid][$cat][$key] = $stored;
97 return $cached && $stored;
103 public function delete(int $uid, string $cat, string $key)
105 $cacheRemoved = $this->configCache->delete($uid, $cat, $key);
107 if (isset($this->db_loaded[$uid][$cat][$key])) {
108 unset($this->db_loaded[$uid][$cat][$key]);
111 if (!$this->configModel->isConnected()) {
112 return $cacheRemoved;
115 $storeRemoved = $this->configModel->delete($uid, $cat, $key);
117 return $cacheRemoved || $storeRemoved;