Login
Username: Password:
Remember me

[Sign Up] / [Recover Password]

Theme / Language

Popular
New Torrents
new name
Size: 18.40 MB
Most active
new name
S: 74 L: 3
New Posters
[MOD] Memcached addition for TorrentTrad
Controls ·  Search New posts · Mark unread

You are in: Forum / Forum / Torrent Trader / v3 Mods in Progress

Unknown User
2020-02-22 16:20:24
With memcached, you can actually get all keys from the cache with getAllKeys();

1. Installation
Debian based systems
sudo apt install memcached

RHEL based systems
sudo yum install memcached


2. Open backend/config.php and replace cache_type and cache_memcache_host to:

$site_config["cache_type"] = "memcached"; // disk = Save cache to disk, memcache = Use memcache, memcached = Use memcached, apc = Use APC, xcache = Use XCache
$site_config["cache_memcache_host"] = "127.0.0.1"; // Host memcache is running on
$site_config["cache_memcache_port"] = 11211; // Port memcache is running on


3. Open backend/cache.php and replace the following with:
<?php
$GLOBALS["TTCache"] = new TTCache;
class TTCache {
   function __construct () {
       global $site_config;
       $this->cachedir = $site_config["cache_dir"];
       $this->type = strtolower(trim($site_config["cache_type"]));
       switch ($this->type) {
           case "memcache":
               $this->obj = new Memcache();
               if (!$this->obj->Connect($site_config["cache_memcache_host"], $site_config["cache_memcache_port"]))
                   $this->type = "disk";
           break;
           case "memcached":
               $this->obj = new Memcached();
               if (!$this->obj->addServer($site_config["cache_memcache_host"], $site_config["cache_memcache_port"]))
                   $this->type = "disk";
           break;      
           case "apc":
               if (function_exists("apc_store"))
                   break;
           case "xcache":
               if (function_exists("xcache_set"))
                   break;
           default:
               $this->type = "disk";
       }
   }
   function Set ($var, $val, $expire = 0) {
   global $site_config;
       if ($expire == 0)
           return;
       switch ($this->type) {
           case "memcache":
               return $this->obj->set($site_config['SITENAME']."_".$var, $val, $expire);
           break;
           case "memcached":
               return $this->obj->set($site_config['SITENAME']."_".$var, $val, $expire);
           break;
           case "apc":
               return apc_store($var, $val, $expire);
           break;
           case "disk":
               $fp = fopen($this->cachedir."/$var.cache", "w");
               fwrite($fp, serialize($val));
               fclose($fp);
               return;
           break;
           case "xcache":
               return xcache_set($var, serialize($val), $expire);
           break;
       }
   }
   function Delete ($var) {
       global $site_config;
       switch ($this->type) {
           case "memcache":
               return $this->obj->delete($site_config['SITENAME']."_".$var);
           break;
           case "memcached":
               return $this->obj->delete($site_config['SITENAME']."_".$var);
           break;
           case "apc":
               return apc_delete($var);
           break;
           case "disk":
               @unlink($this->cachedir."/$var.cache");
           break;
           case "xcache":
               return xcache_unset($var);
           break;
       }
   }
   function Get ($var, $expire = 0) {
   global $site_config;
       if ($expire == 0)
           return false;
       switch ($this->type) {
           case "memcache":
               return $this->obj->get($site_config['SITENAME']."_".$var);
           break;
           case "memcached":
               return $this->obj->get($site_config['SITENAME']."_".$var);
           break;
           case "apc":
               return apc_fetch($var);
           break;
           case "disk":
               $file = $this->cachedir."/$var.cache";
               if (file_exists($file) && (time() - filemtime($file)) < $expire)
                   return unserialize(file_get_contents($file));  
               return false;
           break;
           case "xcache":
              if (xcache_isset($var))
                  return unserialize(xcache_get($var));
              return false;
           break;
       }
   }
}
// Cached MySQL Functions
function get_row_count_cached ($table, $suffix = "") {
   global $TTCache;
   $query = "SELECT COUNT(*) FROM $table $suffix";
   $cache = "get_row_count/".sha1($query);
   if (($ret = $TTCache->Get($cache, 300)) === false) {
       $res = SQL_Query_exec($query);
       $row = mysqli_fetch_row($res);
       $ret = $row[0];
       $TTCache->Set($cache, $ret, 300);
   }
   return $ret;
}
function SQL_Query_exec_cached ($query, $cache_time = 300, $cache_blank = 1) {
   global $TTCache;
   $cache = "queries/".sha1($query);
   if (($rows = $TTCache->Get($cache, $cache_time)) === false) {
       $res = SQL_Query_exec($query);
       $rows = array();
       while ($row = mysqli_fetch_assoc($res))
           $rows[] = $row;
       if (count($rows) || $cache_blank)
           $TTCache->Set($cache, $rows, $cache_time);
   }
   return count($rows) ? $rows : false;
}
?>
Unknown User
2020-02-22 16:32:30
USAGE:
(DO NOT USE QUERIES WHERE YOU HAVE A DYNAMIC WHERE = ?)

$stmt = SQL_Query_exec_cached("SELECT * FROM torrents LIMIT 20 ORDER BY seeders", 21600);
foreach ($stmt as $torrent)
{
     echo $torrent['name'];
}


This will cache 20 torrents limited by seeders into cache and this function will be retrvieved from memcached and will be saved there for 6 hours before requerying it again.

CHECK CACHE:
To check a cache to see if the data is stored there you can:
$cache = new Memcached();
$cache->addServer($site_config["cache_memcache_host"], $site_config["cache_memcache_port"]);
print_r($cache->getAllKeys());
Unknown User
2020-02-22 16:48:57
"xblade, post: 67952, member: 20395":

MVP and i was chating about this some time ago and this will not work

What won't work? I've already tested it and you get the data from the cache itself rather than query.
Unknown User
2020-02-22 16:55:32
"xblade, post: 67952, member: 20395":

...Also where is the ACP  to clear the Memcached data...

You really need to learn manuals of memcached before commenting.

To clear the cache you can do this: (I've added an echo so you can see that the data is actually being deleted)

$cache = new Memcached();
$cache->addServer('127.0.0.1', 11211);

$results = $cache->getAllKeys();
foreach ($results as $k => $v)
{
$cache->delete($v);
echo 'Cached file number: ' . $k . ' with the name: ' . $v . ' has been deleted. <br>';
}
2020-02-23 21:28:19
I did this weeks ago and demonstrated in index.php and using the PHP mecached management script to monitor data. This guide seems to trail off after the functions.php which implies it’s experimental. Thanks so much, I’ll post my bit today of what I did to torrenttable() caching @ 128MB allocated.
Unknown User
2020-02-23 21:41:06
"STorrents2020, post: 67974, member: 20382":

I did this weeks ago and demonstrated in index.php and using the PHP mecached management script to monitor data. This guide seems to trail off after the functions.php which implies it’s experimental. Thanks so much, I’ll post my bit today of what I did to torrenttable() caching @ 128MB allocated.

Alright, waiting for your input.
2020-02-23 21:48:19
You got as far as implementing the backend support on port 11211, you should include the memcached manager for admincp, it’s easier. Next, is caching and requesting data for torrenttable() which has a 100% success rate/hit. let me reimplement this and see what’s going on. I dedicated to not used this after some tests, so I need to find my code snippet.
2020-02-23 22:59:25
Try this
memcached_manage.php.zip
Download
2020-02-23 23:36:35
To debug the connection, go with this


<?
/**
* Class and Function List:
* Function list:
* - TTCache()
* - Set()
* - Delete()
* - Get()
* - get_row_count_cached()
* - SQL_Query_exec_cached()
* Classes list:
* - TTCache
*/
$GLOBALS["TTCache"] = new TTCache;

class TTCache
{
function TTCache()
{
 global $site_config;
 $this->cachedir = $site_config["cache_dir"];
 $this->type     = strtolower(trim($site_config["cache_type"]));

 switch ($this->type)
 {
  case "memcached":

   $this->obj      = new Memcached;
   $this
    ->obj
    ->addServer('127.0.0.1', 11211);
   $this
    ->obj
    ->setOption(Memcached::OPT_BINARY_PROTOCOL, true);

  break;
  case "apc":
   if (function_exists("apc_store")) break;
  case "xcache":
   if (function_exists("xcache_set")) break;
  default:
   $this->type = "disk";
  }
 }

 function Set($var, $val, $expire     = 0)
 {
  global $site_config;
  if ($expire == 0) return;
  switch ($this->type)
  {
   case "memcached":
    return $this
     ->obj
     ->set($site_config['SITENAME'] . "_" . $var, $val, 0, $expire);
   break;
   case "apc":
    return apc_store($var, $val, $expire);
   break;
   case "disk":
    $fp = fopen($this->cachedir . "/$var.cache", "w");
    fwrite($fp, serialize($val));
    fclose($fp);
    return;
   break;
   case "xcache":
    return xcache_set($var, serialize($val) , $expire);
   break;
  }
 }

 function Delete($var)
 {
  global $site_config;

  switch ($this->type)
  {
   case "memcached":
    return $this
     ->obj
     ->delete($site_config['SITENAME'] . "_" . $var);
   break;
   case "apc":
    return apc_delete($var);
   break;
   case "disk":
    @unlink($this->cachedir . "/$var.cache");
   break;
   case "xcache":
    return xcache_unset($var);
   break;
  }
 }

 function Get($var, $expire = 0)
 {
  global $site_config;
  if ($expire == 0) return false;
  switch ($this->type)
  {
   case "memcached":
    return $this
     ->obj
     ->get($site_config['SITENAME'] . "_" . $var);
   break;
   case "apc":
    return apc_fetch($var);
   break;
   case "disk":
    $file = $this->cachedir . "/$var.cache";
    if (file_exists($file) && (time() - filemtime($file)) < $expire) return unserialize(file_get_contents($file));
    return false;
    break;
   case "xcache":
    if (xcache_isset($var)) return unserialize(xcache_get($var));
    return false;
    break;
   }
  }
}

// Cached MySQL Functions
function get_row_count_cached($table, $suffix = "")
{
 global $TTCache;

 $query  = "SELECT COUNT(*) FROM $table $suffix";
 $cache  = "get_row_count/" . sha1($query);
 if (($ret    = $TTCache->Get($cache, 300)) === false)
 {
  $res    = SQL_Query_exec($query);
  $row    = mysql_fetch_row($res);
  $ret    = $row[0];
  $TTCache->Set($cache, $ret, 300);
 }
 return $ret;
}

function SQL_Query_exec_cached($query, $cache_time  = 300, $cache_blank = 1)
{
 global $TTCache;

 $cache       = "queries/" . sha1($query);
 if (($rows        = $TTCache->Get($cache, $cache_time)) === false)
 {
  $res         = SQL_Query_exec($query);
  $rows        = array();
  while ($row         = mysql_fetch_assoc($res)) $rows[]             = $row;
  if (count($rows) || $cache_blank) $TTCache->Set($cache, $rows, $cache_time);
 }
 return count($rows) ? $rows : false;
}
?>


This will get you torrent table data back, encoded in JSON but that is where I didn't continue with it. I have JSON disabled on my extension, so if we decode this return data maybe it will kick in. Experiment with the object connection parameters until it clicks and works.

-> [url='https://www.php.net/manual/en/memcached.constants.php']Memcached Predefined Constants[/url]

In the meantime, it does work:

https://www.switch-torrents.com/testmemcached.php
2020-02-24 11:27:39
I got Memcached working completely after maybe 6 hours. Created a new torrents_cached() and replaced everything for torrenttable() on index.php with 1 line.

So.. after all this time today, it works great. I flushed the storage at every request which brought time to be very long, but reliably tested. Simple plugged my existing HTML into it, traced the array values from the md5 key => query and found out what all row values are, threw them in after writing a quick "cheat sheet" on the array values and after some testing on that for each value its pulling out at cache time, its pretty good :)

I honestly don't know what the point of this is with such small load for me, as if to say "hey I got it", but maybe someone can find it useful.

The challenge would be to apply it to a stock functions.php for the standard template. Makes sharing difficult when layout is too different.

Anyway, anyone want this function for their site? Will take maybe 1 hour to make compatible & write up quick instructions.
Unknown User
2020-02-24 12:50:19
Caching torrenttable will make humdreds of cache files per every search, every page, every category so thats what i still can't optimize to cache only linear torrent table without dynamic where requests or cache all searches inside 1 file only, not hundreds of them.

Thats why its bad to cache curuser, user profiles and torrenttable with current way of it

I did an experiment with 400 torrents to show up then cache them, load speed with cache was under 0.03 where without cache was 1.2
2020-02-24 16:17:05
That’s why you set the lifetime for the cache, I’m not understanding quite what you mean. This lines up with almost any memcached php tutorial on the web. Torrent table split the query process in half between the function and continued on with the rest of the execution on index.php. Remove whole block on index and write your own. The internal caching Is broken, and was never updated.

Caching little tid bits of data like a users age, or signature is a waste of resource and time, you want to target the highest hits like the torrents on the home page maybe even content on torrents-details.php
Unknown User
2020-02-24 16:22:52
"STorrents2020, post: 67988, member: 20382":

That’s why you set the lifetime for the cache, I’m not understanding quite what you mean. This lines up with almost any memcached php tutorial on the web. Torrent table split the query process in half between the function and continued on with the rest of the execution on index.php. Remove whole block on index and write your own. The internal caching Is broken, and was never updated.

Caching little tid bits of data like a users age, or signature is a waste of resource and time, you want to target the highest hits like the torrents on the home page maybe even content on torrents-details.php

In cureent state if you set caching on torrent search and i search for "hfhrhhfj" it will be saved in cache even though no data is returned and so on.

Ettv found a way to cache search queriea and all torrents inside 1 big array rather than millions of cache files.

Those tutorials are not what i want to achieve, will try to come up with something and store everything within one big cache file which will be periodically updated for torrenttable
2020-02-24 16:45:15
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>" : "") . "&nbsp;$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>" : "") . "&nbsp;<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>" : "") . "&nbsp;$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>" : "") . "&nbsp;<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)
Unknown User
2020-02-24 17:04:03
You are just caching linear queries files which is fine (without where name = $_get['search'] or category = $_get['cat'] or parent_cat, etc)

I am already making it to be a singular file and start caching files older than 5 minutes so the newly uploaded torrents are shown instantly from sql, rest of them from cache. Then it will dynamically add new cache file per every category and parent category with big array inside and everything which includes get search will be added into  1 big array in cache.

In total around 300 files will be in RAM which will memcached read instead of 100000. Ez performance boost
Users online

No users online

Search



Statistics

    Torrents

  • Tracking: 3
  • Todays torrents: 0
  • Seeders: 0
  • Leechers: 0
  • Peers: 0
  • Downloaded: 89.96 GB
  • Uploaded: 9.86 GB

  • Members

  • We have: 838
  • New today: 0
  • Guest today: 9

  • Online

  • Total online: 6
  • Members: 0
  • Guests online: 6
  • Comments: 3
  • Messages: 1,728