Adding cache detection to many pages

This will allow pages to skip loading data from backends like the
database or the wallet RPC server. If a cached page is detected and
valid, all dynamic content generation will be skipped completely.

Other pages that have not been adjusted in this commit will still fetch
backend data all the time. This will ensure clients always see the most
recent data, like worker information or account changes.

This should fix #309 completely but needs some testing.
This commit is contained in:
Sebastian Grewe 2013-07-06 18:36:11 +02:00
parent 3c426e913b
commit bffeea07c9
9 changed files with 130 additions and 88 deletions

View File

@ -2,10 +2,20 @@
// Make sure we are called from index.php
if (!defined('SECURITY')) die('Hacking attempt');
if ($user->isAuthenticated()) {
// Check user to ensure they are admin
if (!$user->isAuthenticated() || !$user->isAdmin($_SESSION['USERDATA']['id'])) {
header("HTTP/1.1 404 Page not found");
die("404 Page not found");
}
if (!$smarty->isCached('master.tpl', md5(serialize($_REQUEST)))) {
$debug->append('No cached version available, fetching from backend', 3);
$aTransactions = $transaction->getAllTransactions(@$_REQUEST['start']);
if (!$aTransactions) $_SESSION['POPUP'][] = array('CONTENT' => 'Could not find any transaction', 'TYPE' => 'errormsg');
$smarty->assign('TRANSACTIONS', $aTransactions);
$smarty->assign('CONTENT', 'default.tpl');
} else {
$debug->append('Using cached page', 3);
}
$smarty->assign('TRANSACTIONS', $aTransactions);
$smarty->assign('CONTENT', 'default.tpl');
?>

View File

@ -9,15 +9,22 @@ if (!$user->isAuthenticated() || !$user->isAdmin($_SESSION['USERDATA']['id'])) {
die("404 Page not found");
}
if ($bitcoin->can_connect() === true){
$dBalance = $bitcoin->query('getbalance');
if (!$smarty->isCached('master.tpl', md5(serialize($_REQUEST)))) {
$debug->append('No cached version available, fetching from backend', 3);
if ($bitcoin->can_connect() === true){
$dBalance = $bitcoin->query('getbalance');
} else {
$dBalance = 0;
$_SESSION['POPUP'][] = array('CONTENT' => 'Unable to connect to wallet RPC service: ' . $bitcoin->can_connect(), 'TYPE' => 'errormsg');
}
// Fetch locked balance from transactions
$dLockedBalance = $transaction->getLockedBalance();
} else {
$dBalance = 0;
$_SESSION['POPUP'][] = array('CONTENT' => 'Unable to connect to wallet RPC service: ' . $bitcoin->can_connect(), 'TYPE' => 'errormsg');
$debug->append('Using cached page', 3);
}
$smarty->assign("BALANCE", $dBalance);
$smarty->assign("LOCKED", $transaction->getLockedBalance());
$smarty->assign("LOCKED", $dLockedBalance);
// Tempalte specifics
$smarty->assign("CONTENT", "default.tpl");

View File

@ -6,13 +6,18 @@ if (!defined('SECURITY')) die('Hacking attempt');
// Include markdown library
use \Michelf\Markdown;
// Fetch active news to display
$aNews = $news->getAllActive();
if (is_array($aNews)) {
foreach ($aNews as $key => $aData) {
// Transform Markdown content to HTML
$aNews[$key]['content'] = Markdown::defaultTransform($aData['content']);
if (!$smarty->isCached('master.tpl', md5(serialize($_REQUEST)))) {
$debug->append('No cached version available, fetching from backend', 3);
// Fetch active news to display
$aNews = $news->getAllActive();
if (is_array($aNews)) {
foreach ($aNews as $key => $aData) {
// Transform Markdown content to HTML
$aNews[$key]['content'] = Markdown::defaultTransform($aData['content']);
}
}
} else {
$debug->append('Using cached page', 3);
}
// Load news entries for Desktop site and unauthenticated users

View File

@ -4,18 +4,23 @@
if (!defined('SECURITY'))
die('Hacking attempt');
if ($bitcoin->can_connect() === true){
$dDifficulty = $bitcoin->query('getdifficulty');
if (is_array($dDifficulty) && array_key_exists('proof-of-work', $dDifficulty))
$dDifficulty = $dDifficulty['proof-of-work'];
$iBlock = $bitcoin->query('getblockcount');
if (!$smarty->isCached('master.tpl', md5(serialize($_REQUEST)))) {
$debug->append('No cached version available, fetching from backend', 3);
if ($bitcoin->can_connect() === true){
$dDifficulty = $bitcoin->query('getdifficulty');
if (is_array($dDifficulty) && array_key_exists('proof-of-work', $dDifficulty))
$dDifficulty = $dDifficulty['proof-of-work'];
$iBlock = $bitcoin->query('getblockcount');
} else {
$dDifficulty = 1;
$iBlock = 0;
$_SESSION['POPUP'][] = array('CONTENT' => 'Unable to connect to litecoind RPC service: ' . $bitcoin->can_connect(), 'TYPE' => 'errormsg');
}
$smarty->assign("CURRENTBLOCK", $iBlock);
$smarty->assign("DIFFICULTY", $dDifficulty);
} else {
$dDifficulty = 1;
$iBlock = 0;
$_SESSION['POPUP'][] = array('CONTENT' => 'Unable to connect to litecoind RPC service: ' . $bitcoin->can_connect(), 'TYPE' => 'errormsg');
$debug->append('Using cached page', 3);
}
$smarty->assign("CURRENTBLOCK", $iBlock);
$smarty->assign("DIFFICULTY", $dDifficulty);
$smarty->assign("CONTENT", "default.tpl");
?>

View File

@ -4,12 +4,19 @@
if (!defined('SECURITY')) die('Hacking attempt');
// Grab the last blocks found
$iLimit = 20;
$aBlocksFoundData = $statistics->getBlocksFound($iLimit);
if (!$smarty->isCached('master.tpl', md5(serialize($_REQUEST)))) {
$debug->append('No cached version available, fetching from backend', 3);
// Grab the last blocks found
$iLimit = 20;
$aBlocksFoundData = $statistics->getBlocksFound($iLimit);
// Propagate content our template
$smarty->assign("BLOCKSFOUND", $aBlocksFoundData);
$smarty->assign("BLOCKLIMIT", $iLimit);
// Propagate content our template
$smarty->assign("BLOCKSFOUND", $aBlocksFoundData);
$smarty->assign("BLOCKLIMIT", $iLimit);
} else {
$debug->append('Using cached page', 3);
}
if ($config['website']['acl']['statistics']['blocks'] == 'public') {
$smarty->assign("CONTENT", "default.tpl");

View File

@ -1,16 +1,19 @@
<?php
// Make sure we are called from index.php
if (!defined('SECURITY'))
die('Hacking attempt');
if (!defined('SECURITY')) die('Hacking attempt');
if ($user->isAuthenticated()) {
$aHourlyHashRates = $statistics->getHourlyHashrateByAccount($_SESSION['USERDATA']['id']);
$aPoolHourlyHashRates = $statistics->getHourlyHashrateByPool();
if (!$smarty->isCached('master.tpl', md5(serialize($_REQUEST)))) {
$debug->append('No cached version available, fetching from backend', 3);
if ($user->isAuthenticated()) {
$aHourlyHashRates = $statistics->getHourlyHashrateByAccount($_SESSION['USERDATA']['id']);
$aPoolHourlyHashRates = $statistics->getHourlyHashrateByPool();
}
$smarty->assign("YOURHASHRATES", @$aHourlyHashRates);
$smarty->assign("POOLHASHRATES", @$aPoolHourlyHashRates);
} else {
$debug->append('Using cached page', 3);
}
// Propagate content our template
$smarty->assign("YOURHASHRATES", @$aHourlyHashRates);
$smarty->assign("POOLHASHRATES", @$aPoolHourlyHashRates);
$smarty->assign("CONTENT", "default.tpl");
?>

View File

@ -1,58 +1,63 @@
<?php
// Make sure we are called from index.php
if (!defined('SECURITY'))
die('Hacking attempt');
if (!defined('SECURITY')) die('Hacking attempt');
// Fetch data from wallet
if ($bitcoin->can_connect() === true){
$dDifficulty = $bitcoin->getdifficulty();
if (is_array($dDifficulty) && array_key_exists('proof-of-work', $dDifficulty))
$dDifficulty = $dDifficulty['proof-of-work'];
$iBlock = $bitcoin->getblockcount();
if (!$smarty->isCached('master.tpl', md5(serialize($_REQUEST)))) {
$debug->append('No cached version available, fetching from backend', 3);
// Fetch data from wallet
if ($bitcoin->can_connect() === true){
$dDifficulty = $bitcoin->getdifficulty();
if (is_array($dDifficulty) && array_key_exists('proof-of-work', $dDifficulty))
$dDifficulty = $dDifficulty['proof-of-work'];
$iBlock = $bitcoin->getblockcount();
} else {
$dDifficulty = 1;
$iBlock = 0;
$_SESSION['POPUP'][] = array('CONTENT' => 'Unable to connect to wallet RPC service: ' . $bitcoin->can_connect(), 'TYPE' => 'errormsg');
}
// Top share contributors
$aContributorsShares = $statistics->getTopContributors('shares', 15);
// Top hash contributors
$aContributorsHashes = $statistics->getTopContributors('hashes', 15);
// Grab the last 10 blocks found
$iLimit = 5;
$aBlocksFoundData = $statistics->getBlocksFound($iLimit);
count($aBlocksFoundData) > 0 ? $aBlockData = $aBlocksFoundData[0] : $aBlockData = array();
// Estimated time to find the next block
$iCurrentPoolHashrate = $statistics->getCurrentHashrate();
// Time in seconds, not hours, using modifier in smarty to translate
$iCurrentPoolHashrate > 0 ? $iEstTime = $dDifficulty * pow(2,32) / ($iCurrentPoolHashrate * 1000) : $iEstTime = 0;
// Time since last block
$now = new DateTime( "now" );
if (!empty($aBlockData)) {
$dTimeSinceLast = ($now->getTimestamp() - $aBlockData['time']);
} else {
$dTimeSinceLast = 0;
}
// Propagate content our template
$smarty->assign("ESTTIME", $iEstTime);
$smarty->assign("TIMESINCELAST", $dTimeSinceLast);
$smarty->assign("BLOCKSFOUND", $aBlocksFoundData);
$smarty->assign("BLOCKLIMIT", $iLimit);
$smarty->assign("CONTRIBSHARES", $aContributorsShares);
$smarty->assign("CONTRIBHASHES", $aContributorsHashes);
$smarty->assign("CURRENTBLOCK", $iBlock);
count($aBlockData) > 0 ? $smarty->assign("LASTBLOCK", $aBlockData['height']) : $smarty->assign("LASTBLOCK", 0);
$smarty->assign("DIFFICULTY", $dDifficulty);
$smarty->assign("REWARD", $config['reward']);
} else {
$dDifficulty = 1;
$iBlock = 0;
$_SESSION['POPUP'][] = array('CONTENT' => 'Unable to connect to wallet RPC service: ' . $bitcoin->can_connect(), 'TYPE' => 'errormsg');
$debug->append('Using cached page', 3);
}
// Top share contributors
$aContributorsShares = $statistics->getTopContributors('shares', 15);
// Top hash contributors
$aContributorsHashes = $statistics->getTopContributors('hashes', 15);
// Grab the last 10 blocks found
$iLimit = 5;
$aBlocksFoundData = $statistics->getBlocksFound($iLimit);
count($aBlocksFoundData) > 0 ? $aBlockData = $aBlocksFoundData[0] : $aBlockData = array();
// Estimated time to find the next block
$iCurrentPoolHashrate = $statistics->getCurrentHashrate();
// Time in seconds, not hours, using modifier in smarty to translate
$iCurrentPoolHashrate > 0 ? $iEstTime = $dDifficulty * pow(2,32) / ($iCurrentPoolHashrate * 1000) : $iEstTime = 0;
// Time since last block
$now = new DateTime( "now" );
if (!empty($aBlockData)) {
$dTimeSinceLast = ($now->getTimestamp() - $aBlockData['time']);
} else {
$dTimeSinceLast = 0;
}
// Propagate content our template
$smarty->assign("ESTTIME", $iEstTime);
$smarty->assign("TIMESINCELAST", $dTimeSinceLast);
$smarty->assign("BLOCKSFOUND", $aBlocksFoundData);
$smarty->assign("BLOCKLIMIT", $iLimit);
$smarty->assign("CONTRIBSHARES", $aContributorsShares);
$smarty->assign("CONTRIBHASHES", $aContributorsHashes);
$smarty->assign("CURRENTBLOCK", $iBlock);
count($aBlockData) > 0 ? $smarty->assign("LASTBLOCK", $aBlockData['height']) : $smarty->assign("LASTBLOCK", 0);
$smarty->assign("DIFFICULTY", $dDifficulty);
$smarty->assign("REWARD", $config['reward']);
// Public / private page detection
if ($config['website']['acl']['statistics']['pool'] == 'public') {
$smarty->assign("CONTENT", "authenticated.tpl");
} else if ($user->isAuthenticated() && $config['website']['acl']['statistics']['pool'] == 'private') {

View File

@ -2,7 +2,7 @@
// Make sure we are called from index.php
if (!defined('SECURITY'))
die('Hacking attempt');
die('Hacking attempt');
// Globally available variables
$debug->append('Global smarty variables', 3);
@ -109,7 +109,7 @@ if (@$_SESSION['USERDATA']['id']) {
// Site-wide notifications, based on user events
if ($aGlobal['userdata']['balance']['confirmed'] >= $config['ap_threshold']['max'])
$_SESSION['POPUP'][] = array('CONTENT' => 'You have exceeded the pools configured ' . $config['currency'] . ' warning threshold. Please initiate a transfer!', 'TYPE' => 'warning');
$_SESSION['POPUP'][] = array('CONTENT' => 'You have exceeded your accounts balance. Please transfer some ' . $config['currency'] . "!", 'TYPE' => 'errormsg');
if ($user->getUserFailed($_SESSION['USERDATA']['id']) > 0)
$_SESSION['POPUP'][] = array('CONTENT' => 'You have ' . $user->getUserFailed($_SESSION['USERDATA']['id']) . ' failed login attempts! <a href="?page=account&action=reset_failed">Reset Counter</a>', 'TYPE' => 'errormsg');
}

View File

@ -69,7 +69,7 @@
</div>
</div>
<div id="debug">
{include file="system/debugger.tpl"}
{nocache}{include file="system/debugger.tpl"}{/nocache}
</div>
</div>
</body>