mumuri
Franc-Maçon
enregistré depuis : oct. 2005
Messages : 639
dernière visite: 04.07.08
|
Voila par défaut, le cache est mis à "false" pour toutes les pages de pnForum, ce qui fait que vous ne bénificiez pas pleinement des options de cache des smarty. J'ai recodé une partie des fonctions pour mettre en place une gestion de cache de base fonctionnelle.
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
[size=large]partie 1 : mise en cache des pages principales du forum[/size]
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.
- 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');
- }
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).
- 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');
- }
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 ;).
- 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');
-
- }
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.
- /**
- * 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;
- }
- }
... je vais rédiger la partie 2 sur un autres posts, merci de patienter
modifié par : mumuri, 29 Jan 2006 - 21:31
|
mumuri
Franc-Maçon
enregistré depuis : oct. 2005
Messages : 639
dernière visite: 04.07.08
|
[size=large]partie 2 : raffraichissement du cache avec clear_cache[/size]
c'est bien joli de mettre en page des caches, mais l'intéret c'est de forcer leur raffraichissement, indépendamment du compteur de cache. c'est ce que nous allons faire
si nous repondons à un sujet et que nous validons la réponse, alors on éfface TOUT le cache (en effet pour l'instant on peut pas faire autrement car smarty ne le permet pas , enfin je crois)
- function pnForum_user_reply($args=array())
- {
- // get the input
- if(count($args)>0) {
- extract($args);
- unset($args);
- } else {
- list($topic_id,
- $post_id,
- $message,
- $attach_signature,
- $subscribe_topic,
- $preview,
- $submit,
- $cancel ) = pnVarCleanFromInput('topic',
- 'post',
- 'message',
- 'attach_signature',
- 'subscribe_topic',
- 'preview',
- 'submit',
- 'cancel');
- }
-
- $post_id = (int)$post_id;
- $topic_id = (int)$topic_id;
- $attach_signature = (int)$attach_signature;
- $subscribe_topic = (int)$subscribe_topic;
-
- /**
- * if cancel is submitted move to forum-view
- */
- if(!empty($cancel)) {
- return pnRedirect(pnModURL('pnForum', 'user', 'viewtopic', array('topic'=> $topic_id)));
- }
-
- $preview = (empty($preview)) ? false : true;
-
- if (empty($submit)) {
- $submit = false;
- $subject="";
- $message="";
- } else {
- $submit = true;
- }
-
- if ($submit==true && $preview==false) {
- // Confirm authorisation code
- if (!pnSecConfirmAuthKey()) {
- return showforumerror(_BADAUTHKEY, __FILE__, __LINE__);
- }
-
- // sync the users, so that new pn users get into the pnForum
- // database
- pnModAPIFunc('pnForum', 'user', 'usersync');
-
- list($start,$post_id ,$forum_id) = pnModAPIFunc('pnForum', 'user', 'storereply',
-
- array( 'topic_id' => $topic_id,
- 'message' => $message,
- 'attach_signature' => $attach_signature,
-
- 'subscribe_topic' => $subscribe_topic));
- // edit mumu
- $pnr =& new pnRender('pnForum');
- $pnr->clear_cache(null);
-
- return pnRedirect(pnModURL('pnForum', 'user', 'viewforum',
- array('forum' => $forum_id)));
- } else {
- list($last_visit, $last_visit_unix) = pnModAPIFunc('pnForum', 'user', 'setcookies');
- $reply = pnModAPIFunc('pnForum', 'user', 'preparereply',
- array('topic_id' => $topic_id,
- 'post_id' => $post_id,
- 'last_visit' => $last_visit,
- 'reply_start'=> empty($message),
- 'attach_signature' => $attach_signature,
- 'subscribe_topic' => $subscribe_topic));
- if($preview==true) {
- $reply['message'] = pnfVarPrepHTMLDisplay($message);
- }
-
- $pnr =& new pnRender('pnForum');
- // edit mumu
-
- $pnr->caching = false;
- $pnr->add_core_data();
- $pnr->assign( 'reply', $reply);
- $pnr->assign( 'preview', $preview);
- $pnr->assign( 'last_visit', $last_visit);
- $pnr->assign( 'last_visit_unix', $last_visit_unix);
- return $pnr->fetch('pnforum_user_reply.html');
- }
- }
on fait pareil à la création de nouveau sujet
- function pnForum_user_newtopic($args=array())
- {
- // get the input
- if(count($args)>0) {
- extract($args);
- unset($args);
- } else {
- list($forum_id,
- $message,
- $subject,
- $cancel,
- $submit,
- $attach_signature,
- $subscribe_topic,
- $preview) = pnVarCleanFromInput('forum',
- 'message',
- 'subject',
- 'cancel',
- 'submit',
- 'attach_signature',
- 'subscribe_topic',
- 'preview');
- }
-
- $preview = (empty($preview)) ? false : true;
- $cancel = (empty($cancel)) ? false : true;
- $submit = (empty($submit)) ? false : true;
-
- // if cancel is submitted move to forum-view
- if($cancel==true) {
- return pnRedirect(pnModURL('pnForum','user', 'viewforum', array('forum'=>$forum_id)));
- }
-
- if($submit==false) {
- $subject = '';
- $message = '';
- }
-
- list($last_visit, $last_visit_unix) = pnModAPIFunc('pnForum', 'user', 'setcookies');
-
- $newtopic = pnModAPIFunc('pnForum', 'user', 'preparenewtopic',
- array('forum_id' => $forum_id,
- 'subject' => $subject,
- 'message' => $message,
- 'topic_start'=> (empty($subject) && empty($message)),
- 'attach_signature' => $attach_signature,
- 'subscribe_topic' => $subscribe_topic));
- if($submit==true && $preview==false) {
- // sync the users, so that new pn users get into the pnForum
- // database
- pnModAPIFunc('pnForum', 'user', 'usersync');
-
- // it's a submitted page
- // Confirm authorisation code
- if (!pnSecConfirmAuthKey()) {
- return showforumerror(_BADAUTHKEY, __FILE__, __LINE__);
- }
-
- //store the new topic
- $topic_id = pnModAPIFunc('pnForum', 'user', 'storenewtopic',
- array('forum_id' => $forum_id,
- 'subject' => $subject,
- 'message' => $message,
- 'attach_signature' => $attach_signature,
- 'subscribe_topic' => $subscribe_topic));
- $pnr =& new pnRender('pnForum');
-
- $pnr->clear_cache(null);
- return pnRedirect(pnModURL('pnForum', 'user', 'viewforum',
- array('forum' => pnVarPrepForStore($forum_id))));
- } else {
- // new topic
- $pnr =& new pnRender('pnForum');
-
- $pnr->caching = false;
- $pnr->add_core_data();
- $pnr->assign( 'preview', $preview);
- $pnr->assign( 'newtopic', $newtopic);
- $pnr->assign( 'last_visit', $last_visit);
- $pnr->assign( 'last_visit_unix', $last_visit_unix);
- return $pnr->fetch('pnforum_user_newtopic.html');
- }
- }
à l'édition de post, on éfface tout aussi.
- /**
- * editpost
- *
- */
- function pnForum_user_editpost($args=array())
- {
- // get the input
- if(count($args)>0) {
- extract($args);
- unset($args);
- } else {
- list($post_id,
- $topic_id,
- $message,
- $subject,
- $submit,
- $delete,
- $cancel,
- $preview) = pnVarCleanFromInput('post',
- 'topic',
- 'message',
- 'subject',
- 'submit',
- 'delete',
- 'cancel',
- 'preview');
- }
-
- $preview = (empty($preview)) ? false : true;
-
- // if cancel is submitted move to forum-view
- if(!empty($cancel)) {
- return pnRedirect(pnModURL('pnForum','user', 'viewtopic', array('topic'=>$topic_id)));
- }
-
- if (empty($submit)) {
- $subject="";
- $message="";
- }
-
- list($last_visit, $last_visit_unix) = pnModAPIFunc('pnForum', 'user', 'setcookies');
-
- if($submit && !$preview) {
- /**
- * Confirm authorisation code
- */
- if (!pnSecConfirmAuthKey()) {
- return showforumerror(_BADAUTHKEY, __FILE__, __LINE__);
- }
- //store the new topic
- $redirect = pnModAPIFunc('pnForum', 'user', 'updatepost',
- array('post_id' => $post_id,
- 'delete' => $delete,
- 'subject' => $subject,
- 'message' => $message));
- // edit mumu
- $pnr =& new pnRender('pnForum');
- $pnr->clear_cache(null);
- return pnRedirect($redirect);
-
- } else {
- $post = pnModAPIFunc('pnForum', 'user', 'readpost',
- array('post_id' => $post_id));
- if(!empty($subject)) {
- $post['topic_subject'] = $subject;
- }
-
- // if the current user is the original poster we allow to
- // edit the subject
- $firstpost = pnModAPIFunc('pnForum', 'user', 'get_firstlast_post_in_topic',
- array('topic_id' => $post['topic_id'],
- 'first' => true));
- if($post['poster_data']['pn_uid'] = $firstpost['poster_data']['pn_uid']) {
- $post['edit_subject'] = true;
- }
-
- if(!empty($message)) {
- $post['post_text'] = $message;
- list($post['post_textdisplay']) = pnModCallHooks('item', 'transform', '', array($message));
- }
- $pnr =& new pnRender('pnForum');
- $pnr->caching = false;
- $pnr->add_core_data();
- $pnr->assign( 'preview', $preview);
- $pnr->assign( 'post', $post);
- $pnr->assign( 'last_visit', $last_visit);
- $pnr->assign( 'last_visit_unix', $last_visit_unix);
- return $pnr->fetch('pnforum_user_editpost.html');
- }
- }
... passons à la partie admin ...
chaque fois qu'on modére un sujet il faut aussi raffraichir le cache (genre si vous déplacer un sujet, ou si vous le découper en deux sous sujet etc ...)
- /**
- * topicadmin
- *
- */
- function pnForum_user_topicadmin($args=array())
- {
- // get the input
- if(count($args)>0) {
- extract($args);
- unset($args);
- } else {
- $topic_id = (int)pnVarCleanFromInput('topic');
- $post_id = (int)pnVarCleanFromInput('post');
- $forum_id = (int)pnVarCleanFromInput('forum'); // for move
- $mode = pnVarCleanFromInput('mode');
- $submit = pnVarCleanFromInput('submit');
- $shadow = pnVarCleanFromInput('createshadowtopic');
- }
- $shadow = (empty($shadow)) ? false : true;
-
- if(empty($topic_id) && !empty($post_id)) {
- $topic_id = pnModAPIFunc('pnForum', 'user', 'get_topicid_by_postid',
- array('post_id' => $post_id));
- }
- $topic = pnModAPIFunc('pnForum', 'user', 'readtopic',
- array('topic_id' => $topic_id));
- if($topic['access_moderate']<>true) {
- return showforumerror(_PNFORUM_NOAUTH_TOMODERATE, __FILE__, __LINE__);
- }
-
- $pnr =& new pnRender('pnForum');
- $pnr->caching = false;
- $pnr->add_core_data();
- $pnr->assign('mode', $mode);
- $pnr->assign('topic_id', $topic_id);
- $pnr->assign('last_visit', $last_visit);
- $pnr->assign('last_visit_unix', $last_visit_unix);
-
- if(empty($submit)) {
- switch($mode) {
- case "del":
- case "delete":
- $templatename = "pnforum_user_deletetopic.html";
- break;
- case "move":
- case "join":
- $pnr->assign('forums', pnModAPIFunc('pnForum', 'user', 'readuserforums'));
- $templatename = "pnforum_user_movetopic.html";
- break;
- case "lock":
- case "unlock":
- $templatename = "pnforum_user_locktopic.html";
- break;
- case "sticky":
- case "unsticky":
- $templatename = "pnforum_user_stickytopic.html";
- break;
- case "viewip":
- $pnr->assign('viewip', pnModAPIFunc('pnForum', 'user', 'get_viewip_data', array('post_id' => $post_id)));
- $templatename = "pnforum_user_viewip.html";
- break;
- default:
- return pnRedirect(pnModURL('pnForum', 'user', 'viewtopic', array('topic'=>$topic_id)));
- }
- return $pnr->fetch($templatename);
-
- } else { // submit is set
- $pnr->clear_cache(null); // edit : modération éffectuée on efface le cache
-
- if (!pnSecConfirmAuthKey()) {
- return showforumerror(_BADAUTHKEY, __FILE__, __LINE__);
- }
- switch($mode) {
- case "del":
- case "delete":
- $forum_id = pnModAPIFunc('pnForum', 'user', 'deletetopic', array('topic_id'=>$topic_id));
- return pnRedirect(pnModURL('pnForum', 'user', 'viewforum', array('forum'=>$forum_id)));
- break;
- case "move":
- pnModAPIFunc('pnForum', 'user', 'movetopic', array('topic_id' => $topic_id,
- 'forum_id' => $forum_id,
- 'shadow' => $shadow ));
- break;
- case "lock":
- case "unlock":
- pnModAPIFunc('pnForum', 'user', 'lockunlocktopic', array('topic_id'=> $topic_id, 'mode'=>$mode));
- break;
- case "sticky":
- case "unsticky":
- pnModAPIFunc('pnForum', 'user', 'stickyunstickytopic', array('topic_id'=> $topic_id, 'mode'=>$mode));
- break;
- case "join":
- $to_topic_id = pnVarCleanFromInput('to_topic_id');
- pnModAPIFunc('pnForum', 'user', 'jointopics', array('from_topic_id' => $topic_id,
- 'to_topic_id' => $to_topic_id));
- return pnRedirect(pnModURL('pnForum', 'user', 'viewtopic', array('topic' => $to_topic_id)));
- break;
- default:
- }
- return pnRedirect(pnModURL('pnForum', 'user', 'viewtopic', array('topic'=>$topic_id)));
- }
- }
- /**
- * splittopic
- *
- */
- function pnForum_user_splittopic($args=array())
- {
- // get the input
- if(count($args)>0) {
- extract($args);
- unset($args);
- } else {
- list($post_id,
- $submit,
- $newsubject) = pnVarCleanFromInput('post',
- 'submit',
- 'newsubject');
- }
-
|