소스 검색

Rafraîchissement des flux en cache super rapide

Contribue à
https://github.com/marienfressinaud/FreshRSS/issues/351#issuecomment-31755012
Les flux non-modifiés et en cache ne coûtent maintenant presque plus
rien (304, ou délai de cache SimplePie non expiré), alors qu'avant
toutes les entrées étaient rechargées
Alexandre Alapetite 12 년 전
부모
커밋
cf8ee6bd48
4개의 변경된 파일70개의 추가작업 그리고 46개의 파일을 삭제
  1. 34 23
      app/Controllers/feedController.php
  2. 9 5
      app/Models/Feed.php
  3. 15 9
      app/Models/FeedDAO.php
  4. 12 9
      lib/SimplePie/SimplePie.php

+ 34 - 23
app/Controllers/feedController.php

@@ -191,36 +191,45 @@ class FreshRSS_feed_Controller extends Minz_ActionController {
 		foreach ($feeds as $feed) {
 			try {
 				$url = $feed->url();
+				$feedHistory = $feed->keepHistory();
+
 				$feed->load(false);
 				$entries = array_reverse($feed->entries());	//We want chronological order and SimplePie uses reverse order
+				$hasTransaction = false;
 
-				//For this feed, check last n entry GUIDs already in database
-				$existingGuids = array_fill_keys ($entryDAO->listLastGuidsByFeed ($feed->id (), count($entries) + 10), 1);
-				$useDeclaredDate = empty($existingGuids);
+				if (count($entries) > 0) {
+					//For this feed, check last n entry GUIDs already in database
+					$existingGuids = array_fill_keys ($entryDAO->listLastGuidsByFeed ($feed->id (), count($entries) + 10), 1);
+					$useDeclaredDate = empty($existingGuids);
 
-				$feedHistory = $feed->keepHistory();
-				if ($feedHistory == -2) {	//default
-					$feedHistory = $this->view->conf->keep_history_default;
-				}
+					if ($feedHistory == -2) {	//default
+						$feedHistory = $this->view->conf->keep_history_default;
+					}
+
+					$hasTransaction = true;
+					$feedDAO->beginTransaction();
 
-				// On ne vérifie pas strictement que l'article n'est pas déjà en BDD
-				// La BDD refusera l'ajout car (id_feed, guid) doit être unique
-				$feedDAO->beginTransaction ();
-				foreach ($entries as $entry) {
-					$eDate = $entry->date (true);
-					if ((!isset ($existingGuids[$entry->guid ()])) &&
-						(($feedHistory != 0) || ($eDate  >= $date_min))) {
-						$values = $entry->toArray ();
-						//Use declared date at first import, otherwise use discovery date
-						$values['id'] = ($useDeclaredDate || $eDate < $date_min) ?
-							min(time(), $eDate) . uSecString() :
-							uTimeString();
-						$values['is_read'] = $is_read;
-						$entryDAO->addEntry ($values);
+					// On ne vérifie pas strictement que l'article n'est pas déjà en BDD
+					// La BDD refusera l'ajout car (id_feed, guid) doit être unique
+					foreach ($entries as $entry) {
+						$eDate = $entry->date (true);
+						if ((!isset ($existingGuids[$entry->guid ()])) &&
+							(($feedHistory != 0) || ($eDate  >= $date_min))) {
+							$values = $entry->toArray ();
+							//Use declared date at first import, otherwise use discovery date
+							$values['id'] = ($useDeclaredDate || $eDate < $date_min) ?
+								min(time(), $eDate) . uSecString() :
+								uTimeString();
+							$values['is_read'] = $is_read;
+							$entryDAO->addEntry ($values);
+						}
 					}
 				}
 
 				if (($feedHistory >= 0) && (rand(0, 30) === 1)) {
+					if (!$hasTransaction) {
+						$feedDAO->beginTransaction();
+					}
 					$nb = $feedDAO->cleanOldEntries ($feed->id (), $date_min, max($feedHistory, count($entries) + 10));
 					if ($nb > 0) {
 						Minz_Log::record ($nb . ' old entries cleaned in feed [' . $feed->url() . ']', Minz_Log::DEBUG);
@@ -228,8 +237,10 @@ class FreshRSS_feed_Controller extends Minz_ActionController {
 				}
 
 				// on indique que le flux vient d'être mis à jour en BDD
-				$feedDAO->updateLastUpdate ($feed->id ());
-				$feedDAO->commit ();
+				$feedDAO->updateLastUpdate ($feed->id (), 0, $hasTransaction);
+				if ($hasTransaction) {
+					$feedDAO->commit();
+				}
 				$flux_update++;
 				if ($feed->url() !== $url) {	//URL has changed (auto-discovery)
 					$feedDAO->updateFeed($feed->id(), array('url' => $feed->url()));

+ 9 - 5
app/Models/Feed.php

@@ -193,10 +193,10 @@ class FreshRSS_Feed extends Minz_Model {
 				}
 				$feed = customSimplePie();
 				$feed->set_feed_url ($url);
-				$feed->init ();
+				$initResult = $feed->init ();
 
-				if ($feed->error ()) {
-					throw new FreshRSS_Feed_Exception ($feed->error . ' [' . $url . ']');
+				if ((!$initResult) || $feed->error()) {
+					throw new FreshRSS_Feed_Exception ($feed->error() . ' [' . $url . ']');
 				}
 
 				// si on a utilisé l'auto-discover, notre url va avoir changé
@@ -217,11 +217,15 @@ class FreshRSS_Feed extends Minz_Model {
 					$this->_description(html_only_entity_decode($feed->get_description()));
 				}
 
-				// et on charge les articles du flux
-				$this->loadEntries ($feed);
+				if (($initResult == SIMPLEPIE_INIT_SUCCESS) || $loadDetails) {
+					$this->loadEntries($feed);	// et on charge les articles du flux
+				} else {
+					$this->entries = array();
+				}
 			}
 		}
 	}
+
 	private function loadEntries ($feed) {
 		$entries = array ();
 

+ 15 - 9
app/Models/FeedDAO.php

@@ -52,21 +52,27 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo {
 		}
 	}
 
-	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=?';
-
-		$stm = $this->bd->prepare ($sql);
+	public function updateLastUpdate ($id, $inError = 0, $updateCache = true) {
+		if ($updateCache) {
+			$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=?';
+		} else {
+			$sql = 'UPDATE `' . $this->prefix . 'feed` f '
+			     . 'SET lastUpdate=?, error=? '
+			     . 'WHERE f.id=?';
+		}
 
 		$values = array (
-			time (),
+			time(),
 			$inError,
 			$id,
 		);
 
+		$stm = $this->bd->prepare ($sql);
+
 		if ($stm && $stm->execute ($values)) {
 			return $stm->rowCount();
 		} else {

+ 12 - 9
lib/SimplePie/SimplePie.php

@@ -402,6 +402,9 @@ define('SIMPLEPIE_FILE_SOURCE_CURL', 8);
  */
 define('SIMPLEPIE_FILE_SOURCE_FILE_GET_CONTENTS', 16);
 
+define('SIMPLEPIE_INIT_FAIL', 0);	//FreshRSS
+define('SIMPLEPIE_INIT_SUCCESS', 1);	//FreshRSS
+define('SIMPLEPIE_INIT_CACHE', 2);	//FreshRSS
 
 
 /**
@@ -1226,7 +1229,7 @@ class SimplePie
 		// Check absolute bare minimum requirements.
 		if (!extension_loaded('xml') || !extension_loaded('pcre'))
 		{
-			return false;
+			return SIMPLEPIE_INIT_FAIL;
 		}
 		// Then check the xml extension is sane (i.e., libxml 2.7.x issue on PHP < 5.2.9 and libxml 2.7.0 to 2.7.2 on any version) if we don't have xmlreader.
 		elseif (!extension_loaded('xmlreader'))
@@ -1241,7 +1244,7 @@ class SimplePie
 			}
 			if (!$xml_is_sane)
 			{
-				return false;
+				return SIMPLEPIE_INIT_FAIL;
 			}
 		}
 
@@ -1273,11 +1276,11 @@ class SimplePie
 				}
 				$i++;
 			}
-			return (bool) $success;
+			return inval($success);
 		}
 		elseif ($this->feed_url === null && $this->raw_data === null)
 		{
-			return false;
+			return SIMPLEPIE_INIT_FAIL;
 		}
 
 		$this->error = null;
@@ -1298,10 +1301,10 @@ class SimplePie
 			// Fetch the data via SimplePie_File into $this->raw_data
 			if (($fetched = $this->fetch_data($cache)) === true)
 			{
-				return true;
+				return SIMPLEPIE_INIT_CACHE;
 			}
 			elseif ($fetched === false) {
-				return false;
+				return SIMPLEPIE_INIT_FAIL;
 			}
 
 			list($headers, $sniffed) = $fetched;
@@ -1378,7 +1381,7 @@ class SimplePie
 					{
 						$this->error = "A feed could not be found at $this->feed_url. This does not appear to be a valid RSS or Atom feed.";
 						$this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));
-						return false;
+						return SIMPLEPIE_INIT_FAIL;
 					}
 
 					if (isset($headers))
@@ -1392,7 +1395,7 @@ class SimplePie
 					{
 						trigger_error("$this->cache_location is not writeable. Make sure you've set the correct relative or absolute path, and that the location is server-writable.", E_USER_WARNING);
 					}
-					return true;
+					return SIMPLEPIE_INIT_SUCCESS;
 				}
 			}
 		}
@@ -1409,7 +1412,7 @@ class SimplePie
 
 		$this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));
 
-		return false;
+		return SIMPLEPIE_INIT_FAIL;
 	}
 
 	/**