[web-people] master: Reduce cache timeout for fetching user data
| From: | Nikita Popov | Date: | Sun, 11 Apr 2021 16:29:04 +0000 |
| Subject: | [web-people] master: Reduce cache timeout for fetching user data | ||
| Groups: | php.webmaster | ||
| Request: | Send a blank email to php-webmaster+get-29214@lists.php.net to get a copy of this message | ||
Author: Nikita Popov (nikic)
Date: 2021-04-11T18:28:24+02:00
Commit: https://github.com/php/web-people/commit/53ec463e0ed7d47cce79ca4720e3855f3281e88b
Raw diff: https://github.com/php/web-people/commit/53ec463e0ed7d47cce79ca4720e3855f3281e88b.diff
Reduce cache timeout for fetching user data
Changed paths:
M include/misc.php
Diff:
diff --git a/include/misc.php b/include/misc.php
index 98bd791..5f57ec9 100644
--- a/include/misc.php
+++ b/include/misc.php
@@ -15,7 +15,7 @@ function getDOMNodeFrom($url, $nodename)
return $search->item(0);
}
-function findAllUsers($page) {
+function fetchAllUsers() {
$opts = array("ignore_errors" => true);
$ctx = stream_context_create(array("http" => $opts));
$token = getenv("TOKEN");
@@ -23,7 +23,7 @@ function findAllUsers($page) {
$token = trim(file_get_contents("token"));
}
$url = "https://main.php.net/fetch/allusers.php?token="
. rawurlencode($token);
- $retval = cached($url, $ctx);
+ $retval = cached($url, $ctx, "-1 hour");
$json = json_decode($retval, true);
if (!is_array($json)) {
error("Something happened to main");
@@ -31,6 +31,11 @@ function findAllUsers($page) {
if (isset($json["error"])) {
error($json["error"]);
}
+ return $json;
+}
+
+function findAllUsers($page) {
+ $json = fetchAllUsers();
usort($json, function ($a, $b) {
return strcmp($a["username"], $b["username"]);
@@ -39,23 +44,10 @@ function findAllUsers($page) {
$offset = ($page - 1) * 50;
return array_slice($json, $offset, 50);
}
+
function findPHPUser($username)
{
- $opts = array("ignore_errors" => true);
- $ctx = stream_context_create(array("http" => $opts));
- $token = getenv("TOKEN");
- if (!$token) {
- $token = trim(file_get_contents("token"));
- }
- $url = "https://main.php.net/fetch/allusers.php?token="
. rawurlencode($token);
- $retval = cached($url, $ctx);
- $json = json_decode($retval, true);
- if (!is_array($json)) {
- error("Something happend to main");
- }
- if (isset($json["error"])) {
- error($json["error"]);
- }
+ $json = fetchAllUsers();
foreach($json as $k) {
if ($k["username"] == $username) {
@@ -65,13 +57,13 @@ function findPHPUser($username)
error("No such user");
}
-function cached($url, $ctx = null)
+function cached($url, $ctx = null, $timeout = "-1 day")
{
$tmpdir = sys_get_temp_dir();
$user = sha1($url);
$tmpfile = $tmpdir . "/" . $user;
- if (file_exists($tmpfile) && filemtime($tmpfile) > strtotime("-1 day")) {
+ if (file_exists($tmpfile) && filemtime($tmpfile) > strtotime($timeout)) {
return file_get_contents($tmpfile);
}
$content = file_get_contents($url, false, $ctx);