从Mysql缓存多个API请求

时间:2017-12-23 03:17:02

标签: php mysql caching

我正在尝试缓存从SQL数据库中提取的每个数据。我已成功建立了一个缓存机制,但它只缓存了SQL数据库中的一个名称。

过程:

它只能同时缓存一个玩家。是否可以为每个转换后的uuid创建多个文件?

<?php

                        $connection2 = mysqli_connect("localhost", "root", "admin", "intense");

                        require_once "includes/dbConfig.php";

                        $uuid = $_GET['uuid'];

                        $query2 = "SELECT * FROM irduels_stats WHERE uuid = '$uuid'";
                        $result2 = mysqli_query($connection2, $query2);

                        while($row = mysqli_fetch_array($result2)) {
                            $rows[] = $row;

                            $uuid = $row['uuid'];

                            echo "<img src='https://crafatar.com/avatars/" . $row['uuid'] . "?size=100' alt=\"...\" class=\"img-thumbnail\">";

                            $uuidName = $row['uuid'];
                            $uuidName = str_replace('-', '', $uuidName);

                            $cache_file = "players/data.json";

                            if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 60 * 5 ))) {
                                // Cache file is less than five minutes old.
                                // Don't bother refreshing, just use the file as-is.
                                $file = file_get_contents($cache_file);
                            } else {
                                // Our cache is out-of-date, so load the data from our remote server,
                                // and also save it over our cache for next time.
                                $file = file_get_contents("https://sessionserver.mojang.com/session/minecraft/profile/". $uuidName ."");
                                file_put_contents($cache_file, $file, LOCK_EX);
                            }

                            $json_response = file_get_contents("data.json");
                            $data = json_decode($json_response);
                            $uuidConverted = $data->name;

                            echo "<blockquote class=\"blockquote playerName\">";

                            echo "<h2 class=\"mb-0\">". $uuidConverted ." <span class=\"badge badge-secondary\">Rank here</span></h2>";
                            echo "<footer class=\"blockquote-footer\">Viewing stats of <cite title=\"Source Title\">". $uuidConverted ."</cite></footer>";
                        }

                        echo "</blockquote>";

                        mysqli_close($connection2); //Make sure to close out the database connection

                        ?>

0 个答案:

没有答案