Решение
Основные идеи
- Простота.
- По возможности не обращаться к базе данных - страница из кеша отдаётся в несколько сотен раз быстрее, чем обычная.
- Кэшируем только анонимов. Если им разрешено что-либо публиковать - будут проблемы.
Реализация
1. Создать папку для кэша:
mkdir /tmp/cache
chmod 777 /tmp/cache
chmod 777 /tmp/cache
2. В index.php, в самом начале, до bootstrap
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
добавить:
Global $time1;
$time1 = microtime(1);
function logcache($s) {
Global $time1;
$log = fopen("/var/log/drupal-cache.log", 'a');
fwrite ($log, $s.', ' . ((int)(1000*(microtime(1) - $time1))) . " ms used\n");
fclose ($log);
}
$req = $_SERVER['REQUEST_URI'];
if (empty($_POST) && !isset($_COOKIE['drupal_uid']) && !strstr($req, '/admin') && !strstr($req, 'AJAX')) {
$f = '/tmp/cache/'.strtr($req, '/', '_');
if (file_exists($f) && time() - filemtime($f) < 13*60) {
readfile($f);
logcache ("+ $req served from cache");
exit;
} else {
ob_start();
}
} else {
$f = '';
}
$time1 = microtime(1);
function logcache($s) {
Global $time1;
$log = fopen("/var/log/drupal-cache.log", 'a');
fwrite ($log, $s.', ' . ((int)(1000*(microtime(1) - $time1))) . " ms used\n");
fclose ($log);
}
$req = $_SERVER['REQUEST_URI'];
if (empty($_POST) && !isset($_COOKIE['drupal_uid']) && !strstr($req, '/admin') && !strstr($req, 'AJAX')) {
$f = '/tmp/cache/'.strtr($req, '/', '_');
if (file_exists($f) && time() - filemtime($f) < 13*60) {
readfile($f);
logcache ("+ $req served from cache");
exit;
} else {
ob_start();
}
} else {
$f = '';
}
3. В самом конце, после drupal_page_footer():
Global $user;
if ($user->uid) setcookie('drupal_uid', $user->uid, time() + (60 * 60 * 24 * 30), '/');
Global $nofilecache;
if (!$nofilecache && $f) {
if (file_exists($f)) $cachetime = (int)((time() - filemtime($f))/60);
$fh = fopen($f, 'w');
fwrite($fh, ob_get_contents());
fclose($fh);
ob_end_flush();
if ($cachetime) logcache("- $req replaced - has $cachetime min old");
else logcache ("- $req created cache");
} else {
logcache ("* $req no cache, uid = ".$_COOKIE['drupal_uid']);
}
if ($user->uid) setcookie('drupal_uid', $user->uid, time() + (60 * 60 * 24 * 30), '/');
Global $nofilecache;
if (!$nofilecache && $f) {
if (file_exists($f)) $cachetime = (int)((time() - filemtime($f))/60);
$fh = fopen($f, 'w');
fwrite($fh, ob_get_contents());
fclose($fh);
ob_end_flush();
if ($cachetime) logcache("- $req replaced - has $cachetime min old");
else logcache ("- $req created cache");
} else {
logcache ("* $req no cache, uid = ".$_COOKIE['drupal_uid']);
}
4.
Чтобы не кешировать редиректы в includes/common.inc в начале функции drupal_goto:
Global $nofilecache;
$nofilecache = 1;
$nofilecache = 1;
5. В крон:
11 * * * * find /tmp/cache/ -mmin 15 -delete
Использованные материалы
Полезные ссылки
- Кеширование на файлах. Новая версия. Более тормозно но более гибко
- Кеширование на файлах - самая первая версия
Bookmark/Search this post with










