3 namespace Friendica\Core\Config;
5 use Friendica\Core\BaseConfig;
9 * This class implements the preload configuration, which will cache
10 * all 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 PreloadConfig extends BaseConfig
17 private $config_loaded;
20 * @param Cache $configCache The configuration cache (based on the config-files)
21 * @param Model\Config\Config $configModel The configuration model
23 public function __construct(Cache $configCache, Model\Config\Config $configModel)
25 parent::__construct($configCache, $configModel);
26 $this->config_loaded = false;
34 * This loads all config values everytime load is called
37 public function load(string $cat = 'config')
39 // Don't load the whole configuration twice
40 if ($this->config_loaded) {
44 // If not connected, do nothing
45 if (!$this->configModel->isConnected()) {
49 $config = $this->configModel->load();
50 $this->config_loaded = true;
52 // load the whole category out of the DB into the cache
53 $this->configCache->load($config, true);
59 public function get(string $cat, string $key, $default_value = null, bool $refresh = false)
62 if ($this->configModel->isConnected()) {
63 $config = $this->configModel->get($cat, $key);
65 $this->configCache->set($cat, $key, $config);
70 // use the config cache for return
71 $result = $this->configCache->get($cat, $key);
73 return (isset($result)) ? $result : $default_value;
79 public function set(string $cat, string $key, $value)
81 if (!$this->config_loaded) {
85 // set the cache first
86 $cached = $this->configCache->set($cat, $key, $value);
88 // If there is no connected adapter, we're finished
89 if (!$this->configModel->isConnected()) {
93 $stored = $this->configModel->set($cat, $key, $value);
95 return $cached && $stored;
101 public function delete(string $cat, string $key)
103 if ($this->config_loaded) {
107 $cacheRemoved = $this->configCache->delete($cat, $key);
109 if (!$this->configModel->isConnected()) {
110 return $cacheRemoved;
113 $storeRemoved = $this->configModel->delete($cat, $key);
115 return $cacheRemoved || $storeRemoved;