mirror of
https://github.com/friendica/friendica
synced 2025-04-26 09:10:15 +00:00
Refactor PConfiguration
This commit is contained in:
parent
ff99a62584
commit
9d98a4ce3a
16 changed files with 1084 additions and 690 deletions
|
@ -1,83 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Config\Adapter;
|
||||
|
||||
use Friendica\Database\DBA;
|
||||
|
||||
abstract class AbstractDbaConfigAdapter
|
||||
{
|
||||
/**
|
||||
* The connection state of the adapter
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $connected = true;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->connected = DBA::connected();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the adapter is currently connected
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isConnected()
|
||||
{
|
||||
return $this->connected;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a DB value to a config value
|
||||
* - null = The db-value isn't set
|
||||
* - bool = The db-value is either '0' or '1'
|
||||
* - array = The db-value is a serialized array
|
||||
* - string = The db-value is a string
|
||||
*
|
||||
* Keep in mind that there aren't any numeric/integer config values in the database
|
||||
*
|
||||
* @param null|string $value
|
||||
*
|
||||
* @return null|array|string
|
||||
*/
|
||||
protected function toConfigValue($value)
|
||||
{
|
||||
if (!isset($value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
switch (true) {
|
||||
// manage array value
|
||||
case preg_match("|^a:[0-9]+:{.*}$|s", $value):
|
||||
return unserialize($value);
|
||||
|
||||
default:
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a config value to a DB value (string)
|
||||
*
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function toDbValue($value)
|
||||
{
|
||||
// if not set, save an empty string
|
||||
if (!isset($value)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
switch (true) {
|
||||
// manage arrays
|
||||
case is_array($value):
|
||||
return serialize($value);
|
||||
|
||||
default:
|
||||
return (string)$value;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,85 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Config\Adapter;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author benlo
|
||||
*/
|
||||
interface IPConfigAdapter
|
||||
{
|
||||
/**
|
||||
* Loads all configuration values of a user's config family and returns the loaded category as an array.
|
||||
*
|
||||
* @param string $uid The user_id
|
||||
* @param string $cat The category of the configuration value
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function load($uid, $cat);
|
||||
|
||||
/**
|
||||
* Get a particular user's config variable given the category name
|
||||
* ($family) and a key.
|
||||
*
|
||||
* Note: Boolean variables are defined as 0/1 in the database
|
||||
*
|
||||
* @param string $uid The user_id
|
||||
* @param string $cat The category of the configuration value
|
||||
* @param string $key The configuration key to query
|
||||
*
|
||||
* @return null|mixed Stored value or null if it does not exist
|
||||
*/
|
||||
public function get($uid, $cat, $key);
|
||||
|
||||
/**
|
||||
* Stores a config value ($value) in the category ($family) under the key ($key)
|
||||
* for the user_id $uid.
|
||||
*
|
||||
* @note Please do not store booleans - convert to 0/1 integer values!
|
||||
*
|
||||
* @param string $uid The user_id
|
||||
* @param string $cat The category of the configuration value
|
||||
* @param string $key The configuration key to set
|
||||
* @param string $value The value to store
|
||||
*
|
||||
* @return bool Operation success
|
||||
*/
|
||||
public function set($uid, $cat, $key, $value);
|
||||
|
||||
/**
|
||||
* Removes the configured value from the stored cache
|
||||
* and removes it from the database.
|
||||
*
|
||||
* @param string $uid The user_id
|
||||
* @param string $cat The category of the configuration value
|
||||
* @param string $key The configuration key to delete
|
||||
*
|
||||
* @return bool Operation success
|
||||
*/
|
||||
public function delete($uid, $cat, $key);
|
||||
|
||||
/**
|
||||
* Checks, if the current adapter is connected to the backend
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isConnected();
|
||||
|
||||
/**
|
||||
* Checks, if a config key ($key) in the category ($cat) is already loaded for the user_id $uid.
|
||||
*
|
||||
* @param string $uid The user_id
|
||||
* @param string $cat The configuration category
|
||||
* @param string $key The configuration key
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isLoaded($uid, $cat, $key);
|
||||
}
|
|
@ -1,145 +0,0 @@
|
|||
<?php
|
||||
namespace Friendica\Core\Config\Adapter;
|
||||
|
||||
use Friendica\Database\DBA;
|
||||
|
||||
/**
|
||||
* JustInTime User Configuration Adapter
|
||||
*
|
||||
* Default PConfig Adapter. Provides the best performance for pages loading few configuration variables.
|
||||
*
|
||||
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
||||
*/
|
||||
class JITPConfigAdapter extends AbstractDbaConfigAdapter implements IPConfigAdapter
|
||||
{
|
||||
private $in_db;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function load($uid, $cat)
|
||||
{
|
||||
$return = [];
|
||||
|
||||
if (!$this->isConnected()) {
|
||||
return $return;
|
||||
}
|
||||
|
||||
$pconfigs = DBA::select('pconfig', ['v', 'k'], ['cat' => $cat, 'uid' => $uid]);
|
||||
if (DBA::isResult($pconfigs)) {
|
||||
while ($pconfig = DBA::fetch($pconfigs)) {
|
||||
$key = $pconfig['k'];
|
||||
$value = $this->toConfigValue($pconfig['v']);
|
||||
|
||||
// The value was in the db, so don't check it again (unless you have to)
|
||||
$this->in_db[$uid][$cat][$key] = true;
|
||||
|
||||
if (isset($value)) {
|
||||
$return[$key] = $value;
|
||||
}
|
||||
}
|
||||
} else if ($cat != 'config') {
|
||||
// Negative caching
|
||||
$return = null;
|
||||
}
|
||||
DBA::close($pconfigs);
|
||||
|
||||
return [$cat => $return];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @param bool $mark if true, mark the selection of the current cat/key pair
|
||||
*/
|
||||
public function get($uid, $cat, $key, $mark = true)
|
||||
{
|
||||
if (!$this->isConnected()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// The value was in the db, so don't check it again (unless you have to)
|
||||
if ($mark) {
|
||||
$this->in_db[$uid][$cat][$key] = true;
|
||||
}
|
||||
|
||||
$pconfig = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
|
||||
if (DBA::isResult($pconfig)) {
|
||||
$value = $this->toConfigValue($pconfig['v']);
|
||||
|
||||
if (isset($value)) {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
$this->in_db[$uid][$cat][$key] = false;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function set($uid, $cat, $key, $value)
|
||||
{
|
||||
if (!$this->isConnected()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// We store our setting values in a string variable.
|
||||
// So we have to do the conversion here so that the compare below works.
|
||||
// The exception are array values.
|
||||
$compare_value = (!is_array($value) ? (string)$value : $value);
|
||||
$stored_value = $this->get($uid, $cat, $key, false);
|
||||
|
||||
if (!isset($this->in_db[$uid])) {
|
||||
$this->in_db[$uid] = [];
|
||||
}
|
||||
if (!isset($this->in_db[$uid][$cat])) {
|
||||
$this->in_db[$uid][$cat] = [];
|
||||
}
|
||||
if (!isset($this->in_db[$uid][$cat][$key])) {
|
||||
$this->in_db[$uid][$cat][$key] = false;
|
||||
}
|
||||
|
||||
if (isset($stored_value) && ($stored_value === $compare_value) && $this->in_db[$uid][$cat][$key]) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// manage array value
|
||||
$dbvalue = (is_array($value) ? serialize($value) : $value);
|
||||
|
||||
$result = DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
|
||||
|
||||
$this->in_db[$uid][$cat][$key] = $result;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function delete($uid, $cat, $key)
|
||||
{
|
||||
if (!$this->isConnected()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isset($this->in_db[$uid][$cat][$key])) {
|
||||
unset($this->in_db[$uid][$cat][$key]);
|
||||
}
|
||||
|
||||
return DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isLoaded($uid, $cat, $key)
|
||||
{
|
||||
if (!$this->isConnected()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (isset($this->in_db[$uid][$cat][$key])) && $this->in_db[$uid][$cat][$key];
|
||||
}
|
||||
}
|
|
@ -1,142 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Config\Adapter;
|
||||
|
||||
use Friendica\Database\DBA;
|
||||
|
||||
/**
|
||||
* Preload User Configuration Adapter
|
||||
*
|
||||
* Minimizes the number of database queries to retrieve configuration values at the cost of memory.
|
||||
*
|
||||
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
||||
*/
|
||||
class PreloadPConfigAdapter extends AbstractDbaConfigAdapter implements IPConfigAdapter
|
||||
{
|
||||
/**
|
||||
* @var array true if config for user is loaded
|
||||
*/
|
||||
private $config_loaded;
|
||||
|
||||
/**
|
||||
* @param int $uid The UID of the current user
|
||||
*/
|
||||
public function __construct($uid = null)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->config_loaded = [];
|
||||
|
||||
if (isset($uid)) {
|
||||
$this->load($uid, 'config');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function load($uid, $cat)
|
||||
{
|
||||
$return = [];
|
||||
|
||||
if (empty($uid)) {
|
||||
return $return;
|
||||
}
|
||||
|
||||
if (!$this->isLoaded($uid, $cat, null)) {
|
||||
return $return;
|
||||
}
|
||||
|
||||
$pconfigs = DBA::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
|
||||
while ($pconfig = DBA::fetch($pconfigs)) {
|
||||
$value = $this->toConfigValue($pconfig['v']);
|
||||
if (isset($value)) {
|
||||
$return[$pconfig['cat']][$pconfig['k']] = $value;
|
||||
}
|
||||
}
|
||||
DBA::close($pconfigs);
|
||||
|
||||
$this->config_loaded[$uid] = true;
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function get($uid, $cat, $key)
|
||||
{
|
||||
if (!$this->isConnected()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!$this->isLoaded($uid, $cat, $key)) {
|
||||
$this->load($uid, $cat);
|
||||
}
|
||||
|
||||
$config = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
|
||||
if (DBA::isResult($config)) {
|
||||
$value = $this->toConfigValue($config['v']);
|
||||
|
||||
if (isset($value)) {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function set($uid, $cat, $key, $value)
|
||||
{
|
||||
if (!$this->isConnected()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$this->isLoaded($uid, $cat, $key)) {
|
||||
$this->load($uid, $cat);
|
||||
}
|
||||
// We store our setting values as strings.
|
||||
// So we have to do the conversion here so that the compare below works.
|
||||
// The exception are array values.
|
||||
$compare_value = !is_array($value) ? (string)$value : $value;
|
||||
$stored_value = $this->get($uid, $cat, $key);
|
||||
|
||||
if (isset($stored_value) && $stored_value === $compare_value) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$dbvalue = $this->toDbValue($value);
|
||||
|
||||
return DBA::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function delete($uid, $cat, $key)
|
||||
{
|
||||
if (!$this->isConnected()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$this->isLoaded($uid, $cat, $key)) {
|
||||
$this->load($uid, $cat);
|
||||
}
|
||||
|
||||
return DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isLoaded($uid, $cat, $key)
|
||||
{
|
||||
if (!$this->isConnected()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return isset($this->config_loaded[$uid]) && $this->config_loaded[$uid];
|
||||
}
|
||||
}
|
119
src/Core/Config/JitPConfiguration.php
Normal file
119
src/Core/Config/JitPConfiguration.php
Normal file
|
@ -0,0 +1,119 @@
|
|||
<?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}
|
||||
*
|
||||
*/
|
||||
public function load(int $uid, string $cat = 'config')
|
||||
{
|
||||
// If not connected, do nothing
|
||||
if (!$this->configModel->isConnected()) {
|
||||
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}
|
||||
*/
|
||||
public function get(int $uid, string $cat, string $key, $default_value = null, bool $refresh = false)
|
||||
{
|
||||
// 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}
|
||||
*/
|
||||
public function set(int $uid, string $cat, string $key, $value)
|
||||
{
|
||||
// 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}
|
||||
*/
|
||||
public function delete(int $uid, string $cat, string $key)
|
||||
{
|
||||
$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;
|
||||
}
|
||||
}
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
namespace Friendica\Core\Config;
|
||||
|
||||
use Friendica\Model;
|
||||
|
||||
/**
|
||||
* This class is responsible for the user-specific configuration values in Friendica
|
||||
* The values are set through the Config-DB-Table (per Config-DB-adapter @see Adapter\IPConfigAdapter )
|
||||
|
@ -9,138 +11,101 @@ namespace Friendica\Core\Config;
|
|||
* The configuration cache (@see Cache\PConfigCache ) is used for temporary caching of database calls. This will
|
||||
* increase the performance.
|
||||
*/
|
||||
class PConfiguration
|
||||
abstract class PConfiguration
|
||||
{
|
||||
/**
|
||||
* @var Cache\PConfigCache
|
||||
*/
|
||||
private $configCache;
|
||||
protected $configCache;
|
||||
|
||||
/**
|
||||
* @var Adapter\IPConfigAdapter
|
||||
* @var Model\Config\PConfig
|
||||
*/
|
||||
private $configAdapter;
|
||||
protected $configModel;
|
||||
|
||||
/**
|
||||
* @param Cache\PConfigCache $configCache The configuration cache
|
||||
* @param Adapter\IPConfigAdapter $configAdapter The configuration DB-backend
|
||||
* @param Cache\PConfigCache $configCache The configuration cache
|
||||
* @param Model\Config\PConfig $configModel The configuration model
|
||||
*/
|
||||
public function __construct(Cache\PConfigCache $configCache, Adapter\IPConfigAdapter $configAdapter)
|
||||
public function __construct(Cache\PConfigCache $configCache, Model\Config\PConfig $configModel)
|
||||
{
|
||||
$this->configCache = $configCache;
|
||||
$this->configAdapter = $configAdapter;
|
||||
$this->configModel = $configModel;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Loads all configuration values of a user's config family into a cached storage.
|
||||
* Returns the Config Cache
|
||||
*
|
||||
* @return Cache\PConfigCache
|
||||
*/
|
||||
public function getCache()
|
||||
{
|
||||
return $this->configCache;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads all configuration values of a user's config family into a cached storage.
|
||||
*
|
||||
* All configuration values of the given user are stored with the $uid in
|
||||
* the cache ( @see PConfigCache )
|
||||
* the cache ( @param int $uid The user_id
|
||||
*
|
||||
* @param string $uid The user_id
|
||||
* @param string $cat The category of the configuration value
|
||||
*
|
||||
* @return void
|
||||
* @see PConfigCache )
|
||||
*
|
||||
*/
|
||||
public function load($uid, $cat = 'config')
|
||||
{
|
||||
// If not connected, do nothing
|
||||
if (!$this->configAdapter->isConnected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// load the whole category out of the DB into the cache
|
||||
$this->configCache->load($uid, $this->configAdapter->load($uid, $cat));
|
||||
}
|
||||
abstract public function load(int $uid, string $cat = 'config');
|
||||
|
||||
/**
|
||||
* @brief Get a particular user's config variable given the category name
|
||||
* Get a particular user's config variable given the category name
|
||||
* ($cat) and a key.
|
||||
*
|
||||
* Get a particular user's config value from the given category ($cat)
|
||||
* and the $key with the $uid from a cached storage either from the $this->configAdapter
|
||||
* (@see IConfigAdapter ) or from the $this->configCache (@see PConfigCache ).
|
||||
* (@param int $uid The user_id
|
||||
*
|
||||
* @param string $uid The user_id
|
||||
* @param string $cat The category of the configuration value
|
||||
* @param string $key The configuration key to query
|
||||
* @param mixed $default_value optional, The value to return if key is not set (default: null)
|
||||
* @param boolean $refresh optional, If true the config is loaded from the db and not from the cache (default: false)
|
||||
*
|
||||
* @return mixed Stored value or null if it does not exist
|
||||
* @see IConfigAdapter ) or from the $this->configCache (@see PConfigCache ).
|
||||
*
|
||||
*/
|
||||
public function get($uid, $cat, $key, $default_value = null, $refresh = false)
|
||||
{
|
||||
// if the value isn't loaded or refresh is needed, load it to the cache
|
||||
if ($this->configAdapter->isConnected() &&
|
||||
(!$this->configAdapter->isLoaded($uid, $cat, $key) ||
|
||||
$refresh)) {
|
||||
$dbValue = $this->configAdapter->get($uid, $cat, $key);
|
||||
|
||||
if (isset($dbValue)) {
|
||||
$this->configCache->set($uid, $cat, $key, $dbValue);
|
||||
return $dbValue;
|
||||
}
|
||||
}
|
||||
|
||||
// use the config cache for return
|
||||
$result = $this->configCache->get($uid, $cat, $key);
|
||||
return (isset($result)) ? $result : $default_value;
|
||||
}
|
||||
abstract public function get(int $uid, string $cat, string $key, $default_value = null, bool $refresh = false);
|
||||
|
||||
/**
|
||||
* @brief Sets a configuration value for a user
|
||||
* Sets a configuration value for a user
|
||||
*
|
||||
* Stores a config value ($value) in the category ($family) under the key ($key)
|
||||
* for the user_id $uid.
|
||||
*
|
||||
* @note Please do not store booleans - convert to 0/1 integer values!
|
||||
*
|
||||
* @param string $uid The user_id
|
||||
* @param string $cat The category of the configuration value
|
||||
* @param string $key The configuration key to set
|
||||
* @param mixed $value The value to store
|
||||
* @param int $uid The user_id
|
||||
* @param string $cat The category of the configuration value
|
||||
* @param string $key The configuration key to set
|
||||
* @param mixed $value The value to store
|
||||
*
|
||||
* @return bool Operation success
|
||||
*/
|
||||
public function set($uid, $cat, $key, $value)
|
||||
{
|
||||
// set the cache first
|
||||
$cached = $this->configCache->set($uid, $cat, $key, $value);
|
||||
|
||||
// If there is no connected adapter, we're finished
|
||||
if (!$this->configAdapter->isConnected()) {
|
||||
return $cached;
|
||||
}
|
||||
|
||||
$stored = $this->configAdapter->set($uid, $cat, $key, $value);
|
||||
|
||||
return $cached && $stored;
|
||||
}
|
||||
abstract public function set(int $uid, string $cat, string $key, $value);
|
||||
|
||||
/**
|
||||
* @brief Deletes the given key from the users's configuration.
|
||||
* Deletes the given key from the users's configuration.
|
||||
*
|
||||
* Removes the configured value from the stored cache in $this->configCache
|
||||
* (@see ConfigCache ) and removes it from the database (@see IConfigAdapter )
|
||||
* with the given $uid.
|
||||
* (@param int $uid The user_id
|
||||
*
|
||||
* @param string $uid The user_id
|
||||
* @param string $cat The category of the configuration value
|
||||
* @param string $key The configuration key to delete
|
||||
*
|
||||
* @return bool
|
||||
* @see ConfigCache ) and removes it from the database (@see IConfigAdapter )
|
||||
* with the given $uid.
|
||||
*
|
||||
*/
|
||||
public function delete($uid, $cat, $key)
|
||||
{
|
||||
$cacheRemoved = $this->configCache->delete($uid, $cat, $key);
|
||||
|
||||
if (!$this->configAdapter->isConnected()) {
|
||||
return $cacheRemoved;
|
||||
}
|
||||
|
||||
$storeRemoved = $this->configAdapter->delete($uid, $cat, $key);
|
||||
|
||||
return $cacheRemoved || $storeRemoved;
|
||||
}
|
||||
abstract public function delete(int $uid, string $cat, string $key);
|
||||
}
|
||||
|
|
116
src/Core/Config/PreloadPConfiguration.php
Normal file
116
src/Core/Config/PreloadPConfiguration.php
Normal file
|
@ -0,0 +1,116 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Config;
|
||||
|
||||
use Friendica\Model;
|
||||
|
||||
/**
|
||||
* This class implements the preload Time configuration, which will cache
|
||||
* all user config values per call in a cache.
|
||||
*
|
||||
* Minimizes the number of database queries to retrieve configuration values at the cost of memory.
|
||||
*/
|
||||
class PreloadPConfiguration extends PConfiguration
|
||||
{
|
||||
/** @var array */
|
||||
private $config_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->config_loaded = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* This loads all config values everytime load is called
|
||||
*
|
||||
*/
|
||||
public function load(int $uid, string $cat = 'config')
|
||||
{
|
||||
// Don't load the whole configuration twice
|
||||
if (!empty($this->config_loaded[$uid])) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If not connected, do nothing
|
||||
if (!$this->configModel->isConnected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$config = $this->configModel->load($uid);
|
||||
$this->config_loaded[$uid] = true;
|
||||
|
||||
// load the whole category out of the DB into the cache
|
||||
$this->configCache->load($uid, $config);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function get(int $uid, string $cat, string $key, $default_value = null, bool $refresh = false)
|
||||
{
|
||||
if (empty($this->config_loaded[$uid])) {
|
||||
$this->load($uid);
|
||||
} elseif ($refresh) {
|
||||
if ($this->configModel->isConnected()) {
|
||||
$config = $this->configModel->get($uid, $cat, $key);
|
||||
if (isset($config)) {
|
||||
$this->configCache->set($uid, $cat, $key, $config);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// use the config cache for return
|
||||
$result = $this->configCache->get($uid, $cat, $key);
|
||||
|
||||
return (isset($result)) ? $result : $default_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function set(int $uid, string $cat, string $key, $value)
|
||||
{
|
||||
if (empty($this->config_loaded[$uid])) {
|
||||
$this->load($uid);
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
return $cached && $stored;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function delete(int $uid, string $cat, string $key)
|
||||
{
|
||||
if (empty($this->config_loaded[$uid])) {
|
||||
$this->load($uid);
|
||||
}
|
||||
|
||||
$cacheRemoved = $this->configCache->delete($uid, $cat, $key);
|
||||
|
||||
if (!$this->configModel->isConnected()) {
|
||||
return $cacheRemoved;
|
||||
}
|
||||
|
||||
$storeRemoved = $this->configModel->delete($uid, $cat, $key);
|
||||
|
||||
return $cacheRemoved || $storeRemoved;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue