Cette étude donne une idée sur une nouvelle notion de la gestion de cache pas seulement basé sur le temps de raffraichissement mais aussi sur des actions volontaires liée à certaines modifications de la part de l'utilisateur.
Je suis actuellement en train de tester le fonctionnement de ces modifs.
L'intéret de ces manipulations ne peut se voir que si vous avez un cache pnrender important (3600 secondes, vois plus, pour tout dire je crois qu'on peut monter à 24 h, voir méme un cache illimité suivant l'espace disponible sur votre serveur)
But
- fournir un tutorial au personne qui veulent utiliser pleinement les options de cache de leur postnuke
- augmenter les temps d'affichage des forums en utilisant un systéme de cache (l'affichage des pages devient quasiment instané si elle est déja en cache)
Explication
Les pages les plus chargés sur un forum sont la page principale (main), la page d'affichage des topics d'un forum (viewforum) et la page de visualisation d'un sujet (viewtopic). Le but est donc de mettre en cache uniquement ces trois pages pour l'instant.
Pour que cette gestion puisse être pleinement fonctionnelle, il faut que le cache soit raffraichi quand :
- on poste un sujet
- on répond à un sujet
- on édite un sujet
- on éffectue des opérations d'administrations sur les sujets
Implémentation
ouvrir module/pnforum/pnuser.php
partie 1 : mise en cache des pages principales du forum
la fonction main est mise en cache avec un id correspondant à l'identifiant utilisateur
en effet, suivant votre statut (ou id) vous ne verrez pas la page principale de la méme maniére
, en particulier si vous etes admin , vous verrez vos forums privés alors que de utilisateurs standard ne le verront pas.
Code
function pnForum_user_main($args=array())
{
// get the input
if(count($args)>0) {
extract($args);
unset($args);
} else {
$viewcat = (int)pnVarCleanFromInput('viewcat');
$favorites = (bool)pnVarCleanFromInput('favorites');
}
$viewcat = (!empty($viewcat)) ? $viewcat : -1;
list($last_visit, $last_visit_unix) = pnModAPIFunc('pnForum', 'user', 'setcookies');
$pnr =& new pnRender('pnForum');
// edit mumu
// $pnr->caching = false;
$uid = pnUserGetVar('uid');
$pnr->cache_id = $uid ;
if ($pnr->is_cached('pnforum_user_main.html')) {
return $pnr->fetch('pnforum_user_main.html');
}
$loggedIn = pnUserLoggedIn();
if(pnModGetVar('pnForum', 'favorites_enabled')=='yes') {
if($loggedIn && empty($favorites)) {
$favorites = pnModAPIFunc('pnForum', 'user', 'get_favorite_status');
}
}
if ($loggedIn && $favorites) {
$tree = pnModAPIFunc('pnForum', 'user', 'getFavorites', array('user_id' => (int)pnUserGetVar('uid'),
'last_visit' => $last_visit ));
} else {
$tree = pnModAPIFunc('pnForum', 'user', 'readcategorytree', array('last_visit' => $last_visit ));
if(pnModGetVar('pnForum', 'slimforum') == 'yes') {
// this needs to be in here because we want to display the favorites
// not go to it if there is only one
// check if we have one category and one forum only
if(count($tree)==1) {
foreach($tree as $catname=>$forumarray) {
if(count($forumarray['forums'])==1) {
return pnRedirect(pnModURL('pnForum', 'user', 'viewforum', array('forum'=>$forumarray['forums'][0]['forum_id'])));
}
}
}
}
}
// edit mumu
// $pnr->caching = false;
$pnr->add_core_data();
$pnr->assign( 'favorites', $favorites);
$pnr->assign( 'tree', $tree);
$pnr->assign( 'view_category', $viewcat);
$pnr->assign( 'last_visit', $last_visit);
$pnr->assign( 'last_visit_unix', $last_visit_unix);
$pnr->assign( 'numposts', pnModAPIFunc('pnForum', 'user', 'boardstats',
array('id' => '0',
'type' => 'all' )));
return $pnr->fetch('pnforum_user_main.html');
}
{
// get the input
if(count($args)>0) {
extract($args);
unset($args);
} else {
$viewcat = (int)pnVarCleanFromInput('viewcat');
$favorites = (bool)pnVarCleanFromInput('favorites');
}
$viewcat = (!empty($viewcat)) ? $viewcat : -1;
list($last_visit, $last_visit_unix) = pnModAPIFunc('pnForum', 'user', 'setcookies');
$pnr =& new pnRender('pnForum');
// edit mumu
// $pnr->caching = false;
$uid = pnUserGetVar('uid');
$pnr->cache_id = $uid ;
if ($pnr->is_cached('pnforum_user_main.html')) {
return $pnr->fetch('pnforum_user_main.html');
}
$loggedIn = pnUserLoggedIn();
if(pnModGetVar('pnForum', 'favorites_enabled')=='yes') {
if($loggedIn && empty($favorites)) {
$favorites = pnModAPIFunc('pnForum', 'user', 'get_favorite_status');
}
}
if ($loggedIn && $favorites) {
$tree = pnModAPIFunc('pnForum', 'user', 'getFavorites', array('user_id' => (int)pnUserGetVar('uid'),
'last_visit' => $last_visit ));
} else {
$tree = pnModAPIFunc('pnForum', 'user', 'readcategorytree', array('last_visit' => $last_visit ));
if(pnModGetVar('pnForum', 'slimforum') == 'yes') {
// this needs to be in here because we want to display the favorites
// not go to it if there is only one
// check if we have one category and one forum only
if(count($tree)==1) {
foreach($tree as $catname=>$forumarray) {
if(count($forumarray['forums'])==1) {
return pnRedirect(pnModURL('pnForum', 'user', 'viewforum', array('forum'=>$forumarray['forums'][0]['forum_id'])));
}
}
}
}
}
// edit mumu
// $pnr->caching = false;
$pnr->add_core_data();
$pnr->assign( 'favorites', $favorites);
$pnr->assign( 'tree', $tree);
$pnr->assign( 'view_category', $viewcat);
$pnr->assign( 'last_visit', $last_visit);
$pnr->assign( 'last_visit_unix', $last_visit_unix);
$pnr->assign( 'numposts', pnModAPIFunc('pnForum', 'user', 'boardstats',
array('id' => '0',
'type' => 'all' )));
return $pnr->fetch('pnforum_user_main.html');
}
la fonction viewforum utilise aussi une gestion d'id basé sur l'id utilisateur, mais comme il y a plusieurs forums il faut générer un id qui soit une combinaison de l'id utilisateur et du numéro de forum (forum_id).
Code
function pnForum_user_viewforum($args=array())
{
// get the input
if(count($args)>0) {
extract($args);
unset($args);
} else {
$forum_id = (int)pnVarCleanFromInput('forum');
$start = (int)pnVarCleanFromInput('start');
}
list($last_visit, $last_visit_unix) = pnModAPIFunc('pnForum', 'user', 'setcookies');
$pnr =& new pnRender('pnForum');
// edit mumu
// $pnr->caching = false;
$uid = pnUserGetVar('uid');
$pnr->cache_id = $forum_id.$uid.$start ;
if ($pnr->is_cached('pnforum_user_viewforum.html')) {
return $pnr->fetch('pnforum_user_viewforum.html');
}
$forum = pnModAPIFunc('pnForum', 'user', 'readforum',
array('forum_id' => $forum_id,
'start' => $start,
'last_visit' => $last_visit,
'last_visit_unix' => $last_visit_unix));
$pnr->add_core_data();
$pnr->assign( 'forum', $forum);
$pnr->assign( 'hot_threshold', pnModGetVar('pnForum', 'hot_threshold'));
$pnr->assign( 'loggedin',$logged );
$pnr->assign( 'last_visit', $last_visit);
$pnr->assign( 'last_visit_unix', $last_visit_unix);
return $pnr->fetch('pnforum_user_viewforum.html');
}
{
// get the input
if(count($args)>0) {
extract($args);
unset($args);
} else {
$forum_id = (int)pnVarCleanFromInput('forum');
$start = (int)pnVarCleanFromInput('start');
}
list($last_visit, $last_visit_unix) = pnModAPIFunc('pnForum', 'user', 'setcookies');
$pnr =& new pnRender('pnForum');
// edit mumu
// $pnr->caching = false;
$uid = pnUserGetVar('uid');
$pnr->cache_id = $forum_id.$uid.$start ;
if ($pnr->is_cached('pnforum_user_viewforum.html')) {
return $pnr->fetch('pnforum_user_viewforum.html');
}
$forum = pnModAPIFunc('pnForum', 'user', 'readforum',
array('forum_id' => $forum_id,
'start' => $start,
'last_visit' => $last_visit,
'last_visit_unix' => $last_visit_unix));
$pnr->add_core_data();
$pnr->assign( 'forum', $forum);
$pnr->assign( 'hot_threshold', pnModGetVar('pnForum', 'hot_threshold'));
$pnr->assign( 'loggedin',$logged );
$pnr->assign( 'last_visit', $last_visit);
$pnr->assign( 'last_visit_unix', $last_visit_unix);
return $pnr->fetch('pnforum_user_viewforum.html');
}
la fonction viewtopic utiilse la méme particularité que la fonction viewforum en utilisant un fait le numéro de sujet au lieu du numéro de forum. au passage, (je l'ai pas dit dans les parties précédentes pour pas surcharger), on fait un test pour voir si la page est en cache au début et si c'est le cas on l'affiche, sinon ca sert à rien de faire les modifs ;).
Code
function pnForum_user_viewtopic($args=array())
{
// get the input
if(count($args)>0) {
extract($args);
unset($args);
} else {
$topic_id = (int)pnVarCleanFromInput('topic');
$start = (int)pnVarCleanFromInput('start');
$view = strtolower(pnVarCleanFromInput('view'));
}
list($last_visit, $last_visit_unix) = pnModAPIFunc('pnForum', 'user', 'setcookies');
$pnr =& new pnRender('pnForum');
$uid = pnUserGetVar('uid');
$pnr->cache_id = $topic_id.$uid.$start;
if ($pnr->is_cached('pnforum_user_viewtopic.html')) {
return $pnr->fetch('pnforum_user_viewtopic.html');
}
if(!empty($view) && ($view=="next" || $view=="previous")) {
$topic_id = pnModAPIFunc('pnForum', 'user', 'get_previous_or_next_topic_id',
array('topic_id' => $topic_id,
'view' => $view));
return pnRedirect(pnModURL('pnForum', 'user', 'viewtopic',
array('topic' => $topic_id)));
}
$topic = pnModAPIFunc('pnForum', 'user', 'readtopic',
array('topic_id' => $topic_id,
'start' => $start,
'last_visit' => $last_visit));
// keywords :
// edit mumu
//$pnr->caching = false;
$pnr->add_core_data();
$pnr->assign( 'topic', $topic);
$pnr->assign( 'post_count', count($topic['posts']));
$pnr->assign( 'hot_threshold', pnModGetVar('pnForum', 'hot_threshold'));
$pnr->assign( 'last_visit', $last_visit);
$pnr->assign( 'last_visit_unix', $last_visit_unix);
return $pnr->fetch('pnforum_user_viewtopic.html');
}
{
// get the input
if(count($args)>0) {
extract($args);
unset($args);
} else {
$topic_id = (int)pnVarCleanFromInput('topic');
$start = (int)pnVarCleanFromInput('start');
$view = strtolower(pnVarCleanFromInput('view'));
}
list($last_visit, $last_visit_unix) = pnModAPIFunc('pnForum', 'user', 'setcookies');
$pnr =& new pnRender('pnForum');
$uid = pnUserGetVar('uid');
$pnr->cache_id = $topic_id.$uid.$start;
if ($pnr->is_cached('pnforum_user_viewtopic.html')) {
return $pnr->fetch('pnforum_user_viewtopic.html');
}
if(!empty($view) && ($view=="next" || $view=="previous")) {
$topic_id = pnModAPIFunc('pnForum', 'user', 'get_previous_or_next_topic_id',
array('topic_id' => $topic_id,
'view' => $view));
return pnRedirect(pnModURL('pnForum', 'user', 'viewtopic',
array('topic' => $topic_id)));
}
$topic = pnModAPIFunc('pnForum', 'user', 'readtopic',
array('topic_id' => $topic_id,
'start' => $start,
'last_visit' => $last_visit));
// keywords :
// edit mumu
//$pnr->caching = false;
$pnr->add_core_data();
$pnr->assign( 'topic', $topic);
$pnr->assign( 'post_count', count($topic['posts']));
$pnr->assign( 'hot_threshold', pnModGetVar('pnForum', 'hot_threshold'));
$pnr->assign( 'last_visit', $last_visit);
$pnr->assign( 'last_visit_unix', $last_visit_unix);
return $pnr->fetch('pnforum_user_viewtopic.html');
}
version imprimable, pour la version imprimable, on ne met que l'identifiant du topic
remarquez que la fonction is_cached ne fonctionnera que si vous avez précisé l'id du cache avant.
Code
/**
* print
* prepare print view of the selected posting or topic
*
*/
function pnForum_user_print($args=array())
{
// get the input
if(count($args)>0) {
extract($args);
unset($args);
} else {
$post_id = (int)pnVarCleanFromInput('post');
$topic_id = (int)pnVarCleanFromInput('topic');
}
if(useragent_is_bot() == true) {
if($post_id <> 0 ) {
$topic_id =pnModAPIFunc('pnForum', 'user', 'get_topicid_by_postid',
array('post_id' => $post_id));
}
if(($topic_id <> 0) && ($topic_id<>false)) {
return pnForum_user_viewtopic(array('topic' => $topic_id,
'start' => 0));
} else {
return pnRedirect(pnModURL('pnForum', 'user', 'main'));
}
} else {
$pnr =& new pnRender('pnForum');
$pnr->cache_id = $topic_id; // idem pour post id
//$pnr->caching = false;
/* if($post_id<>0) {
$post = pnModAPIFunc('pnForum', 'user', 'readpost',
array('post_id' => $post_id));
$pnr->assign('post', $post);
$output = $pnr->fetch('pnforum_user_printpost.html');
} else
je ne gére pas l'impréssion d'un post dans un sujet, pour moi ca ne
sert à rien
*/
if($topic_id<>0) {
if ($pnr->is_cached('pnforum_user_printtopic.html')) {
return $pnr->fetch('pnforum_user_printtopic.html');
}
$topic = pnModAPIFunc('pnForum', 'user', 'readtopic',
array('topic_id' => $topic_id,
'complete' => true ));
$pnr->assign('topic', $topic);
$output = $pnr->fetch('pnforum_user_printtopic.html');
} else {
return pnRedirect(pnModURL('pnForum', 'user', 'main'));
}
echo "<html>\n";
echo "<head>\n";
echo "\r\n";
echo "\n";
global $additional_header;
if (is_array($additional_header))
{
foreach ($additional_header as $header)
echo "$header\n";
}
echo "</head>\n";
echo "<body >\n";
echo $output;
echo "</body>\n";
echo "</html>\n";
exit;
}
}
* prepare print view of the selected posting or topic
*
*/
function pnForum_user_print($args=array())
{
// get the input
if(count($args)>0) {
extract($args);
unset($args);
} else {
$post_id = (int)pnVarCleanFromInput('post');
$topic_id = (int)pnVarCleanFromInput('topic');
}
if(useragent_is_bot() == true) {
if($post_id <> 0 ) {
$topic_id =pnModAPIFunc('pnForum', 'user', 'get_topicid_by_postid',
array('post_id' => $post_id));
}
if(($topic_id <> 0) && ($topic_id<>false)) {
return pnForum_user_viewtopic(array('topic' => $topic_id,
'start' => 0));
} else {
return pnRedirect(pnModURL('pnForum', 'user', 'main'));
}
} else {
$pnr =& new pnRender('pnForum');
$pnr->cache_id = $topic_id; // idem pour post id
//$pnr->caching = false;
/* if($post_id<>0) {
$post = pnModAPIFunc('pnForum', 'user', 'readpost',
array('post_id' => $post_id));
$pnr->assign('post', $post);
$output = $pnr->fetch('pnforum_user_printpost.html');
} else
je ne gére pas l'impréssion d'un post dans un sujet, pour moi ca ne
sert à rien
*/
if($topic_id<>0) {
if ($pnr->is_cached('pnforum_user_printtopic.html')) {
return $pnr->fetch('pnforum_user_printtopic.html');
}
$topic = pnModAPIFunc('pnForum', 'user', 'readtopic',
array('topic_id' => $topic_id,
'complete' => true ));
$pnr->assign('topic', $topic);
$output = $pnr->fetch('pnforum_user_printtopic.html');
} else {
return pnRedirect(pnModURL('pnForum', 'user', 'main'));
}
echo "<html>\n";
echo "<head>\n";
echo "\r\n";
echo "\n";
global $additional_header;
if (is_array($additional_header))
{
foreach ($additional_header as $header)
echo "$header\n";
}
echo "</head>\n";
echo "<body >\n";
echo $output;
echo "</body>\n";
echo "</html>\n";
exit;
}
}
... je vais rédiger la partie 2 sur un autres posts, merci de patienter
modifié par : mumuri, 29 Jan 2006 - 21:31
Membre du PSR Project (Pagesetter replacement)
