Bläddra i källkod

Make httpGet() cache nullable (#8705)

Follow-up of https://github.com/FreshRSS/FreshRSS/pull/8700
Useful for https://github.com/FreshRSS/Extensions/pull/456
Alexandre Alapetite 1 månad sedan
förälder
incheckning
c4c5c72127
1 ändrade filer med 11 tillägg och 8 borttagningar
  1. 11 8
      app/Utils/httpUtil.php

+ 11 - 8
app/Utils/httpUtil.php

@@ -258,6 +258,7 @@ final class FreshRSS_http_Util {
 
 	/**
 	 * @param non-empty-string $url
+	 * @param string|null $cachePath path to cache file, or `null` to disable caching
 	 * @param string $type {html,ico,json,opml,xml}
 	 * @param array<string,mixed> $attributes
 	 * @param array<int,mixed> $curl_options
@@ -267,16 +268,18 @@ final class FreshRSS_http_Util {
 	 *   * `-429` blocked by active `Retry-After` period;
 	 *   * `-500` `curl_init()` failure.
 	 */
-	public static function httpGet(string $url, string $cachePath, string $type = 'html', array $attributes = [], array $curl_options = []): array {
+	public static function httpGet(string $url, ?string $cachePath = null, string $type = 'html', array $attributes = [], array $curl_options = []): array {
 		$limits = FreshRSS_Context::systemConf()->limits;
 		$feed_timeout = empty($attributes['timeout']) || !is_numeric($attributes['timeout']) ? 0 : intval($attributes['timeout']);
 
-		$cacheMtime = @filemtime($cachePath);
-		if ($cacheMtime !== false && $cacheMtime > time() - intval($limits['cache_duration'])) {
-			$body = @file_get_contents($cachePath);
-			if ($body != false) {
-				syslog(LOG_DEBUG, 'FreshRSS uses cache for ' . \SimplePie\Misc::url_remove_credentials($url));
-				return ['body' => $body, 'effective_url' => $url, 'redirect_count' => 0, 'fail' => false, 'status' => -200, 'error' => ''];
+		if ($cachePath !== null) {
+			$cacheMtime = @filemtime($cachePath);
+			if ($cacheMtime !== false && $cacheMtime > time() - intval($limits['cache_duration'])) {
+				$body = @file_get_contents($cachePath);
+				if ($body != false) {
+					syslog(LOG_DEBUG, 'FreshRSS uses cache for ' . \SimplePie\Misc::url_remove_credentials($url));
+					return ['body' => $body, 'effective_url' => $url, 'redirect_count' => 0, 'fail' => false, 'status' => -200, 'error' => ''];
+				}
 			}
 		}
 
@@ -420,7 +423,7 @@ final class FreshRSS_http_Util {
 			}
 		}
 
-		if (file_put_contents($cachePath, $body) === false) {
+		if ($cachePath !== null && file_put_contents($cachePath, $body) === false) {
 			Minz_Log::warning("Error saving cache $cachePath for $url");
 		}