From 6ffaece83798f01a2b0559c2cfc0ca15c9e4e757 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Fri, 17 May 2013 16:25:45 +0200 Subject: [PATCH 1/4] allow caching to be more transparent and configurable --- public/include/autoloader.inc.php | 5 +- public/include/classes/debug.class.php | 2 +- public/include/classes/statistics.class.php | 123 ++++++++++++++++--- public/include/classes/statscache.class.php | 49 ++++++++ public/include/config/global.inc.dist.php | 7 ++ public/include/pages/statistics/pool.inc.php | 22 +--- public/include/pages/statistics/user.inc.php | 11 +- public/include/smarty_globals.inc.php | 52 ++------ public/templates/cache/README.md | 2 + 9 files changed, 182 insertions(+), 91 deletions(-) create mode 100644 public/include/classes/statscache.class.php create mode 100644 public/templates/cache/README.md diff --git a/public/include/autoloader.inc.php b/public/include/autoloader.inc.php index 4aeb78ef..0da83b59 100644 --- a/public/include/autoloader.inc.php +++ b/public/include/autoloader.inc.php @@ -2,6 +2,7 @@ require_once(CLASS_DIR . '/debug.class.php'); require_once(CLASS_DIR . '/bitcoin.class.php'); +require_once(CLASS_DIR . '/statscache.class.php'); require_once(INCLUDE_DIR . '/database.inc.php'); require_once(INCLUDE_DIR . '/smarty.inc.php'); // Load classes that need the above as dependencies @@ -12,7 +13,3 @@ require_once(CLASS_DIR . '/worker.class.php'); require_once(CLASS_DIR . '/statistics.class.php'); require_once(CLASS_DIR . '/transaction.class.php'); require_once(CLASS_DIR . '/settings.class.php'); - -// Use Memcache to store our data -$memcache = new Memcached(); -$memcache->addServer('localhost', 11211); diff --git a/public/include/classes/debug.class.php b/public/include/classes/debug.class.php index f0c549fc..362a926d 100644 --- a/public/include/classes/debug.class.php +++ b/public/include/classes/debug.class.php @@ -66,7 +66,7 @@ class Debug { } return $backtrace; } - + /** * We fill our data array here * @param string $msg Debug Message diff --git a/public/include/classes/statistics.class.php b/public/include/classes/statistics.class.php index 32fda095..1761ba39 100644 --- a/public/include/classes/statistics.class.php +++ b/public/include/classes/statistics.class.php @@ -4,21 +4,29 @@ if (!defined('SECURITY')) die('Hacking attempt'); + +/* + * We give access to plenty of statistics through this class + * Statistics should be non-intrusive and not change any + * rows in our database to ensure data integrity for the backend + **/ class Statistics { private $sError = ''; private $table = 'statistics_shares'; - public function __construct($debug, $mysqli, $config, $share, $user, $block) { + public function __construct($debug, $mysqli, $config, $share, $user, $block, $memcache) { $this->debug = $debug; $this->mysqli = $mysqli; $this->share = $share; $this->config = $config; $this->user = $user; $this->block = $block; + $this->memcache = $memcache; $this->debug->append("Instantiated Share class", 2); } - // get and set methods + /* Some basic get and set methods + **/ private function setErrorMessage($msg) { $this->sError = $msg; } @@ -35,7 +43,26 @@ class Statistics { return true; } + /** + * Another wrapper, we want to store data in memcache and return the actual data + * for further processing + * @param key string Our memcache key + * @param data mixed Our data to store in Memcache + * @param expiration time Our expiration time, see Memcached documentation + * @return data mixed Return our stored data unchanged + **/ + public function setCache($key, $data, $expiration=NULL) { + if ($this->config['memcache']['enabled']) $this->memcache->set($key, $data, $expiration); + return $data; + } + + /** + * Get our last $limit blocks found + * @param limit int Last limit blocks + * @return array + **/ public function getBlocksFound($limit=10) { + if ($data = $this->memcache->get(__FUNCTION__ . $limit)) return $data; $stmt = $this->mysqli->prepare(" SELECT b.*, a.username as finder FROM " . $this->block->getTableName() . " AS b @@ -43,11 +70,19 @@ class Statistics { ON b.account_id = a.id ORDER BY height DESC LIMIT ?"); if ($this->checkStmt($stmt) && $stmt->bind_param("i", $limit) && $stmt->execute() && $result = $stmt->get_result()) - return $result->fetch_all(MYSQLI_ASSOC); + return $this->setCache(__FUNCTION__ . $limit, $result->fetch_all(MYSQLI_ASSOC), 5); // Catchall $this->debug->append("Failed to find blocks:" . $this->mysqli->error); return false; } + + /** + * Currently the only function writing to the database + * Stored per block user statistics of valid and invalid shares + * @param aStats array Array with user id, valid and invalid shares + * @param iBlockId int Block ID as store in the Block table + * @return bool + **/ public function updateShareStatistics($aStats, $iBlockId) { $stmt = $this->mysqli->prepare("INSERT INTO $this->table (account_id, valid, invalid, block_id) VALUES (?, ?, ?, ?)"); if ($this->checkStmt($stmt) && $stmt->bind_param('iiii', $aStats['id'], $aStats['valid'], $aStats['invalid'], $iBlockId) && $stmt->execute()) return true; @@ -56,7 +91,14 @@ class Statistics { return false; } + /** + * Get our current pool hashrate for the past 10 minutes across both + * shares and shares_archive table + * @param none + * @return data object Return our hashrateas an object + **/ public function getCurrentHashrate() { + if ($data = $this->memcache->get(__FUNCTION__)) return $data; $stmt = $this->mysqli->prepare(" SELECT SUM(hashrate) AS hashrate FROM ( @@ -65,12 +107,18 @@ class Statistics { SELECT ROUND(COUNT(id) * POW(2, " . $this->config['difficulty'] . ")/600/1000) AS hashrate FROM " . $this->share->getArchiveTableName() . " WHERE time > DATE_SUB(now(), INTERVAL 10 MINUTE) ) AS sum"); // Catchall - if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result() ) return $result->fetch_object()->hashrate; + if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result() ) return $this->setCache(__FUNCTION__, $result->fetch_object()->hashrate); $this->debug->append("Failed to get hashrate: " . $this->mysqli->error); return false; } + /** + * Same as getCurrentHashrate but for Shares + * @param none + * @return data object Our share rate in shares per second + **/ public function getCurrentShareRate() { + if ($data = $this->memcache->get(__FUNCTION__)) return $data; $stmt = $this->mysqli->prepare(" SELECT ROUND(SUM(sharerate) / 600, 2) AS sharerate FROM ( @@ -78,13 +126,19 @@ class Statistics { UNION ALL SELECT COUNT(id) AS sharerate FROM " . $this->share->getArchiveTableName() . " WHERE time > DATE_SUB(now(), INTERVAL 10 MINUTE) ) AS sum"); - if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result() ) return $result->fetch_object()->sharerate; + if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result() ) return $this->setCache(__FUNCTION__, $result->fetch_object()->sharerate); // Catchall $this->debug->append("Failed to fetch share rate: " . $this->mysqli->error); return false; } + /** + * Get total shares for this round, since last block found + * @param none + * @return data array invalid and valid shares + **/ public function getRoundShares() { + if ($data = $this->memcache->get(__FUNCTION__)) return $data; $stmt = $this->mysqli->prepare(" SELECT ( SELECT IFNULL(count(id), 0) @@ -95,13 +149,20 @@ class Statistics { FROM " . $this->share->getTableName() . " WHERE UNIX_TIMESTAMP(time) >IFNULL((SELECT MAX(time) FROM blocks),0) AND our_result = 'N' ) as invalid"); - if ( $this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result() ) return $result->fetch_assoc(); + if ( $this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result() ) + return $this->setCache(__FUNCTION__, $result->fetch_assoc()); // Catchall $this->debug->append("Failed to fetch round shares: " . $this->mysqli->error); return false; } + /** + * Get amount of shares for a specific user + * @param account_id int User ID + * @return data array invalid and valid share counts + **/ public function getUserShares($account_id) { + if ($data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data; $stmt = $this->mysqli->prepare(" SELECT ( @@ -109,7 +170,7 @@ class Statistics { FROM " . $this->share->getTableName() . " AS s, " . $this->user->getTableName() . " AS u WHERE u.username = SUBSTRING_INDEX( s.username, '.', 1 ) - AND UNIX_TIMESTAMP(s.time) >IFNULL((SELECT MAX(b.time) FROM blocks AS b),0) + AND UNIX_TIMESTAMP(s.time) >IFNULL((SELECT MAX(b.time) FROM " . $this->block->getTableName() . " AS b),0) AND our_result = 'Y' AND u.id = ? ) AS valid, @@ -118,17 +179,24 @@ class Statistics { FROM " . $this->share->getTableName() . " AS s, " . $this->user->getTableName() . " AS u WHERE u.username = SUBSTRING_INDEX( s.username, '.', 1 ) - AND UNIX_TIMESTAMP(s.time) >IFNULL((SELECT MAX(b.time) FROM blocks AS b),0) + AND UNIX_TIMESTAMP(s.time) >IFNULL((SELECT MAX(b.time) FROM " . $this->block->getTableName() . " AS b),0) AND our_result = 'N' AND u.id = ? ) AS invalid"); - if ($stmt && $stmt->bind_param("ii", $account_id, $account_id) && $stmt->execute() && $result = $stmt->get_result()) return $result->fetch_assoc(); + if ($stmt && $stmt->bind_param("ii", $account_id, $account_id) && $stmt->execute() && $result = $stmt->get_result()) + return $this->setCache(__FUNCTION__ . $account_id, $result->fetch_assoc()); // Catchall $this->debug->append("Unable to fetch user round shares: " . $this->mysqli->error); return false; } + /** + * Same as getUserShares for Hashrate + * @param account_id integer User ID + * @return data integer Current Hashrate in khash/s + **/ public function getUserHashrate($account_id) { + if ($data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data; $stmt = $this->mysqli->prepare(" SELECT ROUND(COUNT(s.id) * POW(2, " . $this->config['difficulty'] . ")/600/1000) AS hashrate FROM " . $this->share->getTableName() . " AS s, @@ -136,13 +204,20 @@ class Statistics { WHERE u.username = SUBSTRING_INDEX( s.username, '.', 1 ) AND s.time > DATE_SUB(now(), INTERVAL 10 MINUTE) AND u.id = ?"); - if ($this->checkStmt($stmt) && $stmt->bind_param("i", $account_id) && $stmt->execute() && $result = $stmt->get_result() ) return $result->fetch_object()->hashrate; + if ($this->checkStmt($stmt) && $stmt->bind_param("i", $account_id) && $stmt->execute() && $result = $stmt->get_result() ) + return $this->setCache(__FUNCTION__ . $account_id, $result->fetch_object()->hashrate); // Catchall $this->debug->append("Failed to fetch hashrate: " . $this->mysqli->error); return false; } + /** + * Get hashrate for a specific worker + * @param worker_id int Worker ID to fetch hashrate for + * @return data int Current hashrate in khash/s + **/ public function getWorkerHashrate($worker_id) { + if ($data = $this->memcache->get(__FUNCTION__ . $worker_id)) return $data; $stmt = $this->mysqli->prepare(" SELECT ROUND(COUNT(s.id) * POW(2,21)/600/1000) AS hashrate FROM " . $this->share->getTableName() . " AS s, @@ -150,13 +225,21 @@ class Statistics { WHERE u.username = SUBSTRING_INDEX( s.username, '.', 1 ) AND s.time > DATE_SUB(now(), INTERVAL 10 MINUTE) AND u.id = ?"); - if ($this->checkStmt($stmt) && $stmt->bind_param("i", $account_id) && $stmt->execute() && $result = $stmt->get_result() ) return $result->fetch_object()->hashrate; + if ($this->checkStmt($stmt) && $stmt->bind_param("i", $account_id) && $stmt->execute() && $result = $stmt->get_result() ) + return $this->setCache(__FUNCTION__ . $worker_id, $result->fetch_object()->hashrate); // Catchall $this->debug->append("Failed to fetch hashrate: " . $this->mysqli->error); return false; } + /** + * get our top contributors for either shares or hashrate + * @param type string shares or hashes + * @param limit int Limit result to $limit + * @return data array Users with shares, account or hashrate, account + **/ public function getTopContributors($type='shares', $limit=15) { + if ($data = $this->memcache->get(__FUNCTION__ . $type . $limit)) return $data; switch ($type) { case 'shares': $stmt = $this->mysqli->prepare(" @@ -168,7 +251,7 @@ class Statistics { ORDER BY shares LIMIT ?"); if ($this->checkStmt($stmt) && $stmt->bind_param("i", $limit) && $stmt->execute() && $result = $stmt->get_result()) - return $result->fetch_all(MYSQLI_ASSOC); + return $this->setCache(__FUNCTION__ . $type . $limit, $result->fetch_all(MYSQLI_ASSOC)); $this->debug->append("Fetching shares failed: "); return false; break; @@ -183,14 +266,21 @@ class Statistics { GROUP BY account ORDER BY hashrate DESC LIMIT ?"); if ($this->checkStmt($stmt) && $stmt->bind_param("i", $limit) && $stmt->execute() && $result = $stmt->get_result()) - return $result->fetch_all(MYSQLI_ASSOC); + return $this->setCache(__FUNCTION__ . $type . $limit, $result->fetch_all(MYSQLI_ASSOC)); $this->debug->append("Fetching shares failed: "); return false; break; } } + /** + * get Hourly hashrate for a user + * Not working yet since I was not able to solve this via SQL queries + * @param account_id int User ID + * @return data array NOT FINISHED YET + **/ public function getHourlyHashrateByAccount($account_id) { + if ($data = $this->memcache->get(__FUNCTION__ . $account_id)) return $data; $stmt = $this->mysqli->prepare(" SELECT ROUND(COUNT(s.id) * POW(2, 12)/600/1000) AS hashrate, @@ -209,11 +299,12 @@ class Statistics { AND a.username = SUBSTRING_INDEX( s.username, '.', 1 ) AND a.id = ? GROUP BY HOUR(time)"); - if ($this->checkStmt($stmt) && $stmt->bind_param("ii", $account_id, $account_id) && $stmt->execute() && $hourlyhashrates = $stmt->get_result()) - return $hourlyhashrates->fetch_all(MYSQLI_ASSOC); + if ($this->checkStmt($stmt) && $stmt->bind_param("ii", $account_id, $account_id) && $stmt->execute() && $result = $stmt->get_result()) + return $this->setCache(__FUNCTION__ . $account_id, $result->fetch_all(MYSQLI_ASSOC), 3600); // Catchall $this->debug->append("Failed to fetch hourly hashrate: " . $this->mysqli->error); return false; } } -$statistics = new Statistics($debug, $mysqli, $config, $share, $user, $block); + +$statistics = new Statistics($debug, $mysqli, $config, $share, $user, $block, $memcache); diff --git a/public/include/classes/statscache.class.php b/public/include/classes/statscache.class.php new file mode 100644 index 00000000..7931da99 --- /dev/null +++ b/public/include/classes/statscache.class.php @@ -0,0 +1,49 @@ +config = $config; + $this->debug = $debug; + if (! $config['memcache']['enabled'] ) $this->debug->append("Not storing any values in memcache"); + return parent::__construct(); + } + + /** + * Wrapper around memcache->set + * Do not store values if memcache is disabled + **/ + public function set($key, $value, $expiration=NULL) { + if (! $this->config['memcache']['enabled']) return false; + if (empty($expiration)) + $expiration = $this->config['memcache']['expiration'] + rand( -$this->config['memcache']['splay'], $this->config['memcache']['splay']); + $this->debug->append("Stroing " . $this->config['memcache']['keyprefix'] . "$key with expiration of $expiration"); + return parent::set($this->config['memcache']['keyprefix'] . $key, $value, $expiration); + } + + /** + * Wrapper around memcache->get + * Always return false if memcache is disabled + **/ + public function get($key, $cache_cb = NULL, &$cas_token = NULL) { + if (! $this->config['memcache']['enabled']) return false; + $this->debug->append("Trying to fetch key " . $this->config['memcache']['keyprefix'] . "$key from cache", 3); + if ($data = parent::get($this->config['memcache']['keyprefix'].$key)) { + $this->debug->append("Found key in cache", 3); + return $data; + } else { + $this->debug->append("Key not found", 3); + } + } +} + +$memcache = new StatsCache($config, $debug); +$memcache->addServer($config['memcache']['host'], $config['memcache']['port']); diff --git a/public/include/config/global.inc.dist.php b/public/include/config/global.inc.dist.php index fead5b6c..88430965 100644 --- a/public/include/config/global.inc.dist.php +++ b/public/include/config/global.inc.dist.php @@ -28,6 +28,13 @@ $config = array( 'difficulty' => '31', // Target difficulty for this pool 'reward' => '50', // Reward for finding blocks 'confirmations' => '120', // Confirmations per block found to credit transactions + 'memcache' => array( + 'host' => 'localhost', // Memcache Host + 'post' => 11211, // Memcache Port + 'keyprefix' => 'mmcfe_ng_', // Prefix for all keys + 'expiration'=> '90', // Cache time + 'splay' => '15' // Splay time + ), 'wallet' => array( 'type' => 'http', // http or https are supported 'host' => 'localhost:9332', diff --git a/public/include/pages/statistics/pool.inc.php b/public/include/pages/statistics/pool.inc.php index 74c1d171..af0a4ebf 100644 --- a/public/include/pages/statistics/pool.inc.php +++ b/public/include/pages/statistics/pool.inc.php @@ -8,11 +8,11 @@ if (!defined('SECURITY')) if ($bitcoin->can_connect() === true){ if (!$dDifficulty = $memcache->get('dDifficulty')) { $dDifficulty = $bitcoin->query('getdifficulty'); - $memcache->set('dDifficulty', $dDifficulty, 60); + $memcache->set('dDifficulty', $dDifficulty); } if (!$iBlock = $memcache->get('iBlock')) { $iBlock = $bitcoin->query('getblockcount'); - $memcache->set('iBlock', $iBlock, 60); + $memcache->set('iBlock', $iBlock); } } else { $iDifficulty = 1; @@ -21,31 +21,17 @@ if ($bitcoin->can_connect() === true){ } // Top share contributors -if (!$aContributorsShares = $memcache->get('aContributorsShares')) { - $debug->append('STA Fetching contributor shares from database'); - $aContributorsShares = $statistics->getTopContributors('shares', 15); - $memcache->set('aContributorsShares', $aContributorsShares, 60); - $debug->append('END Fetching contributor shares from database'); -} +$aContributorsShares = $statistics->getTopContributors('shares', 15); // Top hash contributors -if (!$aContributorsHashes = $memcache->get('aContributorsHashes')) { - $debug->append('STA Fetching contributor hashes from database'); $aContributorsHashes = $statistics->getTopContributors('hashes', 15); - $memcache->set('aContributorsHashes', $aContributorsHashes, 60); - $debug->append('END Fetching contributor hashes from database'); -} // Grab the last 10 blocks found $aBlocksFoundData = $statistics->getBlocksFound(10); $aBlockData = $aBlocksFoundData[0]; // Estimated time to find the next block -if (!$iCurrentPoolHashrate = $memcache->get('iCurrentPoolHashrate')) { - $debug->append('Fetching iCurrentPoolHashrate from database'); - $iCurrentPoolHashrate = $statistics->getCurrentHashrate(); - $memcache->set('iCurrentPoolHashrate', $iCurrentPoolHashrate, 60); -} +$iCurrentPoolHashrate = $statistics->getCurrentHashrate(); // Time in seconds, not hours, using modifier in smarty to translate $iEstTime = $dDifficulty * pow(2,32) / ($iCurrentPoolHashrate * 1000); diff --git a/public/include/pages/statistics/user.inc.php b/public/include/pages/statistics/user.inc.php index e690fc80..41316beb 100644 --- a/public/include/pages/statistics/user.inc.php +++ b/public/include/pages/statistics/user.inc.php @@ -8,11 +8,11 @@ if (!defined('SECURITY')) if ($bitcoin->can_connect() === true){ if (!$dDifficulty = $memcache->get('dDifficulty')) { $dDifficulty = $bitcoin->query('getdifficulty'); - $memcache->set('dDifficulty', $dDifficulty, 60); + $memcache->set('dDifficulty', $dDifficulty); } if (!$iBlock = $memcache->get('iBlock')) { $iBlock = $bitcoin->query('getblockcount'); - $memcache->set('iBlock', $iBlock, 60); + $memcache->set('iBlock', $iBlock); } } else { $iDifficulty = 1; @@ -20,12 +20,7 @@ if ($bitcoin->can_connect() === true){ $_SESSION['POPUP'][] = array('CONTENT' => 'Unable to connect to pushpool service: ' . $bitcoin->can_connect(), 'TYPE' => 'errormsg'); } -if (!$aHourlyHashRates = $memcache->get('mmcfe_' . $_SESSION['USERDATA']['id'] . '_hourlyhashrate')) { - $debug->append('STA Fetching hourly hashrates from database'); - $aHourlyHashRates = $statistics->getHourlyHashrateByAccount($_SESSION['USERDATA']['id']); - $memcache->set('mmcfe_' . $_SESSION['USERDATA']['id'] . '_hourlyhashrate', $aHourlyHashRates, 600); - $debug->append('END Fetching hourly hashrates from database'); -} +$aHourlyHashRates = $statistics->getHourlyHashrateByAccount($_SESSION['USERDATA']['id']); // Propagate content our template $smarty->assign("YOURHASHRATES", $aHourlyHashRates); diff --git a/public/include/smarty_globals.inc.php b/public/include/smarty_globals.inc.php index 060aba83..a6cbcb3e 100644 --- a/public/include/smarty_globals.inc.php +++ b/public/include/smarty_globals.inc.php @@ -7,34 +7,11 @@ if (!defined('SECURITY')) // Globally available variables $debug->append('Global smarty variables', 3); -// Store some stuff in memcache prior to assigning it to Smarty -if (!$aRoundShares = $memcache->get('aRoundShares')) { - $debug->append('STA Fetching aRoundShares from database'); - $aRoundShares = $statistics->getRoundShares(); - $debug->append('END Fetching aRoundShares from database'); - $memcache->set('aRoundShares', $aRoundShares, 90); -} - -if (!$iCurrentActiveWorkers = $memcache->get('iCurrentActiveWorkers')) { - $debug->append('STA Fetching iCurrentActiveWorkers from database'); - $iCurrentActiveWorkers = $worker->getCountAllActiveWorkers(); - $debug->append('END Fetching iCurrentActiveWorkers from database'); - $memcache->set('iCurrentActiveWorkers', $iCurrentActiveWorkers, 80); -} - -if (!$iCurrentPoolHashrate = $memcache->get('iCurrentPoolHashrate')) { - $debug->append('STA Fetching iCurrentPoolHashrate from database'); - $iCurrentPoolHashrate = $statistics->getCurrentHashrate(); - $debug->append('END Fetching iCurrentPoolHashrate from database'); - $memcache->set('iCurrentPoolHashrate', $iCurrentPoolHashrate, 90); -} - -if (!$iCurrentPoolShareRate = $memcache->get('iCurrentPoolShareRate')) { - $debug->append('STA Fetching iCurrentPoolShareRate from database'); - $iCurrentPoolShareRate = $statistics->getCurrentShareRate(); - $debug->append('END Fetching iCurrentPoolShareRate from database'); - $memcache->set('iCurrentPoolShareRate', $iCurrentPoolShareRate, 90); -} +// Fetch some data +$aRoundShares = $statistics->getRoundShares(); +$iCurrentActiveWorkers = $worker->getCountAllActiveWorkers(); +$iCurrentPoolHashrate = $statistics->getCurrentHashrate(); +$iCurrentPoolShareRate = $statistics->getCurrentShareRate(); $aGlobal = array( 'slogan' => $config['website']['slogan'], @@ -47,26 +24,13 @@ $aGlobal = array( 'reward' => $config['reward'] ); -// We don't want the session infos cached +// We don't want these session infos cached $aGlobal['userdata'] = $_SESSION['USERDATA']['id'] ? $user->getUserData($_SESSION['USERDATA']['id']) : array(); - -// Balance should also not be cached $aGlobal['userdata']['balance'] = $transaction->getBalance($_SESSION['USERDATA']['id']); // Other userdata that we can cache savely -if (!$aGlobal['userdata']['shares'] = $memcache->get('global_' . $_SESSION['USERDATA']['id'] . '_shares')) { - $debug->append('STA Loading user shares from database'); - $aGlobal['userdata']['shares'] = $statistics->getUserShares($_SESSION['USERDATA']['id']); - $debug->append('END Loading user shares from database'); - $memcache->set('global_' . $_SESSION['USERDATA']['id'] . '_shares', $aGlobal['userdata']['shares'], 80); -} - -if (!$aGlobal['userdata']['hashrate'] = $memcache->get('global_' . $_SESSION['USERDATA']['id'] . '_hashrate') ) { - $debug->append('STA Loading user hashrate from database'); - $aGlobal['userdata']['hashrate'] = $statistics->getUserHashrate($_SESSION['USERDATA']['id']); - $debug->append('END Loading user hashrate from database'); - $memcache->set('global_' . $_SESSION['USERDATA']['id'] . '_hashrate', $aGlobal['userdata']['hashrate'], 70); -} +$aGlobal['userdata']['shares'] = $statistics->getUserShares($_SESSION['USERDATA']['id']); +$aGlobal['userdata']['hashrate'] = $statistics->getUserHashrate($_SESSION['USERDATA']['id']); // Make it available in Smarty $smarty->assign('PATH', 'site_assets/' . THEME); diff --git a/public/templates/cache/README.md b/public/templates/cache/README.md new file mode 100644 index 00000000..8b5701aa --- /dev/null +++ b/public/templates/cache/README.md @@ -0,0 +1,2 @@ +Please ensure the webserver has access to this folder to write the +caching templates. From 6102357cb926e195c365bdc7fdec51d573cebe49 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Fri, 17 May 2013 16:26:30 +0200 Subject: [PATCH 2/4] adding memcache switch to dist config --- public/include/config/global.inc.dist.php | 1 + 1 file changed, 1 insertion(+) diff --git a/public/include/config/global.inc.dist.php b/public/include/config/global.inc.dist.php index 88430965..4da05f0f 100644 --- a/public/include/config/global.inc.dist.php +++ b/public/include/config/global.inc.dist.php @@ -29,6 +29,7 @@ $config = array( 'reward' => '50', // Reward for finding blocks 'confirmations' => '120', // Confirmations per block found to credit transactions 'memcache' => array( + 'enabled' => true, 'host' => 'localhost', // Memcache Host 'post' => 11211, // Memcache Port 'keyprefix' => 'mmcfe_ng_', // Prefix for all keys From 6a2173d504aa144b9867c4f58bf495d596b0076c Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Fri, 17 May 2013 16:38:58 +0200 Subject: [PATCH 3/4] fixed typo, added debug level to key store --- public/include/classes/statscache.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/include/classes/statscache.class.php b/public/include/classes/statscache.class.php index 7931da99..b6884d61 100644 --- a/public/include/classes/statscache.class.php +++ b/public/include/classes/statscache.class.php @@ -25,7 +25,7 @@ class StatsCache extends Memcached { if (! $this->config['memcache']['enabled']) return false; if (empty($expiration)) $expiration = $this->config['memcache']['expiration'] + rand( -$this->config['memcache']['splay'], $this->config['memcache']['splay']); - $this->debug->append("Stroing " . $this->config['memcache']['keyprefix'] . "$key with expiration of $expiration"); + $this->debug->append("Storing " . $this->config['memcache']['keyprefix'] . "$key with expiration $expiration", 3); return parent::set($this->config['memcache']['keyprefix'] . $key, $value, $expiration); } From 7197a318b7b48612c773c47fbc360378149a2c9b Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Fri, 17 May 2013 16:41:28 +0200 Subject: [PATCH 4/4] adding more documentation --- README.md | 3 ++- public/include/config/global.inc.dist.php | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f6f60726..71daca7e 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,8 @@ Memcache Please install and start a default memcache instance. Not only would you need one for `pushpoold` but the statistics page is storing data in -`memcache` as well to improve performance. +`memcache` as well to improve performance. Your memcache can be +configured in the global configuration file (see below). Configuration ------------- diff --git a/public/include/config/global.inc.dist.php b/public/include/config/global.inc.dist.php index 4da05f0f..2e311300 100644 --- a/public/include/config/global.inc.dist.php +++ b/public/include/config/global.inc.dist.php @@ -25,9 +25,9 @@ $config = array( 'name' => 'The Pool', 'slogan' => 'Resistance is futile', ), - 'difficulty' => '31', // Target difficulty for this pool - 'reward' => '50', // Reward for finding blocks - 'confirmations' => '120', // Confirmations per block found to credit transactions + 'difficulty' => '31', // Target difficulty for this pool as set in pushpoold json + 'reward' => '50', // Reward for finding blocks, fixed value but changes someday + 'confirmations' => '120', // Confirmations per block needed to credit transactions 'memcache' => array( 'enabled' => true, 'host' => 'localhost', // Memcache Host