09 Memcache Modification

Started by Mindless, July 20, 2012, 03:30:01 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

xblade

#7
Add to the config file

$TBDEV['expires']['latestuser'] = 0; // 0 = infinite
$TBDEV['expires']['MyPeers_'] = 120; // 60 = 60 seconds
$TBDEV['expires']['unread'] = 86400; // 86400 = 1 day
$TBDEV['expires']['alerts'] = 0;  // 0 = infinite
$TBDEV['expires']['user_cache'] = 900;  // 900 = 15 min
$TBDEV['expires']['forum_posts'] = 0;  // 900 = 15 min
$TBDEV['expires']['torrent_comments'] = 900;  // 900 = 15 min
$TBDEV['expires']['latestposts'] = 0;  // 900 = 15 min
$TBDEV['expires']['top5_torrents'] = 0; // 0 = infinite
$TBDEV['expires']['last5_torrents'] = 0; // 0 = infinite
$TBDEV['expires']['iphistory'] = 900;  // 900 = 15 min
$TBDEV['expires']['newpoll'] = 0;  // 900 = 15 min
$TBDEV['expires']['curuser'] = 900;  // 900 = 15 min
$TBDEV['expires']['genrelist'] = 30*86400; // 30x86400 = 30 days
$TBDEV['expires']['poll_data'] = 0; // 300 = 5 min
$TBDEV['expires']['torrent_data'] = 900; // 900 = 15 min
$TBDEV['expires']['user_flag'] = 86400*28; // 900 = 15 min
$TBDEV['expires']['shit_list'] = 900; // 900 = 15 min
$TBDEV['expires']['port_data'] = 900; // 900 = 15 min
$TBDEV['expires']['user_peers'] = 900; // 900 = 15 min
$TBDEV['expires']['user_friends'] = 900; // 900 = 15 min
$TBDEV['expires']['user_hash'] = 900; // 900 = 15 min
$TBDEV['expires']['hnr_data'] = 300; // 900 = 15 min
$TBDEV['expires']['snatch_data'] = 300; // 900 = 15 min
$TBDEV['expires']['user_snatches_data'] = 300; // 900 = 15 min
$TBDEV['expires']['staff_snatches_data'] = 300; // 900 = 15 min
$TBDEV['expires']['user_snatches_complete'] = 300; // 900 = 15 min
$TBDEV['expires']['completed_torrents'] = 300; // 300 = 5 min
$TBDEV['expires']['activeusers'] = 60; // 60 = 1 minutes
$TBDEV['expires']['last24'] = 3600; // 3600 = 1 hours
$TBDEV['expires']['activeircusers'] = 300; // 900 = 15 min
$TBDEV['expires']['birthdayusers'] = 43200; //== 43200 = 12 hours
$TBDEV['expires']['news_users'] = 3600; // 3600 = 1 hours
$TBDEV['expires']['user_invitees'] = 900; // 900 = 15 min
$TBDEV['expires']['ip_data'] = 900; // 900 = 15 min
$TBDEV['expires']['latesttorrents'] = 0;  // 0 = infinite
$TBDEV['expires']['invited_by'] = 900; // 900 = 15 min
$TBDEV['expires']['user_torrents'] = 900; // 900 = 15 min
$TBDEV['expires']['user_seedleech'] = 900; // 900 = 15 min
$TBDEV['expires']['radio'] = 0; // 0 = infinite
$TBDEV['expires']['total_funds'] = 0; // 0 = infinite
$TBDEV['expires']['latest_news'] = 0; // 0 = infinite
$TBDEV['expires']['site_stats'] = 300; // 300 = 5 min
$TBDEV['expires']['share_ratio'] = 900; // 900 = 15 min

      $res = sql_query('SELECT COUNT(torrent) As tcount, userid, seedbonus FROM peers LEFT JOIN users ON users.id = userid WHERE seeder = "yes" AND connectable = "yes" GROUP BY userid') or sqlerr(__FILE__, __LINE__);
      if (mysql_num_rows($res) > 0) {
        while ($arr = mysql_fetch_assoc($res)) {
            if ($arr['tcount'] >= 1000)
                $arr['tcount'] = 5;
            $users_buffer[] = '(' . $arr['userid'] . ',0.225 * ' . $arr['tcount'] . ')';
            $update['seedbonus'] = ($arr['seedbonus']+0.225*$arr['tcount']);
            $mc1->begin_transaction('MyUser_'.$arr['userid']);
    $mc1->update_row(false, array('seedbonus' => $update['seedbonus']));
        $mc1->commit_transaction(900);
        $mc1->begin_transaction('user'.$arr['userid']);
    $mc1->update_row(false, array('seedbonus' => $update['seedbonus']));
        $mc1->commit_transaction(900);

        }
        $count = count($users_buffer);
    if ($count > 0){
        sql_query("INSERT INTO users (id,seedbonus) VALUES " . implode(', ', $users_buffer) . " ON DUPLICATE key UPDATE seedbonus=seedbonus+values(seedbonus)") or sqlerr(__FILE__, __LINE__);
        write_log("Cleanup - ".$count." users received seedbonus");
        }
        unset ($users_buffer, $update, $count);
    }

tcboxa

I couldn't get this working for the life of me, inspected all the different versions and made sure memcache for php 5.6 was installed correctly along with dependencies & had it working with a sample script, however no matter what i do i always keept getting a PHP Fatal error:  Call to a member function begin_transaction() on null in /include/cleanup.php on line 200 warning and the page would crash when i ran a forced cleanup.

I even put require_once(CLASS_DIR.'class_cache.php'); & $mc1 = NEW CACHE(); in the cleanup script but it made no difference. I was testing it out on the seedbonus cleanup and used the memcache one from U232, it fails on $mc1->begin_transaction('MyUser_'.$arr['userid']);

The code used in the cleanup to test was as follows..
//=== Update karma seeding bonus... made nicer by devinkray :D
      //==   Updated and optimized by pdq :)
      //=== Using this will work for multiple torrents UP TO 5!... change the 5 to whatever... 1 to give the karma for only 1 torrent at a time, or 100 to make it unlimited (almost) your choice :P
      ///====== Seeding bonus per torrent
      $res = sql_query('SELECT COUNT(torrent) As tcount, userid, seedbonus FROM peers LEFT JOIN users ON users.id = userid WHERE seeder = "yes" AND connectable = "yes" GROUP BY userid') or sqlerr(__FILE__, __LINE__);
      if (mysql_num_rows($res) > 0) {
        while ($arr = mysql_fetch_assoc($res)) {
            if ($arr['tcount'] >= 1000)
                $arr['tcount'] = 5;
            $users_buffer[] = '(' . $arr['userid'] . ',0.225 * ' . $arr['tcount'] . ')';
            $update['seedbonus'] = ($arr['seedbonus']+0.225*$arr['tcount']);
            $mc1->begin_transaction('MyUser_'.$arr['userid']);
    $mc1->update_row(false, array('seedbonus' => $update['seedbonus']));
        $mc1->commit_transaction(900);
        $mc1->begin_transaction('user'.$arr['userid']);
    $mc1->update_row(false, array('seedbonus' => $update['seedbonus']));
        $mc1->commit_transaction(900);
       
        }
        $count = count($users_buffer);
    if ($count > 0){
        sql_query("INSERT INTO users (id,seedbonus) VALUES " . implode(', ', $users_buffer) . " ON DUPLICATE key UPDATE seedbonus=seedbonus+values(seedbonus)") or sqlerr(__FILE__, __LINE__);
        write_log("Cleanup - ".$count." users received seedbonus");
        }
        unset ($users_buffer, $update, $count);
    }


And i used the class_cache.php code in this thread which defiantly has the begin_transaction() function. I declared the $mc1 variable & require_once file in bittorrent.php and then in the cleanup. Googled for it and found a few possible solutions but unfortunately no luck. Its a shame because i spent a bit of time getting memcache going and was looking forward to seeing it in action.

I was also wondering where is MyUser_ coming from? I couldnt seem to find it in any of the U232 files.
Centos 7.4.1708 x86
Apache 2.4.29
PHP 7.0.14
MariaDB 10.1.29

Daniel

i will chek it out  i will run the site this hole wekkend

http://tracker.sourcebook.eu

what about announce.php do i the same there or is that a comple diffrent storry now that mine have no passkey :S

Mindless

Aye, to use it now you must install modifications that have memcache added, V1 section for now because its going to take a while to build the archives here.

Daniel

so if im correct in bittorent.php

it should look like this ?


require_once('include/class_cache.php');
require_once("include/config.php");
require_once("cleanup.php");
$mc1 = NEW CACHE();

Daniel

Thats the spirit mate Lol xD

Mindless

#1
Memcache your default Tbdev 09 source code...

Credits to pdq, dj grrr, gazelle and many others....

Ok here we go, to memcache your default Tbdev 09 source its really simple. First ensure your memcache install is correct and the memcache server is running. Once thats done this mod is a five minute job.

Upload class_cache.php to include folder :

Code (php) Select
<?php
/**
 *   https://github.com/Bigjoos/
 *   Licence Info: GPL
 *   Copyright (C) 2010 U-232 v.3
 *   A bittorrent tracker source based on TBDev.net/tbsource/bytemonsoon.
 *   Project Leaders: Mindless, putyn.
 **/

if (!extension_loaded('memcache')) {
    die(
'Memcache Extension not loaded.');
}

class 
CACHE extends Memcache {
    public 
$CacheHits = array();
    public 
$MemcacheDBArray = array();
    public 
$MemcacheDBKey '';
    protected 
$InTransaction false;
    public 
$Time 0;
    protected 
$Page = array();
    protected 
$Row 1;
    protected 
$Part 0;
    
    function 
__construct() {
        
$this->connect('127.0.0.1'11211);
    }
    
//---------- Caching functions ----------//
    // Wrapper for Memcache::set, with the zlib option removed and default duration of 1 hour
    
public function cache_value($Key$Value$Duration=2592000) {
        
$StartTime=microtime(true);
        if (empty(
$Key)) {
            
trigger_error("Cache insert failed for empty key");
        }
        if (!
$this->set($Key$Value0$Duration)) {
            
trigger_error("Cache insert failed for key $Key"E_USER_ERROR);
        }
        
$this->Time+=(microtime(true)-$StartTime)*1000;
    }
    public function 
add_value($Key$Value$Duration=2592000) {
        
$StartTime=microtime(true);
        if (empty(
$Key)) {
            
trigger_error("Cache insert failed for empty key");
        }
       
$add $this->add($Key$Value0$Duration);
       
$this->Time+=(microtime(true)-$StartTime)*1000;
       return 
$add;
    }
    public function 
get_value($Key$NoCache=false) {
        
$StartTime=microtime(true);
        if (empty(
$Key)) {
            
trigger_error("Cache retrieval failed for empty key");
        }
        
$Return $this->get($Key);
        
$this->Time+=(microtime(true)-$StartTime)*1000;
        return 
$Return;
    }
    public function 
replace_value($Key$Value$Duration=2592000) {
        
$StartTime=microtime(true);
        
$this->replace($Key$Valuefalse$Duration);
        
$this->Time+=(microtime(true)-$StartTime)*1000;
    }
    
// Wrapper for Memcache::delete. For a reason, see above.
    
public function delete_value($Key) {
        
$StartTime=microtime(true);
        if (empty(
$Key)) {
            
trigger_error("Cache retrieval failed for empty key");
        }
        if (!
$this->delete($Key,0)) {
        }
        
$this->Time+=(microtime(true)-$StartTime)*1000;
    }
    
//---------- memcachedb functions ----------//
    
public function begin_transaction($Key) {
        
$Value $this->get($Key);
        if (!
is_array($Value)) {
            
$this->InTransaction false;
            
$this->MemcacheDBKey = array();
            
$this->MemcacheDBKey '';
            return 
false;
        }
        
$this->MemcacheDBArray $Value;
        
$this->MemcacheDBKey $Key;
        
$this->InTransaction true;
        return 
true;
    }

    public function 
cancel_transaction() {
        
$this->InTransaction false;
        
$this->MemcacheDBKey = array();
        
$this->MemcacheDBKey '';
    }

    public function 
commit_transaction($Time=2592000) {
        if (!
$this->InTransaction) {
            return 
false;
        }
        
$this->cache_value($this->MemcacheDBKey$this->MemcacheDBArray$Time);
        
$this->InTransaction false;
    }
    
// Updates multiple rows in an array
    
public function update_transaction($Rows$Values) {
        if (!
$this->InTransaction) {
            return 
false;
        }
        
$Array $this->MemcacheDBArray;
        if (
is_array($Rows)) {
            
$i 0;
            
$Keys $Rows[0];
            
$Property $Rows[1];
            foreach (
$Keys as $Row) {
                
$Array[$Row][$Property] = $Values[$i];
                
$i++;
            }
        } else {
            
$Array[$Rows] = $Values;
        }
        
$this->MemcacheDBArray $Array;
    }
    
// Updates multiple values in a single row in an array
    // $Values must be an associative array with key:value pairs like in the array we're updating
    
public function update_row($Row$Values) {
        if (!
$this->InTransaction) {
            return 
false;
        }
        if (
$Row === false) {
            
$UpdateArray $this->MemcacheDBArray;
        } else {
            
$UpdateArray $this->MemcacheDBArray[$Row];
        }
        foreach (
$Values as $Key => $Value) {
            if (!
array_key_exists($Key$UpdateArray)) {
                
trigger_error('Bad transaction key ('.$Key.') for cache '.$this->MemcacheDBKey);
            }
            if (
$Value === '+1') {
                if (!
is_number($UpdateArray[$Key])) {
                    
trigger_error('Tried to increment non-number ('.$Key.') for cache '.$this->MemcacheDBKey);
                }
                ++
$UpdateArray[$Key]; // Increment value
            
} elseif ($Value === '-1') {
                if (!
is_number($UpdateArray[$Key])) {
                    
trigger_error('Tried to decrement non-number ('.$Key.') for cache '.$this->MemcacheDBKey);
                }
                --
$UpdateArray[$Key]; // Decrement value
            
} else {
                
$UpdateArray[$Key] = $Value// Otherwise, just alter value
            
}
        }
        if (
$Row === false) {
            
$this->MemcacheDBArray $UpdateArray;
        } else {
            
$this->MemcacheDBArray[$Row] = $UpdateArray;
        }
    }
public static function 
clean() {
if (!self::$this) {
trigger_error('Not connected to Memcache server in '.__METHOD__E_USER_WARNING);
return false;
}

self::$count++;
$time microtime(true);
$clean self::$link->flush();
self::$time += (microtime(true) - $time);

self::set_error(__METHOD__);
return $clean;
}
}
//end class
?>


Next you will include that file as a require once on the top of bittorrent.php :

Code (php) Select
//==Start memcache
require_once('include/class_cache.php');


That loads the class for use now. Next add this under it and your ready to rock and start adding memcache to your code.

Code (php) Select
$mc1 = NEW CACHE();

Simple as that, this post presumes you have memcache installed and running. Now you can add any modification done here that has memcache added.