cache schema version better

This commit is contained in:
Andrew Dolgov 2021-02-25 21:42:05 +03:00
parent 22ae284db4
commit a1ca62af50
9 changed files with 24 additions and 39 deletions

View File

@ -107,8 +107,9 @@ class Config {
private static $instance; private static $instance;
private $params = []; private $params = [];
private $schema_version = null;
public static function get_instance() { public static function get_instance() : Config {
if (self::$instance == null) if (self::$instance == null)
self::$instance = new self(); self::$instance = new self();
@ -133,6 +134,20 @@ class Config {
} }
} }
static function get_schema_version(bool $nocache = false) {
return self::get_instance()->_schema_version($nocache);
}
function _schema_version(bool $nocache = false) {
if (empty($this->schema_version) || $nocache) {
$row = Db::pdo()->query("SELECT schema_version FROM ttrss_version")->fetch();
$this->schema_version = (int) $row["schema_version"];
}
return $this->schema_version;
}
static function cast_to(string $value, int $type_hint) { static function cast_to(string $value, int $type_hint) {
switch ($type_hint) { switch ($type_hint) {
case self::T_BOOL: case self::T_BOOL:

View File

@ -12,8 +12,7 @@ class DbUpdater {
} }
function get_schema_version() { function get_schema_version() {
$row = $this->pdo->query("SELECT schema_version FROM ttrss_version")->fetch(); return Config::get_schema_version(true);
return (int) $row['schema_version'];
} }
function is_update_required() { function is_update_required() {

View File

@ -334,7 +334,7 @@ class Feeds extends Handler_Protected {
/* we don't need those */ /* we don't need those */
foreach (["date_entered", "guid", "last_published", "last_marked", "tag_cache", "favicon_avg_color", foreach (["date_entered", "guid", "last_published", "last_marked", "tag_cache", "favicon_avg_color",
"uuid", "label_cache", "yyiw"] as $k) "uuid", "label_cache", "yyiw", "num_enclosures"] as $k)
unset($line[$k]); unset($line[$k]);
array_push($reply['content'], $line); array_push($reply['content'], $line);

View File

@ -137,7 +137,7 @@ class Prefs {
/** @var PDO */ /** @var PDO */
private $pdo; private $pdo;
public static function get_instance() { public static function get_instance() : Prefs {
if (self::$instance == null) if (self::$instance == null)
self::$instance = new self(); self::$instance = new self();

View File

@ -53,11 +53,9 @@ class RSSUtils {
} }
static function update_daemon_common($limit = null, $options = []) { static function update_daemon_common($limit = null, $options = []) {
$schema_version = get_schema_version();
if (!$limit) $limit = Config::get(Config::DAEMON_FEED_LIMIT); if (!$limit) $limit = Config::get(Config::DAEMON_FEED_LIMIT);
if ($schema_version != SCHEMA_VERSION) { if (get_schema_version() != SCHEMA_VERSION) {
die("Schema version is wrong, please upgrade the database.\n"); die("Schema version is wrong, please upgrade the database.\n");
} }

View File

@ -166,8 +166,6 @@
define('SELF_USER_AGENT', 'Tiny Tiny RSS/' . get_version() . ' (http://tt-rss.org/)'); define('SELF_USER_AGENT', 'Tiny Tiny RSS/' . get_version() . ' (http://tt-rss.org/)');
ini_set('user_agent', SELF_USER_AGENT); ini_set('user_agent', SELF_USER_AGENT);
$schema_version = false;
/* compat shims */ /* compat shims */
function _debug($msg) { function _debug($msg) {
@ -306,13 +304,7 @@
} }
function get_schema_version() { function get_schema_version() {
$pdo = Db::pdo(); return Config::get_schema_version();
$row = $pdo->query("SELECT schema_version FROM ttrss_version")->fetch();
$version = $row["schema_version"];
$schema_version = $version;
return $version;
} }
function file_is_locked($filename) { function file_is_locked($filename) {

View File

@ -19,25 +19,10 @@
ini_set("session.gc_maxlifetime", $session_expire); ini_set("session.gc_maxlifetime", $session_expire);
ini_set("session.cookie_lifetime", "0"); ini_set("session.cookie_lifetime", "0");
function session_get_schema_version() {
global $schema_version;
if (!$schema_version) {
$row = \Db::pdo()->query("SELECT schema_version FROM ttrss_version")->fetch();
$version = $row["schema_version"];
$schema_version = $version;
return $version;
} else {
return $schema_version;
}
}
function validate_session() { function validate_session() {
if (\Config::get(\Config::SINGLE_USER_MODE)) return true; if (\Config::get(\Config::SINGLE_USER_MODE)) return true;
if (isset($_SESSION["ref_schema_version"]) && $_SESSION["ref_schema_version"] != session_get_schema_version()) { if (isset($_SESSION["ref_schema_version"]) && $_SESSION["ref_schema_version"] != \Config::get_schema_version()) {
$_SESSION["login_error_msg"] = $_SESSION["login_error_msg"] =
__("Session failed to validate (schema version changed)"); __("Session failed to validate (schema version changed)");
return false; return false;

View File

@ -145,9 +145,7 @@
} }
if (!isset($options['update-schema'])) { if (!isset($options['update-schema'])) {
$schema_version = get_schema_version(); if (get_schema_version() != SCHEMA_VERSION) {
if ($schema_version != SCHEMA_VERSION) {
die("Schema version is wrong, please upgrade the database (--update-schema).\n"); die("Schema version is wrong, please upgrade the database (--update-schema).\n");
} }
} }

View File

@ -188,9 +188,7 @@
"Maybe another daemon is already running.\n"); "Maybe another daemon is already running.\n");
} }
$schema_version = get_schema_version(); if (get_schema_version() != SCHEMA_VERSION) {
if ($schema_version != SCHEMA_VERSION) {
die("Schema version is wrong, please upgrade the database.\n"); die("Schema version is wrong, please upgrade the database.\n");
} }