Cache fetched sources

dev
gravel 2 years ago
parent 1e05a74e95
commit c0a4b9c09a
Signed by: gravel
GPG Key ID: C0538F3C906B308F

1
.gitignore vendored

@ -13,6 +13,7 @@ listings/lp-output/*
# Server-side cache # Server-side cache
cache cache
cache-lt
# Local tests # Local tests
.test .test

@ -11,6 +11,8 @@
$ROOM_ICONS_CACHE="$CACHE_ROOT/icons"; $ROOM_ICONS_CACHE="$CACHE_ROOT/icons";
$ROOM_ICONS="$DOCUMENT_ROOT/icons"; $ROOM_ICONS="$DOCUMENT_ROOT/icons";
$ROOM_ICONS_RELATIVE="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"; $LISTING_PROVIDER_ROOT="$PROJECT_ROOT/listings";
$LISTINGS_INI="$LISTING_PROVIDER_ROOT/listings.ini"; $LISTINGS_INI="$LISTING_PROVIDER_ROOT/listings.ini";
@ -30,4 +32,4 @@
error_reporting(E_ALL & ~E_WARNING); error_reporting(E_ALL & ~E_WARNING);
date_default_timezone_set('UTC'); date_default_timezone_set('UTC');
?> ?>

@ -5,7 +5,7 @@
### Prerequisites ### Prerequisites
- PHP 8.1+ - PHP 8.1+
- `make` - `make`, `awk`, `xargs`
- `entr` to watch for file changes - `entr` to watch for file changes
- `xdg-open` link handler to invoke browser - `xdg-open` link handler to invoke browser
- `libgd` (`php-gd`, `phpX.Y-gd`) for downsizing images - `libgd` (`php-gd`, `phpX.Y-gd`) for downsizing images

@ -68,7 +68,11 @@ watchdog:
# Remove artefacts # Remove artefacts
clean: clean:
-git clean -dfX -awk '/^[^#]/ { printf " -e \"!%s\"", $$0 }' <etc/.gitpreserve | xargs git clean -Xdf $(CLEANFLAGS)
# Display files affected by artefact removal.
would-clean: CLEANFLAGS="-n"
would-clean: clean
# Build everything from scratch and test functionality. # Build everything from scratch and test functionality.
test: FLAGS = --verbose test: FLAGS = --verbose

@ -0,0 +1,10 @@
# Preserve the following files when cleaning the directory:
# Long-term cache
cache-lt
# Visual Studio Code
.vscode
# Kate
.kate-swp

@ -242,18 +242,31 @@
str_replace("\\/", "/", $this->contents_sdir); str_replace("\\/", "/", $this->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) { private static function fetch_source(string $source_key) {
$url = CommunitySources::SOURCES[$source_key]; $url = CommunitySources::SOURCES[$source_key];
$contents = file_get_contents($url); $contents = file_get_contents($url);
log_debug($http_response_header[0]); log_debug($http_response_header[0]);
$cache_file = CommunitySources::source_cache_file($source_key);
if (!$contents) { if ($contents) {
log_error("Could not fetch source from $url."); file_put_contents($cache_file, $contents);
return ""; 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]; return $this->room_tags[$room_id];
} }
} }
file_exists($SOURCES_CACHE) or mkdir($SOURCES_CACHE, 0755, recursive: true);
?> ?>

Loading…
Cancel
Save