Admin

AdminAdmin is verified member.

Well-Known Member
Staff member
Administrator
Bạn tạo một thư mục tên là Cache để vào trong thư mục forum
PHP:
# Cache nội dung bài viết
  $file = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  $cachefile =  'Cache/' . md5($file) . '.html';
  if(file_exists($cachefile))
  {
    include($cachefile);
  } else {
    # code show nội dung

    $cached = fopen($cachefile, 'w');
    fwrite($cached, $content);
    fclose($cached);
  }
Bạn mở file show bài viết ra, tìm đoạn mà gọi là xuất ra nội dung bài viết ấy rồi chèn code trên vào
Giống như thế này
PHP:
    $content = htmlspecialchars($t['m']['text'],ENT_QUOTES,"UTF-8");
    $content = bbcode::tags($content);
    $content = nl2br($content);
    $content = ModPrince::ModBbcode($content,$t['t']['text']);
    echo $content;
Thì sẽ chèn như sau
PHP:
# Cache nội dung bài viết
  $file = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  $cachefile =  'Cache/' . md5($file) . '.html';
  if(file_exists($cachefile))
  {
    include($cachefile);
  } else {
    $content = htmlspecialchars($t['m']['text'],ENT_QUOTES,"UTF-8");
    $content = bbcode::tags($content);
    $content = nl2br($content);
    $content = ModPrince::ModBbcode($content,$t['t']['text']);
    echo $content;

    $cached = fopen($cachefile, 'w');
    fwrite($cached, $content);
    fclose($cached);
  }
Nếu như bản gốc johncms thì code show bài viết nó ở trong file post.php
PHP:
$text = htmlentities($res['text'], ENT_QUOTES, 'UTF-8');
$text = nl2br($text);
$text = bbcode::tags($text);
if ($set_user['smileys'])
    $text = functions::smileys($text, ($res['rights'] >= 1) ? 1 : 0);
echo $text;
Chỉ cần thay cái code này bằng code show bài viết của mình là được, thay xong nó được như thế này
Tập tin cần sửa là forum/includes/post.php
PHP:
# Cache nội dung bài viết
  $file = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  $cachefile =  'Cache/' . md5($file) . '.html';
  if(file_exists($cachefile))
  {
    include($cachefile);
  } else {
  
/
$text = htmlentities($res['text'], ENT_QUOTES, 'UTF-8');
$text = nl2br($text);
$text = bbcode::tags($text);
if ($set_user['smileys'])
    $text = functions::smileys($text, ($res['rights'] >= 1) ? 1 : 0);
echo $text;

    $cached = fopen($cachefile, 'w');
    fwrite($cached, $content);
    fclose($cached);
  }
Nguyên lí của nó là đầu tiên nó tìm xem đã tồn tại file cache nội dung chưa, nếu có thì nó chèn vào nếu không nó sẽ lưu nội dung vào cache rồi show nội dung ra
Demo ảnh
njdi.png

Đây là danh sách file cache trong thư mục cache
Sẽ cải thiện hiệu suất, giảm đáng kể các truy vấn tới mysql sever, host sẽ load nhanh hơn.
Chúc các bạn thành công!
Theo teenclub.info
 

Facebook Comments

Similar threads

Admin
Replies
0
Views
3K
AdminAdmin is verified member.
Admin
Admin
Replies
0
Views
10K
AdminAdmin is verified member.
Admin
ginbarca
Replies
0
Views
4K
ginbarca
ginbarca
Admin
Replies
0
Views
1K
AdminAdmin is verified member.
Admin
Admin
Replies
0
Views
907
AdminAdmin is verified member.
Admin
Admin
Replies
0
Views
1K
AdminAdmin is verified member.
Admin
Admin
Replies
0
Views
2K
AdminAdmin is verified member.
Admin
Admin
Replies
3
Views
7K
acaivippro
acaivippro
Back
Top