3 namespace Friendica\Core\PConfig;
5 use Friendica\Core\BasePConfig;
9 * This class implements the preload configuration, which will cache
10 * all user config values per call in a cache.
12 * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
14 class PreloadPConfig extends BasePConfig
17 private $config_loaded;
20 * @param Cache $configCache The configuration cache
21 * @param Model\Config\PConfig $configModel The configuration model
23 public function __construct(Cache $configCache, Model\Config\PConfig $configModel)
25 parent::__construct($configCache, $configModel);
26 $this->config_loaded = [];
32 * This loads all config values everytime load is called
35 public function load(int $uid, string $cat = 'config')
37 // Don't load the whole configuration twice or with invalid uid
38 if (!$uid || !empty($this->config_loaded[$uid])) {
42 // If not connected, do nothing
43 if (!$this->configModel->isConnected()) {
47 $config = $this->configModel->load($uid);
48 $this->config_loaded[$uid] = 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 (empty($this->config_loaded[$uid])) {
66 if ($this->configModel->isConnected()) {
67 $config = $this->configModel->get($uid, $cat, $key);
69 $this->configCache->set($uid, $cat, $key, $config);
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)
89 if (empty($this->config_loaded[$uid])) {
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 return $cached && $stored;
109 public function delete(int $uid, string $cat, string $key)
115 if (empty($this->config_loaded[$uid])) {
119 $cacheRemoved = $this->configCache->delete($uid, $cat, $key);
121 if (!$this->configModel->isConnected()) {
122 return $cacheRemoved;
125 $storeRemoved = $this->configModel->delete($uid, $cat, $key);
127 return $cacheRemoved || $storeRemoved;