From c0a4b9c09a8178d4d6b032967137d6ca208fd0a8 Mon Sep 17 00:00:00 2001 From: gravel Date: Tue, 6 Jun 2023 11:54:51 +0000 Subject: [PATCH] Cache fetched sources --- .gitignore | 1 + .phpenv | 4 +++- CONTRIBUTING.md | 2 +- Makefile | 6 +++++- etc/.gitpreserve | 10 ++++++++++ php/servers/sources.php | 23 +++++++++++++++++++---- 6 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 etc/.gitpreserve diff --git a/.gitignore b/.gitignore index 31c4439..1bbe701 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ listings/lp-output/* # Server-side cache cache +cache-lt # Local tests .test diff --git a/.phpenv b/.phpenv index 9d86cc1..9162f73 100644 --- a/.phpenv +++ b/.phpenv @@ -11,6 +11,8 @@ $ROOM_ICONS_CACHE="$CACHE_ROOT/icons"; $ROOM_ICONS="$DOCUMENT_ROOT/icons"; $ROOM_ICONS_RELATIVE="icons"; + $LONG_TERM_CACHE_ROOT="$PROJECT_ROOT/cache-lt"; + $SOURCES_CACHE="$LONG_TERM_CACHE_ROOT/sources"; $LISTING_PROVIDER_ROOT="$PROJECT_ROOT/listings"; $LISTINGS_INI="$LISTING_PROVIDER_ROOT/listings.ini"; @@ -30,4 +32,4 @@ error_reporting(E_ALL & ~E_WARNING); date_default_timezone_set('UTC'); -?> \ No newline at end of file +?> diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a745c5d..77eeeee 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -5,7 +5,7 @@ ### Prerequisites - PHP 8.1+ -- `make` +- `make`, `awk`, `xargs` - `entr` to watch for file changes - `xdg-open` link handler to invoke browser - `libgd` (`php-gd`, `phpX.Y-gd`) for downsizing images diff --git a/Makefile b/Makefile index 8fd541c..c1d3e91 100644 --- a/Makefile +++ b/Makefile @@ -68,7 +68,11 @@ watchdog: # Remove artefacts clean: - -git clean -dfX + -awk '/^[^#]/ { printf " -e \"!%s\"", $$0 }' contents_sdir); } + private static function source_cache_file(string $source_key) { + global $SOURCES_CACHE; + return "$SOURCES_CACHE/$source_key"; + } + private static function fetch_source(string $source_key) { $url = CommunitySources::SOURCES[$source_key]; $contents = file_get_contents($url); log_debug($http_response_header[0]); + $cache_file = CommunitySources::source_cache_file($source_key); - if (!$contents) { - log_error("Could not fetch source from $url."); - return ""; + if ($contents) { + file_put_contents($cache_file, $contents); + return $contents; } - return $contents; + $contents = file_get_contents($cache_file); + if ($contents) { + log_warning("Could not fetch source from $url, using cache"); + return $contents; + } + + log_error("Could not fetch source from $url."); + return ""; } /** @@ -315,4 +328,6 @@ return $this->room_tags[$room_id]; } } + + file_exists($SOURCES_CACHE) or mkdir($SOURCES_CACHE, 0755, recursive: true); ?>