Чтение RSS каналов

Форум

Открыть Ответить

Кеширование блоков с sql-запросами для slaed cms

0

0
1
08.05.2012 18:41:2309.05.2012 18:47:49
В продолжение темы о кешировании блоков для системы slaed cms.


Плюсы:

Минимум изменнений в движке
Уменьшение скорости генерации страниц сайта
Уменьшение нагрузки на сервер

Минусы:

Думаю нет минусов )


Совместимость со всеми версиями слаед при наличии папки config/cache с правами 777


В файл function/function.php добавляем три новые функции:

/* запись кеширования html*/
function create_cache($prefix, $cache_text) {
$filename = "config/cache/".$prefix.".tmp";
$fp = fopen( $filename, 'wb+' );
fwrite( $fp, $cache_text );
fclose( $fp );
@chmod( $filename, 0666 );
return FALSE;
}

/* вызов кеширования html*/
function call_cache($prefix) {
$filename = "config/cache/".$prefix .".tmp";
if(file_exists($filename))return @file_get_contents( $filename );
return FALSE;	
}
/* удалнение кеша*/
function clear_cache($cache_area = false) {

$fdir = opendir('config/cache' );
while ( $file = readdir( $fdir ) ) {
if( $file != '.' and $file != '..' and $file != '.htaccess') {
if( $cache_area ) {
if( strpos( $file, $cache_area ) !== false ) @unlink( 'config/cache/'. $file );
} else {
@unlink( 'config/cache/'. $file );
}}}
closedir($fdir);}

Пример кеширования для блока block-news.php

<?php
# Copyright © 2005 - 2011 SLAED
# Website: http://www.slaed.net

if (!defined(&quot;BLOCK_FILE&quot;)) {
	header(&quot;Location: ../index.php&quot;);
	exit;
}

/* Начало изменений */
$content = call_cache(&quot;block-news&quot;);//вызов кеша
//если нет кеша создам новый
if(!$content){
/* конец изменений */

global $prefix, $db;
$strip = 25;
$result = $db->sql_query(&quot;SELECT sid, title FROM &quot;.$prefix.&quot;_news WHERE time <= now() AND status!='0' ORDER BY time DESC LIMIT 5&quot;);
while(list($sid, $title) = $db->sql_fetchrow($result)) {
	$linkstrip = cutstr($title, $strip);
	$content .= &quot;<table cellspacing=\\&quot;0\\&quot; cellpadding=\\&quot;1\\&quot; border=\\&quot;0\\&quot;><tr><td><img src=\\&quot;&quot;.img_find(&quot;all/news.png&quot;).&quot;\\&quot; title=\\&quot;&quot;.$title.&quot;\\&quot;></td><td><a href=\\&quot;news-view-.html&quot;.$sid.&quot;\\&quot; title=\\&quot;&quot;.$title.&quot;\\&quot;>&quot;.$linkstrip.&quot;</a></td></tr></table>&quot;;
}

/* Начало изменений */
//записываем в кеш
create_cache(&quot;block-news&quot;,$content);
}
/* Начало изменений */


?>	




Далее в файле modules/news/admin/index.php в функции function news_save

Над строкой

header(&quot;Location: &quot;.$admin_file.&quot;.php?op=news&quot;);


Вставляем

clear_cache(&quot;block-news&quot;);//удаляем кеш только для блока block-news.php


Или удаляем все из папки config/cache

clear_cache();


Итог: -1 запрос к DB

Источник http://pro-script.ru/index.php?name=news&op=view&id=406
3.76

3.76
Открыть Ответить
Хотите опробовать SLAED CMS в действии?

Технологии

PHP MySQL HTML 5 CSS 3 jQuery jQuery UI

Контакты

  • D-49179, Deutschland
    Ostercappeln, Im Siek 6
  • +49 176 61966679

  • https://slaed.net
Идеи и предложения
Обратная связь