3 namespace Friendica\Core\Config;
8 * This class implements the preload Time configuration, which will cache
9 * all user 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 PreloadPConfiguration extends PConfiguration
16 private $config_loaded;
19 * @param Cache\PConfigCache $configCache The configuration cache
20 * @param Model\Config\PConfig $configModel The configuration model
22 public function __construct(Cache\PConfigCache $configCache, Model\Config\PConfig $configModel)
24 parent::__construct($configCache, $configModel);
25 $this->config_loaded = [];
31 * This loads all config values everytime load is called
34 public function load(int $uid, string $cat = 'config')
36 // Don't load the whole configuration twice
37 if (!empty($this->config_loaded[$uid])) {
41 // If not connected, do nothing
42 if (!$this->configModel->isConnected()) {
46 $config = $this->configModel->load($uid);
47 $this->config_loaded[$uid] = true;
49 // load the whole category out of the DB into the cache
50 $this->configCache->load($uid, $config);
56 public function get(int $uid, string $cat, string $key, $default_value = null, bool $refresh = false)
58 if (empty($this->config_loaded[$uid])) {
61 if ($this->configModel->isConnected()) {
62 $config = $this->configModel->get($uid, $cat, $key);
64 $this->configCache->set($uid, $cat, $key, $config);
69 // use the config cache for return
70 $result = $this->configCache->get($uid, $cat, $key);
72 return (isset($result)) ? $result : $default_value;
78 public function set(int $uid, string $cat, string $key, $value)
80 if (empty($this->config_loaded[$uid])) {
84 // set the cache first
85 $cached = $this->configCache->set($uid, $cat, $key, $value);
87 // If there is no connected adapter, we're finished
88 if (!$this->configModel->isConnected()) {
92 $stored = $this->configModel->set($uid, $cat, $key, $value);
94 return $cached && $stored;
100 public function delete(int $uid, string $cat, string $key)
102 if (empty($this->config_loaded[$uid])) {
106 $cacheRemoved = $this->configCache->delete($uid, $cat, $key);
108 if (!$this->configModel->isConnected()) {
109 return $cacheRemoved;
112 $storeRemoved = $this->configModel->delete($uid, $cat, $key);
114 return $cacheRemoved || $storeRemoved;