aboutsummaryrefslogtreecommitdiff
path: root/app/models/Feed.php
diff options
context:
space:
mode:
authorGravatar Alexandre Alapetite <alexandre@alapetite.fr> 2013-11-16 21:03:25 +0100
committerGravatar Alexandre Alapetite <alexandre@alapetite.fr> 2013-11-16 21:03:25 +0100
commit32306a78d2e53bbbc864f3eabda9a2f1a3dd2322 (patch)
tree8d3ee43aa98981ebeada93d31930c1b91fae9395 /app/models/Feed.php
parent770ea63258d9cb138e86d7545b2bccc046666f95 (diff)
SQL : grosse mise à jour avec mise en cache du nombre d'articles lus/non-lus
* Mise en cache du nombre d'articles lus et non-lus par flux, via `f.cache_nbEntries, f.cache_nbUnreads` pour de biens meilleures performances * Implémente https://github.com/marienfressinaud/FreshRSS/issues/268 * Révision de la plupart des requêtes de modification en conséquence * En cas d'affichage `not_read`, évite de faire une requête si on sait déjà qu'il n'y a pas d'article non lu et fait directement un affichage `all`. * Appelle `cleanOldEntries` seulement une fois de temps en temps aléatoirement (1 fois sur 30 actuellement) pour économiser les ressources, et avant les insertions pour plus de robustesse. * Utilisation des transactions lors de mises à jour multiples et liées * Lors de requêtes de modifications, retourne le nombre de lignes impactées plutôt qu'un booléen en cas de succès * Suppression de code oublié relatif à is_public qui n'est plus utilisé
Diffstat (limited to 'app/models/Feed.php')
-rw-r--r--app/models/Feed.php84
1 files changed, 41 insertions, 43 deletions
diff --git a/app/models/Feed.php b/app/models/Feed.php
index 407614f9f..ae3a9af83 100644
--- a/app/models/Feed.php
+++ b/app/models/Feed.php
@@ -337,7 +337,7 @@ class FeedDAO extends Model_pdo {
);
if ($stm && $stm->execute ($values)) {
- return true;
+ return $stm->rowCount();
} else {
$info = $stm->errorInfo();
Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
@@ -365,7 +365,7 @@ class FeedDAO extends Model_pdo {
$values[] = $id;
if ($stm && $stm->execute ($values)) {
- return true;
+ return $stm->rowCount();
} else {
$info = $stm->errorInfo();
Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
@@ -373,34 +373,23 @@ class FeedDAO extends Model_pdo {
}
}
- public function updateLastUpdate ($id) {
- $sql = 'UPDATE ' . $this->prefix . 'feed SET lastUpdate=?, error=0 WHERE id=?';
- $stm = $this->bd->prepare ($sql);
-
- $values = array (
- time (),
- $id
- );
+ public function updateLastUpdate ($id, $inError = 0) {
+ $sql = 'UPDATE ' . $this->prefix . 'feed f ' //2 sub-requests with FOREIGN KEY(e.id_feed), INDEX(e.is_read) faster than 1 request with GROUP BY or CASE
+ . 'SET f.cache_nbEntries=(SELECT COUNT(e1.id) FROM ' . $this->prefix . 'entry e1 WHERE e1.id_feed=f.id),'
+ . 'f.cache_nbUnreads=(SELECT COUNT(e2.id) FROM ' . $this->prefix . 'entry e2 WHERE e2.id_feed=f.id AND e2.is_read=0),'
+ . 'lastUpdate=?, error=? '
+ . 'WHERE f.id=?';
- if ($stm && $stm->execute ($values)) {
- return true;
- } else {
- $info = $stm->errorInfo();
- Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
- return false;
- }
- }
-
- public function isInError ($id) {
- $sql = 'UPDATE ' . $this->prefix . 'feed SET error=1 WHERE id=?';
$stm = $this->bd->prepare ($sql);
$values = array (
- $id
+ time (),
+ $inError,
+ $id,
);
if ($stm && $stm->execute ($values)) {
- return true;
+ return $stm->rowCount();
} else {
$info = $stm->errorInfo();
Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
@@ -424,7 +413,7 @@ class FeedDAO extends Model_pdo {
);
if ($stm && $stm->execute ($values)) {
- return true;
+ return $stm->rowCount();
} else {
$info = $stm->errorInfo();
Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
@@ -439,7 +428,7 @@ class FeedDAO extends Model_pdo {
$values = array ($id);
if ($stm && $stm->execute ($values)) {
- return true;
+ return $stm->rowCount();
} else {
$info = $stm->errorInfo();
Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
@@ -453,7 +442,7 @@ class FeedDAO extends Model_pdo {
$values = array ($id);
if ($stm && $stm->execute ($values)) {
- return true;
+ return $stm->rowCount();
} else {
$info = $stm->errorInfo();
Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
@@ -521,15 +510,6 @@ class FeedDAO extends Model_pdo {
return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
}
- public function count () { //Is this used?
- $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'feed';
- $stm = $this->bd->prepare ($sql);
- $stm->execute ();
- $res = $stm->fetchAll (PDO::FETCH_ASSOC);
-
- return $res[0]['count'];
- }
-
public function countEntries ($id) {
$sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'entry WHERE id_feed=?';
$stm = $this->bd->prepare ($sql);
@@ -539,8 +519,8 @@ class FeedDAO extends Model_pdo {
return $res[0]['count'];
}
- public function countNotRead ($id) { //Is this used?
- $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'entry WHERE is_read=0 AND id_feed=?';
+ public function countNotRead ($id) {
+ $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'entry WHERE id_feed=? AND is_read=0';
$stm = $this->bd->prepare ($sql);
$values = array ($id);
$stm->execute ($values);
@@ -548,6 +528,28 @@ class FeedDAO extends Model_pdo {
return $res[0]['count'];
}
+ public function updateCachedValues () { //For one single feed, call updateLastUpdate($id)
+ $sql = 'UPDATE freshrss_feed f '
+ . 'INNER JOIN ('
+ . 'SELECT e.id_feed, '
+ . 'COUNT(CASE WHEN e.is_read = 0 THEN 1 END) AS nbUnreads, '
+ . 'COUNT(e.id) AS nbEntries '
+ . 'FROM freshrss_entry e '
+ . 'GROUP BY e.id_feed'
+ . ') x ON x.id_feed=f.id '
+ . 'SET f.cache_nbEntries=x.nbEntries, f.cache_nbUnreads=x.nbUnreads';
+ $stm = $this->bd->prepare ($sql);
+
+ $values = array ($feed_id);
+
+ if ($stm && $stm->execute ($values)) {
+ return $stm->rowCount();
+ } else {
+ $info = $stm->errorInfo();
+ Minz_Log::record ('SQL error : ' . $info[2], Minz_Log::ERROR);
+ return false;
+ }
+ }
}
class HelperFeed {
@@ -577,12 +579,8 @@ class HelperFeed {
$myFeed->_httpAuth (isset($dao['httpAuth']) ? base64_decode ($dao['httpAuth']) : '');
$myFeed->_error ($dao['error']);
$myFeed->_keepHistory (isset($dao['keep_history']) ? $dao['keep_history'] : '');
- if (isset ($dao['nbNotRead'])) {
- $myFeed->_nbNotRead ($dao['nbNotRead']);
- }
- if (isset ($dao['nbEntries'])) {
- $myFeed->_nbEntries ($dao['nbEntries']);
- }
+ $myFeed->_nbNotRead ($dao['cache_nbUnreads']);
+ $myFeed->_nbEntries ($dao['cache_nbEntries']);
if (isset ($dao['id'])) {
$myFeed->_id ($dao['id']);
}