I disagree, but its all about learning. It does exactly what its supposed to. Set the lifetime according to your traffic and upload frequency, as you do with tuning your sitemap.xml change frequency, same idea.
This method is serializing data much more efficiently than the 2 previous codes I went with to the end, so this may just be better than what people are using. Maybe the caching code you have needs a look over.
This is what I have, and has no respect to default TT template.
index.php :
torrents_cached();
functions.php :
// NEW Memcached TorrentTable function
function torrents_cached(){
global $site_config, $CURUSER, $THEME, $LANGUAGE;
$memc = new Memcached();
$memc->addServer("127.0.0.1", 11211);
//Set the table name according to Your Table
$key = md5("SELECT torrents.id, torrents.hits, torrents.views, torrents.anon, torrents.announce, torrents.category, torrents.leechers, torrents.nfo, torrents.seeders, torrents.name, torrents.times_completed, torrents.size, torrents.added, torrents.comments, torrents.numfiles, torrents.filename, torrents.owner, torrents.external, torrents.freeleech, torrents.image1, torrents.image2, torrents.hits, torrents.cover1 AS cover, categories.name AS cat_name, categories.image AS cat_pic, categories.parent_cat AS cat_parent, users.username, users.avatar, users.privacy, IF(torrents.numratings < 2, NULL, ROUND(torrents.ratingsum / torrents.numratings, 1)) AS rating FROM torrents LEFT JOIN categories ON category = categories.id LEFT JOIN users ON torrents.owner = users.id WHERE visible = 'yes' AND banned = 'no' ORDER BY torrents.added DESC LIMIT 50");
$get_result = array();
$get_result = $memc->get($key);
if($get_result){
echo '<b>Read from Memory:</b><br><br>';
foreach( $get_result as $val ){
// CHEAT SHEET - Array from memory - all tested:
/*
echo 'Name: ' . $val[0] . '<br>';
echo 'ID: ' . $val[1] . '<br>';
echo 'Cover: ' . $val[2] . '<br>';
echo 'Downloads: ' . $val[3] . '<br>';
echo 'Views: ' . $val[4] . '<br>';
echo 'Owner: ' . $val[5] . '<br>';
echo 'Seeders: ' . $val[6] . '<br>';
echo 'Leechers: ' . $val[7] . '<br>';
echo 'Category: ' . $val[8] . '<br>';
echo 'Size: ' . $val[9] . '<br>';
echo 'Image1: ' . $val[10] . '<br>';
echo 'Image2: ' . $val[11] . '<br>';
echo 'Added: ' . $val[12] . '<br>';
*/
// NOT Cached: $query["avatar"] - Until I find a way to use the left join.
// NOT Cached: $query["cat_name"] - Until I find a way to use the left join.
print("<div itemscope itemtype='http://schema.org/ItemList' class='row news-feed'>
<div class='col t12'>
<div id='infinite-newsfeed-container-page-1' style='_margin-top:-.5rem;'>
<section class='recent-news card' style='overflow:visible'>");
print('<article itemprop="itemListElement" itemscope itemtype="http://schema.org/Article" class="row" id="post-52856" class="post-52856 post type-post status-publish format-standard has-post-thumbnail hentry category-news tag-fifa-19 tag-switch news">
<div class="col t5 col-card-image">
<div class="card-image">');
print("<a itemprop=\"url\" href=\"" . $site_config['SITEURL'] . "/details/" . $val[1] . "/\">");
print("<img src='" . $site_config['SITEURL'] . "/uploads/images/" . $val[10] . "' style='height:139px;' /></a>");
print("</div></div>");
print("<div class=\"col t7\">\n");
$shortcatname = htmlspecialchars(str_replace("Switch:", "", $val[8])); // strip category name
$char1 = 39; // Cut name length
$smallname = htmlspecialchars(CutName($val[0], $char1));
$dispname = $smallname;
//$descrs = $row["description"];
//$last_access = $CURUSER["last_browse"];
$time_now = gmtime();
if ($last_access > $time_now || !is_numeric($last_access))
$last_access = $time_now;
//if (sql_timestamp_to_unix_timestamp($val[12]) >= $last_access)
//$dispname .= "New";
//if ($row["freeleech"] == 1)
// $dispname .= " <img src='images/free.gif' border='0' alt='' />";
//if ($row["team"] != 0)
// $dispname .= " <img src='" . $row["teamimage"] . "' border='0' alt = '" . $row["teamname"] . "'>";
//if ($CURUSER) {
// $bookt = SQL_Query_exec("SELECT torrentid FROM bookmarks WHERE torrentid = $id AND userid = $CURUSER[id]");
// if (mysql_num_rows($bookt) > 0) {
// $bookmark = "<a href='" . $site_config['SITEURL'] . "/bookmarks/'><img src='" . $site_config['SITEURL'] . "/images/booked.png' width='25' height='25' border='0' title='Bookmarked'></a>";
// } else {
$bookmark = "<a href='" . $site_config['SITEURL'] . "/bookmark/$id/'><img src='" . $site_config['SITEURL'] . "/images/book.png' width='25' height='25' border='0' title='Bookmark this Torrent'></a>";
// echo "<a href='" . $site_config['SITEURL'] . "/bookmark/$id/'><img src='" . $site_config['SITEURL'] . "/images/book.png' width='25' height='25' border='0' title='Bookmark this Torrent'></a>";
// }
// }
//if (($row["anon"] == "yes" || $row["privacy"] == "strong") && $CURUSER["id"] != $row["owner"] && $CURUSER["edit_torrents"] != "yes")
// echo "Anonymous";
// elseif ($row["username"])
// New tab-style torrent stats per result - homepage (views, downloads) - Feb 2, 2020
print("<span class=\"post-topic left\" style='text-transform:capitalize;'>Added ".date("M d, Y", utc_to_tz_time($val[12]))." <small>by</small> <span style='text-transform:capitalize;'><a href='" . $site_config['SITEURL'] . "/user/?id=$val[5]'>".$row['username']."</a> <img src='" . $site_config['SITEURL'] . "/" . $row['avatar'] . "' style=\"margin-bottom:-3px;height:15px;width:15px;border-radius:50%\" /></span></span><span class=\"post-topic right\" style='text-transform:lowercase;color:#242424;cursor:default;margin-right:-1px;margin-top:-20px'><span style='box-shadow:inset 0 0 10px #D42128;color:#FFF;background:#ED2324;border-radius:4px;padding-left:7px;padding-right:7px;padding-top:4px;padding-bottom:3px'>$val[4] <img src='" . $site_config['SITEURL'] . "/images/index_views.png' class='responsive-img' style='width:16px;height:16px;vertical-align:top' /></span><span style='box-shadow:inset 0 0 10px #D42128;color:#FFF;background:#ED2324;border-radius:4px;padding-left:7px;padding-right:6px;padding-top:4px;padding-bottom:3px;margin-left:7px'>$val[3] <img src='/images/index_dl.png' class='responsive-img' style='width:12px;height:8px;vertical-align:middle' /></span></span>");
if ($CURUSER) {
print("<span itemprop=\"name\"><h3 itemprop=\"headline\" class=\"entry-title\">" . (count($expandrows) ? "<a rel=\"bookmark\" href=\"javascript: klappe_torrent('t" . $val[0] . "')\"></a>" : "") . " $bookmark <a title=\"" . $val[0] . "\" href=\"/details/$val[1]/\" style='color:#000;text-decoration:none;'>$dispname</a> $freeleech $new</h3></span>");
} elseif (!$CURUSER) {
print("<span itemprop=\"name\"><h3 itemprop=\"headline\" class=\"entry-title\">" . (count($expandrows) ? "<a rel=\"bookmark\" href=\"javascript: klappe_torrent('t" . $val[0] . "')\"></a>" : "") . " <a href='/signin/?returnto=/bookmark.php'><img src='../images/book.png' width='25' height='25' border='0' title='Bookmarked'></a> <a title=\"" . $val[0] . "\" href=\"/details/$val[1]/\" style-='color:#000;text-decoration:none;'>$dispname</a> $freeleech $new</h3></span>");
}
print("<div itemprop=\"commentCount\" class=\"comments card-title z-depth-1 valign-wrapper\"><span class=\"valign\"><a itemprop=\"discussionUrl\" class=\"hidden_count\" href=\"/details/$id/#comments\">" . number_format($row["comments"]) . "</a></span></div>");
if ($CURUSER) { // current user
print("<form method='post' action='" . $site_config['SITEURL'] . "/details/$val[1]/' onsubmit='return validate();'><span style='box-shadow:inset 0 0 10px #D42128;background:#ED2324;border-bottom-left-radius:3px;padding-left:7px;padding-right:7px;padding-top:4px;padding-bottom:3px;position:absolute;left:0px;bottom:0px'><a style='color:#FFF;font-size:14px' href='" . $site_config['SITEURL'] . "/cat/" . $val[8] . "/' title='Browse " . $shortcatname . " Torrents'>" . $shortcatname . "</a></span><span style='margin-right:20px;'><button class='btn btn-danger' onclick='this.form.submit();' type='submit' style='height:35px;width:158px;margin-top:-9px;'>Torrent</button></span><span style='font-size:18.75pt;color:#C0C0C0;'>".mksize($val[9])."</span><div style='clear:both;display:inline;float:right;text-align:right;'><span style='text-align:right;font-size:18.75pt;color:#2E8B57;'>".number_format($val[6])."</span>
<span style='text-align:right;font-size:18.75pt;color:#C0C0C0;padding-left:10px;padding-right:5px;'>/</span></form>");
print("<span style='text-align:right;font-size:18.75pt;color:#DC143C;'>" . number_format($val[7]) . "</span>\n");
/* WITH DOWNLOAD BUTTON (DIRECT DOWNLOADING)
print("<form method='post' action='" . $site_config['SITEURL'] . "/download/$id/" . rawurlencode($row["filename"]) . "' onsubmit='return validate();'><span style='box-shadow:inset 0 0 10px #D42128;background:#ED2324;border-bottom-left-radius:3px;padding-left:7px;padding-right:7px;padding-top:4px;padding-bottom:3px;position:absolute;left:0px;bottom:0px'><a style='color:#FFF;font-size:14px' href='" . $site_config['SITEURL'] . "/cat/" . $row['category'] . "/' title='Browse " . $shortcatname . " Torrents'>" . $shortcatname . "</a></span><span style='margin-right:20px;'><button class='btn btn-danger' onclick='this.form.submit();' type='submit' style='height:35px;width:158px;margin-top:-9px;'>Download</button></span><span style='font-size:18.75pt;color:#C0C0C0;'>".mksize($row["size"])."</span><div style='clear:both;display:inline;float:right;text-align:right;'><span style='text-align:right;font-size:18.75pt;color:#2E8B57;'>".number_format($row["seeders"])."</span>
<span style='text-align:right;font-size:18.75pt;color:#C0C0C0;padding-left:10px;padding-right:5px;'>/</span></form>");
*/
} else { // not current user
print("<form method='post' action='" . $site_config['SITEURL'] . "/signin/?returnto=/download/$val[1]/" . rawurlencode($row["filename"]) . "' onsubmit='return validate();'><span style='box-shadow:inset 0 0 10px #D42128;background:#ED2324;border-bottom-left-radius:3px;padding-left:7px;padding-right:7px;padding-top:4px;padding-bottom:3px;position:absolute;left:0px;bottom:0px'><a style='color:#FFF;font-size:14px' href='" . $site_config['SITEURL'] . "/cat/" . $val[8] . "/' title='Browse " . $shortcatname . " Torrents'>" . $shortcatname . "</a></span><span style='margin-right:20px;'><button class='btn btn-danger' onclick='this.form.submit();' type='submit' style='height:35px;width:158px;margin-top:-9px;'>Torrent</button></span><span style='font-size:18.75pt;color:#C0C0C0;'>".mksize($val[9])."</span><div style='clear:both;display:inline;float:right;text-align:right;'><span style='text-align:right;font-size:18.75pt;color:#2E8B57;'>".number_format($val[6])."</span>
<span style='text-align:right;font-size:18.75pt;color:#C0C0C0;padding-left:10px;padding-right:5px;'>/</span></form>");
print("<span style='text-align:right;font-size:18.75pt;color:#DC143C;'>" . number_format($val[7]) . "</span>\n");
}
print("</div></article>
</section>
</div><!-- END #infinite-newsfeed-container-page-1 -->
</div><!-- END col t12 -->
</div><!-- END row news-feed -->
</div><!-- END content-inner -->");
}
}else{ // ----------------------------------
//Set the table name according to Your Table
$query = "SELECT torrents.id, torrents.hits, torrents.views, torrents.anon, torrents.announce, torrents.category, torrents.leechers, torrents.nfo, torrents.seeders, torrents.name, torrents.times_completed, torrents.size, torrents.added, torrents.comments, torrents.numfiles, torrents.filename, torrents.owner, torrents.external, torrents.freeleech, torrents.image1, torrents.image2, torrents.hits, torrents.cover1 AS cover, categories.name AS cat_name, categories.image AS cat_pic, categories.parent_cat AS cat_parent, users.username, users.avatar, users.privacy, IF(torrents.numratings < 2, NULL, ROUND(torrents.ratingsum / torrents.numratings, 1)) AS rating FROM torrents LEFT JOIN categories ON category = categories.id LEFT JOIN users ON torrents.owner = users.id WHERE visible = 'yes' AND banned = 'no' ORDER BY torrents.added DESC LIMIT 50";
$result = mysql_query($query) or die('Error in Query!');
echo '<b>Read from Database:</b><br><br>';
$getAllResult = array();
while( $row = mysql_fetch_array($result) ){
//print "<p><small>Retrieved data from Memcached</small></p>";
print("<div itemscope itemtype='http://schema.org/ItemList' class='row news-feed'><div class='col t12'>
<div id='infinite-newsfeed-container-page-1' style='_margin-top:-.5rem;'>
<section class='recent-news card' style='overflow:visible'>");
print('<article itemprop="itemListElement" itemscope itemtype="http://schema.org/Article" class="row" id="post-52856" class="post-52856 post type-post status-publish format-standard has-post-thumbnail hentry category-news tag-fifa-19 tag-switch news"><div class="col t5 col-card-image">
<div class="card-image">');
print("<a itemprop=\"url\" href=\"" . $site_config['SITEURL'] . "/details/$id/\">");
print("<img src='" . $site_config['SITEURL'] . "/uploads/images/" . $row['image1'] . "' style='height:139px;' /></a>");
print("</div></div>");
print("<div class=\"col t7\">\n");
$shortcatname = htmlspecialchars(str_replace("Switch:", "", $row["cat_name"])); // strip category name
$char1 = 39; // Cut name length
$smallname = htmlspecialchars(CutName($row["name"], $char1));
$dispname = $smallname;
$descrs = $row["description"];
$last_access = $CURUSER["last_browse"];
$time_now = gmtime();
if ($last_access > $time_now || !is_numeric($last_access))
$last_access = $time_now;
//if (sql_timestamp_to_unix_timestamp($row["added"]) >= $last_access)
//$dispname .= "New";
if ($row["freeleech"] == 1)
$dispname .= " <img src='images/free.gif' border='0' alt='' />";
if ($row["team"] != 0)
$dispname .= " <img src='" . $row["teamimage"] . "' border='0' alt = '" . $row["teamname"] . "'>";
/*
if ($CURUSER) {
$bookt = SQL_Query_exec("SELECT torrentid FROM bookmarks WHERE torrentid = $id AND userid = $CURUSER[id]");
if (mysql_num_rows($bookt) > 0) {
$bookmark = "<a href='" . $site_config['SITEURL'] . "/bookmarks/'><img src='" . $site_config['SITEURL'] . "/images/booked.png' width='25' height='25' border='0' title='Bookmarked'></a>";
} else {
$bookmark = "<a href='" . $site_config['SITEURL'] . "/bookmark/$id/'><img src='" . $site_config['SITEURL'] . "/images/book.png' width='25' height='25' border='0' title='Bookmark this Torrent'></a>";
}
}
*/
if (($row["anon"] == "yes" || $row["privacy"] == "strong") && $CURUSER["id"] != $row["owner"] && $CURUSER["edit_torrents"] != "yes")
echo "Anonymous";
elseif ($row["username"])
// New tab-style torrent stats per result - homepage (views, downloads) - Feb 2, 2020
print("<span class=\"post-topic left\" style='text-transform:capitalize;'>Added ".date("M d, Y", utc_to_tz_time($row['added']))." <small>by</small> <span style='text-transform:capitalize;'><a href='" . $site_config['SITEURL'] . "/user/?id=$row[owner]'>".$row['username']."</a> <img src='" . $site_config['SITEURL'] . "/" . $row['avatar'] . "' style=\"margin-bottom:-3px;height:15px;width:15px;border-radius:50%\" /></span></span><span class=\"post-topic right\" style='text-transform:lowercase;color:#242424;cursor:default;margin-right:-1px;margin-top:-20px'><span style='box-shadow:inset 0 0 10px #D42128;color:#FFF;background:#ED2324;border-radius:4px;padding-left:7px;padding-right:7px;padding-top:4px;padding-bottom:3px'>$row[views] <img src='" . $site_config['SITEURL'] . "/images/index_views.png' class='responsive-img' style='width:16px;height:16px;vertical-align:top' /></span><span style='box-shadow:inset 0 0 10px #D42128;color:#FFF;background:#ED2324;border-radius:4px;padding-left:7px;padding-right:6px;padding-top:4px;padding-bottom:3px;margin-left:7px'>$row[hits] <img src='/images/index_dl.png' class='responsive-img' style='width:12px;height:8px;vertical-align:middle' /></span></span>");
if ($CURUSER) {
print("<span itemprop=\"name\"><h3 itemprop=\"headline\" class=\"entry-title\">" . (count($expandrows) ? "<a rel=\"bookmark\" href=\"javascript: klappe_torrent('t" . $row['id'] . "')\"></a>" : "") . " $bookmark <a title=\"" . $row["name"] . "\" href=\"/details/$id/\" style='color:#000;text-decoration:none;'>$dispname</a> $freeleech $new</h3></span>");
} elseif (!$CURUSER) {
print("<span itemprop=\"name\"><h3 itemprop=\"headline\" class=\"entry-title\">" . (count($expandrows) ? "<a rel=\"bookmark\" href=\"javascript: klappe_torrent('t" . $row['id'] . "')\"></a>" : "") . " <a href='/signin/?returnto=/bookmark.php'><img src='../images/book.png' width='25' height='25' border='0' title='Bookmarked'></a> <a title=\"" . $row["name"] . "\" href=\"/details/$id/\" style-='color:#000;text-decoration:none;'>$dispname</a> $freeleech $new</h3></span>");
}
print("<div itemprop=\"commentCount\" class=\"comments card-title z-depth-1 valign-wrapper\"><span class=\"valign\"><a itemprop=\"discussionUrl\" class=\"hidden_count\" href=\"/details/$id/#comments\">" . number_format($row["comments"]) . "</a></span></div>");
if ($CURUSER) { // current user
print("<form method='post' action='" . $site_config['SITEURL'] . "/details/$id/' onsubmit='return validate();'><span style='box-shadow:inset 0 0 10px #D42128;background:#ED2324;border-bottom-left-radius:3px;padding-left:7px;padding-right:7px;padding-top:4px;padding-bottom:3px;position:absolute;left:0px;bottom:0px'><a style='color:#FFF;font-size:14px' href='" . $site_config['SITEURL'] . "/cat/" . $row['category'] . "/' title='Browse " . $shortcatname . " Torrents'>" . $shortcatname . "</a></span><span style='margin-right:20px;'><button class='btn btn-danger' onclick='this.form.submit();' type='submit' style='height:35px;width:158px;margin-top:-9px;'>Torrent</button></span><span style='font-size:18.75pt;color:#C0C0C0;'>".mksize($row["size"])."</span><div style='clear:both;display:inline;float:right;text-align:right;'><span style='text-align:right;font-size:18.75pt;color:#2E8B57;'>".number_format($row["seeders"])."</span>
<span style='text-align:right;font-size:18.75pt;color:#C0C0C0;padding-left:10px;padding-right:5px;'>/</span></form>");
print("<span style='text-align:right;font-size:18.75pt;color:#DC143C;'>" . number_format($val[7]) . "</span>\n");
/* WITH DOWNLOAD BUTTON (DIRECT DOWNLOADING)
print("<form method='post' action='" . $site_config['SITEURL'] . "/download/$id/" . rawurlencode($row["filename"]) . "' onsubmit='return validate();'><span style='box-shadow:inset 0 0 10px #D42128;background:#ED2324;border-bottom-left-radius:3px;padding-left:7px;padding-right:7px;padding-top:4px;padding-bottom:3px;position:absolute;left:0px;bottom:0px'><a style='color:#FFF;font-size:14px' href='" . $site_config['SITEURL'] . "/cat/" . $row['category'] . "/' title='Browse " . $shortcatname . " Torrents'>" . $shortcatname . "</a></span><span style='margin-right:20px;'><button class='btn btn-danger' onclick='this.form.submit();' type='submit' style='height:35px;width:158px;margin-top:-9px;'>Download</button></span><span style='font-size:18.75pt;color:#C0C0C0;'>".mksize($row["size"])."</span><div style='clear:both;display:inline;float:right;text-align:right;'><span style='text-align:right;font-size:18.75pt;color:#2E8B57;'>".number_format($row["seeders"])."</span>
<span style='text-align:right;font-size:18.75pt;color:#C0C0C0;padding-left:10px;padding-right:5px;'>/</span></form>");
*/
} else { // not current user
print("<form method='post' action='" . $site_config['SITEURL'] . "/signin/?returnto=/download/$id/" . rawurlencode($row["filename"]) . "' onsubmit='return validate();'><span style='box-shadow:inset 0 0 10px #D42128;background:#ED2324;border-bottom-left-radius:3px;padding-left:7px;padding-right:7px;padding-top:4px;padding-bottom:3px;position:absolute;left:0px;bottom:0px'><a style='color:#FFF;font-size:14px' href='" . $site_config['SITEURL'] . "/cat/" . $row['category'] . "/' title='Browse " . $shortcatname . " Torrents'>" . $shortcatname . "</a></span><span style='margin-right:20px;'><button class='btn btn-danger' onclick='this.form.submit();' type='submit' style='height:35px;width:158px;margin-top:-9px;'>Torrent</button></span><span style='font-size:18.75pt;color:#C0C0C0;'>".mksize($row["size"])."</span><div style='clear:both;display:inline;float:right;text-align:right;'><span style='text-align:right;font-size:18.75pt;color:#2E8B57;'>".number_format($row["seeders"])."</span>
<span style='text-align:right;font-size:18.75pt;color:#C0C0C0;padding-left:10px;padding-right:5px;'>/</span></form>");
print("<span style='text-align:right;font-size:18.75pt;color:#DC143C;'>" . number_format($val[7]) . "</span>\n");
}
print("</div></article>
</section>
</div><!-- END #infinite-newsfeed-container-page-1 -->
</div><!-- END col t12 -->
</div><!-- END row news-feed -->
</div><!-- END content-inner -->");
// Result set - Full array for what we need.
$getAllResult[] = array($row['name'], $row['id'], $row['cover'], $row['hits'], $row['views'], $row['owner'], $row['seeders'], $row['leechers'], $row['category'], $row['size'], $row['image1'], $row['image2'], $row['added']);
//$getAllResult[] = array($row['name'], $row['id'], $row['cover']);
}
// Set Data In Cache
// 300 = 5 minutes, 600 = 10 minutes, 900 = 15 minutes, 0 = lifetime
$memc->set($key, $getAllResult, 7200);
}
} // end function
Change
[color=rgb(184, 49, 47)]7200[/color] to your desired cache time (near last line)