1
0
Fork 0
mirror of https://github.com/Oreolek/oreolek.ru.git synced 2024-05-17 00:18:22 +03:00

Main page caching (for performance reasons)

This commit is contained in:
Alexander Yakovlev 2013-12-16 15:56:10 +07:00
parent 708c88085c
commit c02c11da55
8 changed files with 60 additions and 7 deletions

3
.gitmodules vendored
View file

@ -28,3 +28,6 @@
[submodule "modules/unittest"]
path = modules/unittest
url = git@github.com:kohana/unittest.git
[submodule "modules/cache"]
path = modules/cache
url = git@github.com:Oreolek/cache.git

View file

@ -114,9 +114,8 @@ Kohana::modules(array(
'debug-toolbar' => MODPATH.'debug-toolbar', // Debug toolbar
'image' => MODPATH.'image', // Image manipulation
'kostache' => MODPATH.'kostache', // Logic-less Mustache views
'sphinxql' => MODPATH.'sphinxql', // Full-text search via Sphinx
'unittest' => MODPATH.'unittest', // Unit testing
// 'cache' => MODPATH.'cache', // Caching with multiple backends
'cache' => MODPATH.'cache', // Caching with multiple backends
// 'codebench' => MODPATH.'codebench', // Benchmarking tool
));

View file

@ -50,10 +50,16 @@ class Controller_Post extends Controller_Layout {
public function action_index()
{
$this->template = new View_Index;
$page_size = Kohana::$config->load('common.page_size');
$this->template->items = ORM::factory('Post')
->where('is_draft', '=', '0')
->order_by('posted_at', 'DESC')
->limit($page_size)
->find_all();
$this->template->item_count = ORM::factory('Post')
->where('is_draft', '=', '0')
->order_by('posted_at', 'DESC')
->count_all();
}
/**
@ -61,13 +67,40 @@ class Controller_Post extends Controller_Layout {
**/
public function action_read()
{
$this->auto_render = FALSE;
$cache = Cache::instance('apcu');
if ($this->request->param('page') == 0)
{
$body = $cache->get('read_posts_0');
if (!empty($body))
{
$latest_change = Model_Post::get_latest_date();
if ($cache->get('latest_post') === $latest_change)
{
$this->response->body($body);
return;
}
}
$cache->delete('read_posts_0');
$cache->set('latest_post', $latest_change);
}
$this->template = new View_Read;
$this->template->title = 'Дневник';
$page_size = Kohana::$config->load('common.page_size');
$this->template->items = ORM::factory('Post')
->with_count('comments', 'comment_count')
->where('is_draft', '=', '0')
->order_by('posted_at', 'DESC')
->limit($page_size)
->find_all();
$this->template->item_count = ORM::factory('Post')
->where('is_draft', '=', '0')
->order_by('posted_at', 'DESC')
->count_all();
$renderer = Kostache_Layout::factory('layout');
$body = $renderer->render($this->template, $this->template->_view);
$cache->set('read_posts_0', $body, 60*60*24); //cache main page for 1 day
$this->response->body($body);
}
/**
@ -77,6 +110,7 @@ class Controller_Post extends Controller_Layout {
{
$this->template = new View_Index;
$this->template->title = 'Cвежие записи';
$this->template->item_count = 10;
$this->template->items = ORM::factory('Post')
->where('is_draft', '=', '0')
->order_by('posted_at', 'DESC')

View file

@ -1,6 +1,5 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');
class Model_Post extends ORM {
/**
* @return array validation rules
**/
@ -56,4 +55,10 @@ class Model_Post extends ORM {
$result = $db->query(Database::SELECT, 'SELECT id FROM `'.$table.'` WHERE MATCH('.$db->quote($term).') LIMIT 100');
return $result->as_array(NULL, 'id');
}
public static function get_latest_date()
{
$query = DB::select(array(DB::expr('MAX(`posted_at`)'), 'max_date'))->from('posts');
return $query->execute()->get('max_date');
}
}

View file

@ -17,6 +17,7 @@ class View_Index extends View_Layout {
* Items to show
**/
public $items = NULL;
public $item_count = 0;
/**
* Index description
**/
@ -30,7 +31,7 @@ class View_Index extends View_Layout {
public function get_paging()
{
$current_page = $this->get_current_page();
$item_count = count($this->items);
$item_count = $this->item_count;
$page_size = Kohana::$config->load('common.page_size');
$page_count = ceil($item_count / $page_size);
if ($page_count === 1.0)
@ -57,8 +58,11 @@ class View_Index extends View_Layout {
{
return 'Не найдено объектов для отображения.';
};
$items = $this->filter_items();
foreach ($items as $item)
if ($this->item_count === count($this->items))
{
$items = $this->filter_items();
}
foreach ($this->items as $item)
{
array_push($result, $this->show_item($item));
}

View file

@ -0,0 +1,8 @@
<?php defined('SYSPATH') or die('No direct script access.');
return array
(
'apcu' => array(
'driver' => 'apcu',
'default_expire' => 3600,
)
);

1
modules/cache Submodule

@ -0,0 +1 @@
Subproject commit 3b384fed3c4896910471895d9ec51dc4893d0e9a

@ -1 +0,0 @@
Subproject commit 0d6e016ffae2bd9da4a642bc5e6388efb2979d7b