Skip to content

Commit

Permalink
MD5-based caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Alkarex committed Jun 18, 2024
1 parent 2edfc22 commit 998bb23
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/SimplePie.php
Original file line number Diff line number Diff line change
Expand Up @@ -1634,6 +1634,39 @@ public function enable_exceptions(bool $enable = true)
$this->enable_exceptions = $enable;
}

/**
* Compute a hash (md5) for caching,
* after having removed the noise from the raw feed (e.g. comments) to improve caching.
*
* @param string $rss The raw feed content
* @return string The hash of the cleaned content
*/
private function clean_hash(string $rss): string
{
//Process by chunks not to use too much memory
if (($stream = fopen('php://temp', 'r+'))
&& fwrite($stream, $rss)
&& rewind($stream)
) {
$ctx = hash_init('md5');
while ($stream_data = fread($stream, 1048576)) {
hash_update(
$ctx, preg_replace(
[
'#<(lastBuildDate|pubDate|updated|feedDate|dc:date|slash:comments)>[^<]+</\\1>#',
'#<(media:starRating|media:statistics) [^/<>]+/>#',
'#<!--.+?-->#s',
],
'', $stream_data
)
);
}
fclose($stream);
return hash_final($ctx);
}
return '';
}

/**
* Initialize the feed object
*
Expand Down Expand Up @@ -1735,6 +1768,9 @@ public function init()
}

[$headers, $sniffed] = $fetched;
if (isset($this->data['md5'])) { // FreshRSS
$md5 = $this->data['md5'];
}
}

// Empty response check
Expand Down Expand Up @@ -1803,6 +1839,8 @@ public function init()
$this->data['headers'] = $headers;
}
$this->data['build'] = Misc::get_build();
$this->data['md5'] = $md5 === '' ? $this->clean_md5($this->raw_data) : $md5; // FreshRSS
$this->data['mtime'] = time(); // FreshRSS

// Cache the file if caching is enabled
$this->data['cache_expiration_time'] = $this->cache_duration + time();
Expand Down

0 comments on commit 998bb23

Please sign in to comment.