3 namespace Friendica\Core\Config;
8 * This class implements the preload configuration, which will cache
9 * all config values per call in a cache.
11 * Minimizes the number of database queries to retrieve configuration values at the cost of memory.
13 class PreloadConfiguration extends Configuration
16 private $config_loaded;
19 * @param Cache\ConfigCache $configCache The configuration cache (based on the config-files)
20 * @param Model\Config\Config $configModel The configuration model
22 public function __construct(Cache\ConfigCache $configCache, Model\Config\Config $configModel)
24 parent::__construct($configCache, $configModel);
25 $this->config_loaded = false;
33 * This loads all config values everytime load is called
36 public function load(string $cat = 'config')
38 // Don't load the whole configuration twice
39 if ($this->config_loaded) {
43 // If not connected, do nothing
44 if (!$this->configModel->isConnected()) {
48 $config = $this->configModel->load();
49 $this->config_loaded = true;
51 // load the whole category out of the DB into the cache
52 $this->configCache->load($config, true);
58 public function get(string $cat, string $key, $default_value = null, bool $refresh = false)
61 if ($this->configModel->isConnected()) {
62 $config = $this->configModel->get($cat, $key);
64 $this->configCache->set($cat, $key, $config);
69 // use the config cache for return
70 $result = $this->configCache->get($cat, $key);
72 return (isset($result)) ? $result : $default_value;
78 public function set(string $cat, string $key, $value)
80 if (!$this->config_loaded) {
84 // set the cache first
85 $cached = $this->configCache->set($cat, $key, $value);
87 // If there is no connected adapter, we're finished
88 if (!$this->configModel->isConnected()) {
92 $stored = $this->configModel->set($cat, $key, $value);
94 return $cached && $stored;
100 public function delete(string $cat, string $key)
102 if ($this->config_loaded) {
106 $cacheRemoved = $this->configCache->delete($cat, $key);
108 if (!$this->configModel->isConnected()) {
109 return $cacheRemoved;
112 $storeRemoved = $this->configModel->delete($cat, $key);
114 return $cacheRemoved || $storeRemoved;