friendica-github/src/Core/Config/JitPConfiguration.php

132 lines
3 KiB
PHP
Raw Normal View History

2019-07-15 18:13:53 +00:00
<?php
namespace Friendica\Core\Config;
use Friendica\Model;
/**
* This class implements the Just-In-Time configuration, which will cache
* user config values in a cache, once they are retrieved.
*
* Default Configuration type.
* Provides the best performance for pages loading few configuration variables.
*/
class JitPConfiguration extends PConfiguration
{
/**
* @var array Array of already loaded db values (even if there was no value)
*/
private $db_loaded;
/**
* @param Cache\PConfigCache $configCache The configuration cache
* @param Model\Config\PConfig $configModel The configuration model
*/
public function __construct(Cache\PConfigCache $configCache, Model\Config\PConfig $configModel)
{
parent::__construct($configCache, $configModel);
$this->db_loaded = [];
}
/**
* {@inheritDoc}
*
*/
2019-07-15 19:11:38 +00:00
public function load($uid, string $cat = 'config')
2019-07-15 18:13:53 +00:00
{
2019-07-15 19:11:38 +00:00
// If not connected or no uid, do nothing
if (!is_int($uid) || !$this->configModel->isConnected()) {
2019-07-15 18:13:53 +00:00
return;
}
$config = $this->configModel->load($uid, $cat);
if (!empty($config[$cat])) {
foreach ($config[$cat] as $key => $value) {
$this->db_loaded[$uid][$cat][$key] = true;
}
}
// load the whole category out of the DB into the cache
$this->configCache->load($uid, $config);
}
/**
* {@inheritDoc}
*/
2019-07-15 19:11:38 +00:00
public function get($uid, string $cat, string $key, $default_value = null, bool $refresh = false)
2019-07-15 18:13:53 +00:00
{
2019-07-15 19:11:38 +00:00
if (!is_int($uid)) {
return $default_value;
}
2019-07-15 18:13:53 +00:00
// if the value isn't loaded or refresh is needed, load it to the cache
if ($this->configModel->isConnected() &&
(empty($this->db_loaded[$uid][$cat][$key]) ||
$refresh)) {
$dbvalue = $this->configModel->get($uid, $cat, $key);
if (isset($dbvalue)) {
$this->configCache->set($uid, $cat, $key, $dbvalue);
unset($dbvalue);
}
$this->db_loaded[$uid][$cat][$key] = true;
}
// use the config cache for return
$result = $this->configCache->get($uid, $cat, $key);
return (isset($result)) ? $result : $default_value;
}
/**
* {@inheritDoc}
*/
2019-07-15 19:11:38 +00:00
public function set($uid, string $cat, string $key, $value)
2019-07-15 18:13:53 +00:00
{
2019-07-15 19:11:38 +00:00
if (!is_int($uid)) {
return false;
}
2019-07-15 18:13:53 +00:00
// set the cache first
$cached = $this->configCache->set($uid, $cat, $key, $value);
// If there is no connected adapter, we're finished
if (!$this->configModel->isConnected()) {
return $cached;
}
$stored = $this->configModel->set($uid, $cat, $key, $value);
$this->db_loaded[$uid][$cat][$key] = $stored;
return $cached && $stored;
}
/**
* {@inheritDoc}
*/
2019-07-15 19:11:38 +00:00
public function delete($uid, string $cat, string $key)
2019-07-15 18:13:53 +00:00
{
2019-07-15 19:11:38 +00:00
if (!is_int($uid)) {
return false;
}
2019-07-15 18:13:53 +00:00
$cacheRemoved = $this->configCache->delete($uid, $cat, $key);
if (isset($this->db_loaded[$uid][$cat][$key])) {
unset($this->db_loaded[$uid][$cat][$key]);
}
if (!$this->configModel->isConnected()) {
return $cacheRemoved;
}
$storeRemoved = $this->configModel->delete($uid, $cat, $key);
return $cacheRemoved || $storeRemoved;
}
}