Bladeren bron

SimplePie : Meilleur cache des flux avec signature MD5

Contribue à
https://github.com/marienfressinaud/FreshRSS/issues/351#issuecomment-31755012
Pour les flux qui ne supportent pas les requêtes conditionnelles.
Filtre les tags et commentaires gênants avant la signature (style
<lastBuildDate> qui change tout le temps sans que le contenu change,
<slash:comments>, ainsi que les commentaires XML qui détruisent le cache
comme <!-- généré en X secondes -->)

Il reste quelques flux à débogger dont le cache n'est pas encore
optimal. C'est pour cela qu'il reste quelques syslog(LOG_DEBUG, ...).

Au passage, évite que SimplePie fasse une double requête pour vérifier
le cache si le serveur est un peu lent.

Un jour, il faudra nettoyer les changements faits à SimplePie et leur
remonter les patchs les plus intéressants.
Alexandre Alapetite 12 jaren geleden
bovenliggende
commit
9aab83af11
3 gewijzigde bestanden met toevoegingen van 28 en 4 verwijderingen
  1. 1 0
      CHANGELOG
  2. 1 1
      app/Models/Feed.php
  3. 26 3
      lib/SimplePie/SimplePie.php

+ 1 - 0
CHANGELOG

@@ -3,6 +3,7 @@
 ## 2014-0x-xx FreshRSS 0.8
 
 * Mise à jour des flux plus rapide grâce à une meilleure utilisation du cache
+	* Utilisation d’une signature MD5 du contenu intéressant pour les flux n’implémentant pas les requêtes conditionnelles
 
 
 ## 2014-01-29 FreshRSS 0.7

+ 1 - 1
app/Models/Feed.php

@@ -218,7 +218,7 @@ class FreshRSS_Feed extends Minz_Model {
 				}
 
 				if (($mtime === true) || ($mtime > $this->lastUpdate)) {
-					syslog(LOG_DEBUG, 'FreshRSS no cache ' . $mtime . ' > ' . $this->lastUpdate);
+					syslog(LOG_DEBUG, 'FreshRSS no cache ' . $mtime . ' > ' . $this->lastUpdate . ' for ' . $subscribe_url);
 					$this->loadEntries($feed);	// et on charge les articles du flux
 				} else {
 					syslog(LOG_DEBUG, 'FreshRSS use cache for ' . $subscribe_url);

+ 26 - 3
lib/SimplePie/SimplePie.php

@@ -1212,6 +1212,10 @@ class SimplePie
 		$this->item_limit = (int) $limit;
 	}
 
+	function cleanMd5($rss) {	//FreshRSS
+		return md5(preg_replace(array('#<(lastBuildDate|pubDate|updated|feedDate|dc:date|slash:comments)>[^<]+</\\1>#', '#<!--.+?-->#s'), '', $rss));
+	}
+
 	/**
 	 * Initialize the feed object
 	 *
@@ -1305,6 +1309,10 @@ class SimplePie
 			}
 
 			list($headers, $sniffed) = $fetched;
+
+			if (isset($this->data['md5'])) {	//FreshRSS
+				$md5 = $this->data['md5'];
+			}
 		}
 
 		// Set up array of possible encodings
@@ -1387,6 +1395,7 @@ class SimplePie
 					}
 					$this->data['build'] = SIMPLEPIE_BUILD;
 					$this->data['mtime'] = time();	//FreshRSS
+					$this->data['md5'] = empty($md5) ? $this->cleanMd5($this->raw_data) : $md5;	//FreshRSS
 
 					// Cache the file if caching is enabled
 					if ($cache && !$cache->save($this))
@@ -1462,7 +1471,7 @@ class SimplePie
 				elseif ($cache->mtime() + $this->cache_duration < time())
 				{
 					// If we have last-modified and/or etag set
-					if (isset($this->data['headers']['last-modified']) || isset($this->data['headers']['etag']))
+					//if (isset($this->data['headers']['last-modified']) || isset($this->data['headers']['etag']))	//FreshRSS removed
 					{
 						$headers = array(
 							'Accept' => 'application/atom+xml, application/rss+xml, application/rdf+xml;q=0.9, application/xml;q=0.8, text/xml;q=0.8, text/html;q=0.7, unknown/unknown;q=0.1, application/unknown;q=0.1, */*;q=0.1',
@@ -1476,7 +1485,7 @@ class SimplePie
 							$headers['if-none-match'] = $this->data['headers']['etag'];
 						}
 
-						$file = $this->registry->create('File', array($this->feed_url, $this->timeout/10, 5, $headers, $this->useragent, $this->force_fsockopen));
+						$file = $this->registry->create('File', array($this->feed_url, $this->timeout, 5, $headers, $this->useragent, $this->force_fsockopen));	//FreshRSS
 
 						if ($file->success)
 						{
@@ -1488,7 +1497,20 @@ class SimplePie
 						}
 						else
 						{
-							unset($file);
+							$this->error = $file->error;	//FreshRSS
+							return !empty($this->data);	//FreshRSS
+							//unset($file);	//FreshRSS removed
+						}
+					}
+					{	//FreshRSS
+						$md5 = $this->cleanMd5($file->body);
+						if ($this->data['md5'] === $md5) {
+							syslog(LOG_DEBUG, 'SimplePie MD5 cache match for ' . $this->feed_url);
+							$cache->touch();
+							return true;	//Content unchanged even though server did not send a 304
+						} else {
+							syslog(LOG_DEBUG, 'SimplePie MD5 cache no match for ' . $this->feed_url);
+							$this->data['md5'] = $md5;
 						}
 					}
 				}
@@ -1557,6 +1579,7 @@ class SimplePie
 				{
 					$this->data = array('url' => $this->feed_url, 'feed_url' => $file->url, 'build' => SIMPLEPIE_BUILD);
 					$this->data['mtime'] = time();	//FreshRSS
+					$this->data['md5'] = empty($md5) ? $this->cleanMd5($file->body) : $md5;	//FreshRSS
 					if (!$cache->save($this))
 					{
 						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);