Skip to content

Commit

Permalink
Fix HTTP cache of user queries (FreshRSS#6718)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alkarex authored Aug 16, 2024
1 parent 1fad724 commit c480e57
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
23 changes: 21 additions & 2 deletions app/Models/UserDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,33 @@ public static function exists(string $username): bool {
return is_dir(USERS_PATH . '/' . $username);
}

/** Update time of the last modification action by the user (e.g., mark an article as read) */
public static function touch(string $username = ''): bool {
if (!FreshRSS_user_Controller::checkUsername($username)) {
if ($username === '') {
$username = Minz_User::name() ?? Minz_User::INTERNAL_USER;
} elseif (!FreshRSS_user_Controller::checkUsername($username)) {
return false;
}
return touch(USERS_PATH . '/' . $username . '/config.php');
}

/** Time of the last modification action by the user (e.g., mark an article as read) */
public static function mtime(string $username): int {
return @(int)filemtime(USERS_PATH . '/' . $username . '/config.php');
return @filemtime(USERS_PATH . '/' . $username . '/config.php') ?: 0;
}

/** Update time of the last new content automatically received by the user (e.g., cron job, WebSub) */
public static function ctouch(string $username = ''): bool {
if ($username === '') {
$username = Minz_User::name() ?? Minz_User::INTERNAL_USER;
} elseif (!FreshRSS_user_Controller::checkUsername($username)) {
return false;
}
return touch(USERS_PATH . '/' . $username . '/' . LOG_FILENAME);
}

/** Time of the last new content automatically received by the user (e.g., cron job, WebSub) */
public static function ctime(string $username): int {
return @filemtime(USERS_PATH . '/' . $username . '/' . LOG_FILENAME) ?: 0;
}
}
6 changes: 1 addition & 5 deletions lib/lib_rss.php
Original file line number Diff line number Diff line change
Expand Up @@ -631,11 +631,7 @@ function invalidateHttpCache(string $username = ''): bool {
Minz_Session::_param('touch', uTimeString());
$username = Minz_User::name() ?? Minz_User::INTERNAL_USER;
}
$ok = @touch(DATA_PATH . '/users/' . $username . '/' . LOG_FILENAME);
//if (!$ok) {
//TODO: Display notification error on front-end
//}
return $ok;
return FreshRSS_UserDAO::ctouch($username);
}
/**
Expand Down
7 changes: 6 additions & 1 deletion p/api/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,13 @@

if (!file_exists(DATA_PATH . '/no-cache.txt')) {
require(LIB_PATH . '/http-conditional.php');
$dateLastModification = max(
FreshRSS_UserDAO::ctime($user),
FreshRSS_UserDAO::mtime($user),
@filemtime(DATA_PATH . '/config.php') ?: 0
);
// TODO: Consider taking advantage of $feedMode, only for monotonous queries {all, categories, feeds} and not dynamic ones {read/unread, favourites, user labels}
if (httpConditional(FreshRSS_UserDAO::mtime($user) ?: time(), 0, 0, false, PHP_COMPRESSION, false)) {
if (httpConditional($dateLastModification ?: time(), 0, 0, false, PHP_COMPRESSION, false)) {
exit(); //No need to send anything
}
}
Expand Down
5 changes: 3 additions & 2 deletions p/i/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@
require(LIB_PATH . '/http-conditional.php');
$currentUser = Minz_User::name();
$dateLastModification = $currentUser === null ? time() : max(
@filemtime(USERS_PATH . '/' . $currentUser . '/' . LOG_FILENAME) ?: 0,
FreshRSS_UserDAO::ctime($currentUser),
FreshRSS_UserDAO::mtime($currentUser),
@filemtime(DATA_PATH . '/config.php') ?: 0
);
if (httpConditional($dateLastModification, 0, 0, false, PHP_COMPRESSION, true)) {
if (httpConditional($dateLastModification ?: time(), 0, 0, false, PHP_COMPRESSION, true)) {
Minz_Session::init('FreshRSS');
Minz_Session::_param('keepAlive', 1); //To prevent the PHP session from expiring
exit(); //No need to send anything
Expand Down

0 comments on commit c480e57

Please sign in to comment.