From 0548099f6cb89ccb1c798d092b298b3a03fd8d88 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 18 Jan 2017 21:45:32 +0000 Subject: [PATCH] The config class now makes less database reads. --- boot.php | 2 +- database.sql | 29 +++++++-------- include/Core/Config.php | 64 +++++++++++++++++++++------------- include/cli_startup.php | 17 ++++----- include/create_shadowentry.php | 6 ++-- include/cron.php | 5 +-- include/cronhooks.php | 6 ++-- include/cronjobs.php | 5 +-- include/dbclean.php | 3 +- include/dbstructure.php | 5 +-- include/dbupdate.php | 5 +-- include/delivery.php | 6 ++-- include/directory.php | 8 ++--- include/discover_poco.php | 6 ++-- include/expire.php | 15 ++++---- include/gprobe.php | 5 +-- include/notifier.php | 7 ++-- include/onepoll.php | 5 +-- include/pubsubpublish.php | 3 +- include/queue.php | 6 ++-- include/remove_contact.php | 6 ++-- include/shadowupdate.php | 6 ++-- include/spool_post.php | 6 ++-- include/tagupdate.php | 6 ++-- include/threadupdate.php | 6 ++-- include/update_gcontact.php | 5 +-- index.php | 5 +-- mod/friendica.php | 4 ++- util/maintenance.php | 8 +++-- 29 files changed, 153 insertions(+), 107 deletions(-) diff --git a/boot.php b/boot.php index d598ef8666..501ae38c42 100644 --- a/boot.php +++ b/boot.php @@ -1569,7 +1569,7 @@ function update_db(App $a) { $stored = intval($build); $current = intval(DB_UPDATE_VERSION); if($stored < $current) { - load_config('database'); + Config::load('database'); // We're reporting a different version than what is currently installed. // Run any existing update scripts to bring the database up to current. diff --git a/database.sql b/database.sql index a12f9d2046..383253fcb1 100644 --- a/database.sql +++ b/database.sql @@ -9,13 +9,14 @@ -- CREATE TABLE IF NOT EXISTS `addon` ( `id` int(11) NOT NULL auto_increment, - `name` varchar(255) NOT NULL DEFAULT '', + `name` varchar(190) NOT NULL DEFAULT '', `version` varchar(255) NOT NULL DEFAULT '', `installed` tinyint(1) NOT NULL DEFAULT 0, `hidden` tinyint(1) NOT NULL DEFAULT 0, `timestamp` bigint(20) NOT NULL DEFAULT 0, `plugin_admin` tinyint(1) NOT NULL DEFAULT 0, - PRIMARY KEY(`id`) + PRIMARY KEY(`id`), + UNIQUE INDEX `name` (`name`) ) DEFAULT CHARSET=utf8mb4; -- @@ -32,9 +33,9 @@ CREATE TABLE IF NOT EXISTS `attach` ( `created` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `edited` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `allow_cid` mediumtext, - `allow_gid` medium_text, - `deny_cid` medium_text, - `deny_gid` medium_text, + `allow_gid` mediumtext, + `deny_cid` mediumtext, + `deny_gid` mediumtext, PRIMARY KEY(`id`) ) DEFAULT CHARSET=utf8mb4; @@ -235,10 +236,10 @@ CREATE TABLE IF NOT EXISTS `event` ( `nofinish` tinyint(1) NOT NULL DEFAULT 0, `adjust` tinyint(1) NOT NULL DEFAULT 1, `ignore` tinyint(1) unsigned NOT NULL DEFAULT 0, - `allow_cid` medium_text, - `allow_gid` medium_text, - `deny_cid` medium_text, - `deny_gid` medium_text, + `allow_cid` mediumtext, + `allow_gid` mediumtext, + `deny_cid` mediumtext, + `deny_gid` mediumtext, PRIMARY KEY(`id`), INDEX `uid_start` (`uid`,`start`) ) DEFAULT CHARSET=utf8mb4; @@ -434,7 +435,7 @@ CREATE TABLE IF NOT EXISTS `hook` ( `function` varchar(255) NOT NULL DEFAULT '', `priority` int(11) unsigned NOT NULL DEFAULT 0, PRIMARY KEY(`id`), - INDEX `hook_file_function` (`hook`(30),`file`(60),`function`(30)) + UNIQUE INDEX `hook_file_function` (`hook`(50),`file`(80),`function`(60)) ) DEFAULT CHARSET=utf8mb4; -- @@ -1073,10 +1074,10 @@ CREATE TABLE IF NOT EXISTS `user` ( `expire_notification_sent` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `service_class` varchar(32) NOT NULL DEFAULT '', `def_gid` int(11) NOT NULL DEFAULT 0, - `allow_cid` medium_text, - `allow_gid` medium_text, - `deny_cid` medium_text, - `deny_gid` medium_text, + `allow_cid` mediumtext, + `allow_gid` mediumtext, + `deny_cid` mediumtext, + `deny_gid` mediumtext, `openidserver` text, PRIMARY KEY(`uid`), INDEX `nickname` (`nickname`(32)) diff --git a/include/Core/Config.php b/include/Core/Config.php index 7b7045a9ee..574ff5b8a5 100644 --- a/include/Core/Config.php +++ b/include/Core/Config.php @@ -22,6 +22,8 @@ use dbm; */ class Config { + private static $cache; + /** * @brief Loads all configuration values of family into a cached storage. * @@ -32,10 +34,17 @@ class Config { * The category of the configuration value * @return void */ - public static function load($family) { + public static function load($family = "config") { + + // We don't preload "system" anymore. + // This reduces the number of database reads a lot. + if ($family == 'system') { + return; + } + $a = get_app(); - $r = q("SELECT `v`, `k` FROM `config` WHERE `cat` = '%s' ORDER BY `cat`, `k`, `id`", dbesc($family)); + $r = q("SELECT `v`, `k` FROM `config` WHERE `cat` = '%s'", dbesc($family)); if (dbm::is_result($r)) { foreach ($r as $rr) { $k = $rr['k']; @@ -43,11 +52,9 @@ class Config { $a->config[$k] = $rr['v']; } else { $a->config[$family][$k] = $rr['v']; + self::$cache[$family][$k] = $rr['v']; } } - } else if ($family != 'config') { - // Negative caching - $a->config[$family] = "!!"; } } @@ -78,34 +85,38 @@ class Config { $a = get_app(); if (!$refresh) { - // Looking if the whole family isn't set - if (isset($a->config[$family])) { - if ($a->config[$family] === '!!') { - return $default_value; - } - } - if (isset($a->config[$family][$key])) { - if ($a->config[$family][$key] === '!!') { + // Do we have the cached value? Then return it + if (isset(self::$cache[$family][$key])) { + if (self::$cache[$family][$key] == '!!') { return $default_value; + } else { + return self::$cache[$family][$key]; } - return $a->config[$family][$key]; } } - $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' ORDER BY `id` DESC LIMIT 1", + $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s'", dbesc($family), dbesc($key) ); - if (count($ret)) { + if (dbm::is_result($ret)) { // manage array value $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']); - $a->config[$family][$key] = $val; + // Assign the value from the database to the cache + self::$cache[$family][$key] = $val; return $val; - } else { - $a->config[$family][$key] = '!!'; + } elseif (isset($a->config[$family][$key])) { + + // Assign the value (mostly) from the .htconfig.php to the cache + self::$cache[$family][$key] = $a->config[$family][$key]; + + return $a->config[$family][$key]; } + + self::$cache[$family][$key] = '!!'; + return $default_value; } @@ -134,7 +145,14 @@ class Config { return true; } - $a->config[$family][$key] = $value; + if ($family === 'config') { + $a->config[$key] = $value; + } elseif ($family != 'system') { + $a->config[$family][$key] = $value; + } + + // Assign the just added value to the cache + self::$cache[$family][$key] = $value; // manage array value $dbvalue = (is_array($value) ? serialize($value) : $value); @@ -174,9 +192,8 @@ class Config { */ public static function delete($family, $key) { - $a = get_app(); - if (x($a->config[$family],$key)) { - unset($a->config[$family][$key]); + if (isset(self::$cache[$family][$key])) { + unset(self::$cache[$family][$key]); } $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s'", dbesc($family), @@ -185,5 +202,4 @@ class Config { return $ret; } - } diff --git a/include/cli_startup.php b/include/cli_startup.php index 4cb86adef8..4b66b47a00 100644 --- a/include/cli_startup.php +++ b/include/cli_startup.php @@ -1,5 +1,7 @@ set_baseurl(get_config('system','url')); diff --git a/include/create_shadowentry.php b/include/create_shadowentry.php index f06a0dd1bf..005295c978 100644 --- a/include/create_shadowentry.php +++ b/include/create_shadowentry.php @@ -5,6 +5,9 @@ * * This script is started from mod/item.php to save some time when doing a post. */ + +use \Friendica\Core\Config; + require_once("boot.php"); require_once("include/threads.php"); @@ -21,8 +24,7 @@ function create_shadowentry_run($argv, $argc) { unset($db_host, $db_user, $db_pass, $db_data); } - load_config('config'); - load_config('system'); + Config::load(); if ($argc != 2) { return; diff --git a/include/cron.php b/include/cron.php index f7def61215..2fc8de51c5 100644 --- a/include/cron.php +++ b/include/cron.php @@ -10,6 +10,8 @@ if (!file_exists("boot.php") AND (sizeof($_SERVER["argv"]) != 0)) { chdir($directory); } +use \Friendica\Core\Config; + require_once("boot.php"); require_once("include/photos.php"); require_once("include/user.php"); @@ -38,8 +40,7 @@ function cron_run(&$argv, &$argc){ require_once('mod/nodeinfo.php'); require_once('include/post_update.php'); - load_config('config'); - load_config('system'); + Config::load(); // Don't check this stuff if the function is called by the poller if (App::callstack() != "poller_run") { diff --git a/include/cronhooks.php b/include/cronhooks.php index 7524a0c3a8..72b86be427 100644 --- a/include/cronhooks.php +++ b/include/cronhooks.php @@ -1,7 +1,8 @@ set_baseurl(get_config('system','url')); diff --git a/include/dbclean.php b/include/dbclean.php index c80e5a3be3..8408ad1882 100644 --- a/include/dbclean.php +++ b/include/dbclean.php @@ -23,8 +23,7 @@ function dbclean_run(&$argv, &$argc) { unset($db_host, $db_user, $db_pass, $db_data); } - Config::load('config'); - Config::load('system'); + Config::load(); if (!Config::get('system', 'dbclean', false)) { return; diff --git a/include/dbstructure.php b/include/dbstructure.php index 283d39d22c..0fe157f329 100644 --- a/include/dbstructure.php +++ b/include/dbstructure.php @@ -495,7 +495,7 @@ function db_definition($charset) { $database["addon"] = array( "fields" => array( "id" => array("type" => "int(11)", "not null" => "1", "extra" => "auto_increment", "primary" => "1"), - "name" => array("type" => "varchar(255)", "not null" => "1", "default" => ""), + "name" => array("type" => "varchar(190)", "not null" => "1", "default" => ""), "version" => array("type" => "varchar(255)", "not null" => "1", "default" => ""), "installed" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"), "hidden" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"), @@ -504,6 +504,7 @@ function db_definition($charset) { ), "indexes" => array( "PRIMARY" => array("id"), + "name" => array("UNIQUE", "name"), ) ); $database["attach"] = array( @@ -922,7 +923,7 @@ function db_definition($charset) { ), "indexes" => array( "PRIMARY" => array("id"), - "hook_file_function" => array("hook(30)","file(60)","function(30)"), + "hook_file_function" => array("UNIQUE", "hook(50)","file(80)","function(60)"), ) ); $database["intro"] = array( diff --git a/include/dbupdate.php b/include/dbupdate.php index 28f1de340b..3583be3106 100644 --- a/include/dbupdate.php +++ b/include/dbupdate.php @@ -1,5 +1,7 @@ set_baseurl(get_config('system','url')); diff --git a/include/gprobe.php b/include/gprobe.php index 7169aada3f..4407fa6d6c 100644 --- a/include/gprobe.php +++ b/include/gprobe.php @@ -1,5 +1,7 @@ set_baseurl(get_config('system','url')); diff --git a/include/notifier.php b/include/notifier.php index 7bea239c6f..24830a11ab 100644 --- a/include/notifier.php +++ b/include/notifier.php @@ -1,4 +1,7 @@ set_baseurl(get_config('system','url')); diff --git a/include/pubsubpublish.php b/include/pubsubpublish.php index 6bd90bfc21..428103a971 100644 --- a/include/pubsubpublish.php +++ b/include/pubsubpublish.php @@ -72,8 +72,7 @@ function pubsubpublish_run(&$argv, &$argc){ require_once('include/items.php'); - load_config('config'); - load_config('system'); + Config::load(); // Don't check this stuff if the function is called by the poller if (App::callstack() != "poller_run") { diff --git a/include/queue.php b/include/queue.php index f36e7723cd..bcd32985db 100644 --- a/include/queue.php +++ b/include/queue.php @@ -1,4 +1,7 @@ set_baseurl(get_config('system','url')); diff --git a/index.php b/index.php index f05151757b..7408f495cd 100644 --- a/index.php +++ b/index.php @@ -13,6 +13,8 @@ * */ +use \Friendica\Core\Config; + require_once('boot.php'); require_once('object/BaseObject.php'); @@ -54,8 +56,7 @@ if(!$install) { * Load configs from db. Overwrite configs from .htconfig.php */ - load_config('config'); - load_config('system'); + Config::load(); if ($a->max_processes_reached() OR $a->maxload_reached()) { header($_SERVER["SERVER_PROTOCOL"].' 503 Service Temporarily Unavailable'); diff --git a/mod/friendica.php b/mod/friendica.php index 3f242f7c56..f613dfd39c 100644 --- a/mod/friendica.php +++ b/mod/friendica.php @@ -1,5 +1,7 @@ argv[1]=="json"){ $register_policy = Array('REGISTER_CLOSED', 'REGISTER_APPROVE', 'REGISTER_OPEN'); @@ -29,7 +31,7 @@ function friendica_init(App $a) { $visible_plugins[] = $rr['name']; } - load_config('feature_lock'); + Config::load('feature_lock'); $locked_features = array(); if(is_array($a->config['feature_lock']) && count($a->config['feature_lock'])) { foreach($a->config['feature_lock'] as $k => $v) { diff --git a/util/maintenance.php b/util/maintenance.php index d1ff94524e..28f3a503ad 100644 --- a/util/maintenance.php +++ b/util/maintenance.php @@ -1,5 +1,7 @@ 1)