3 namespace Friendica\Core\Config;
8 * This class implements the preload 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 or with invalid uid
37 if (!$uid || !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)
59 return $default_value;
62 if (empty($this->config_loaded[$uid])) {
65 if ($this->configModel->isConnected()) {
66 $config = $this->configModel->get($uid, $cat, $key);
68 $this->configCache->set($uid, $cat, $key, $config);
73 // use the config cache for return
74 $result = $this->configCache->get($uid, $cat, $key);
76 return (isset($result)) ? $result : $default_value;
82 public function set(int $uid, string $cat, string $key, $value)
88 if (empty($this->config_loaded[$uid])) {
92 // set the cache first
93 $cached = $this->configCache->set($uid, $cat, $key, $value);
95 // If there is no connected adapter, we're finished
96 if (!$this->configModel->isConnected()) {
100 $stored = $this->configModel->set($uid, $cat, $key, $value);
102 return $cached && $stored;
108 public function delete(int $uid, string $cat, string $key)
114 if (empty($this->config_loaded[$uid])) {
118 $cacheRemoved = $this->configCache->delete($uid, $cat, $key);
120 if (!$this->configModel->isConnected()) {
121 return $cacheRemoved;
124 $storeRemoved = $this->configModel->delete($uid, $cat, $key);
126 return $cacheRemoved || $storeRemoved;