Reduced page loads

- Entity of user currently logged in is kept serialized in session
- Post is retrieved only if necessary in thumbnail generator
This commit is contained in:
Marcin Kurczewski 2013-10-23 00:16:52 +02:00
parent 872780397d
commit e1acb8bd99
5 changed files with 22 additions and 13 deletions

View file

@ -6,7 +6,12 @@ class Bootstrap
$this->context->loggedIn = false; $this->context->loggedIn = false;
if (isset($_SESSION['user-id'])) if (isset($_SESSION['user-id']))
{ {
$this->context->user = R::findOne('user', 'id = ?', [$_SESSION['user-id']]); if (!isset($_SESSION['user']))
{
$dbUser = R::findOne('user', 'id = ?', [$_SESSION['user-id']]);
$_SESSION['user'] = serialize($dbUser);
}
$this->context->user = unserialize($_SESSION['user']);
if (!empty($this->context->user)) if (!empty($this->context->user))
{ {
$this->context->loggedIn = true; $this->context->loggedIn = true;

View file

@ -23,6 +23,7 @@ class AuthController
PrivilegesHelper::confirmEmail($dbUser); PrivilegesHelper::confirmEmail($dbUser);
$_SESSION['user-id'] = $dbUser->id; $_SESSION['user-id'] = $dbUser->id;
$_SESSION['user'] = serialize($dbUser);
\Chibi\UrlHelper::forward(\Chibi\UrlHelper::route('index', 'index')); \Chibi\UrlHelper::forward(\Chibi\UrlHelper::route('index', 'index'));
return $dbUser; return $dbUser;
} }

View file

@ -557,19 +557,19 @@ class PostController
/** /**
* Action that renders the thumbnail of the requested file and sends it to user. * Action that renders the thumbnail of the requested file and sends it to user.
* @route /post/{id}/thumb * @route /post/{name}/thumb
*/ */
public function thumbAction($id) public function thumbAction($name)
{ {
$this->context->layoutName = 'layout-file'; $this->context->layoutName = 'layout-file';
$post = Model_Post::locate($id);
PrivilegesHelper::confirmWithException(Privilege::ViewPost); $path = $this->config->main->thumbsPath . DS . $name;
PrivilegesHelper::confirmWithException(Privilege::ViewPost, PostSafety::toString($post->safety));
$path = $this->config->main->thumbsPath . DS . $post->name;
if (!file_exists($path)) if (!file_exists($path))
{ {
$post = Model_Post::locate($id);
PrivilegesHelper::confirmWithException(Privilege::ViewPost);
PrivilegesHelper::confirmWithException(Privilege::ViewPost, PostSafety::toString($post->safety));
$srcPath = $this->config->main->filesPath . DS . $post->name; $srcPath = $this->config->main->filesPath . DS . $post->name;
$dstPath = $path; $dstPath = $path;
$dstWidth = $this->config->browsing->thumbWidth; $dstWidth = $this->config->browsing->thumbWidth;
@ -623,17 +623,17 @@ class PostController
{ {
$path = $this->config->main->mediaPath . DS . 'img' . DS . 'thumb.png'; $path = $this->config->main->mediaPath . DS . 'img' . DS . 'thumb.png';
} }
if (isset($tmpPath))
unlink($tmpPath);
} }
if (!is_readable($path)) if (!is_readable($path))
throw new SimpleException('Thumbnail file is not readable'); throw new SimpleException('Thumbnail file is not readable');
$this->context->transport->cacheDaysToLive = 30; $this->context->transport->cacheDaysToLive = 30;
$this->context->transport->mimeType = 'image/png'; $this->context->transport->mimeType = 'image/png';
$this->context->transport->fileHash = 'thumb' . md5($post->file_hash . filemtime($path)); $this->context->transport->fileHash = 'thumb' . md5($name . filemtime($path));
$this->context->transport->filePath = $path; $this->context->transport->filePath = $path;
if (isset($tmpPath))
unlink($tmpPath);
} }

View file

@ -246,7 +246,10 @@ class UserController
R::store($user); R::store($user);
$this->context->transport->user = $user; $this->context->transport->user = $user;
if ($this->context->user->id == $user->id) if ($this->context->user->id == $user->id)
{
$this->context->user = $user; $this->context->user = $user;
unset($_SESSION['user']);
}
$this->context->transport->success = true; $this->context->transport->success = true;
} }
} }

View file

@ -1,6 +1,6 @@
<div class="post post-type-<?php echo TextHelper::camelCaseToHumanCase(PostType::toString($this->context->post->type)) ?>"> <div class="post post-type-<?php echo TextHelper::camelCaseToHumanCase(PostType::toString($this->context->post->type)) ?>">
<a href="<?php echo \Chibi\UrlHelper::route('post', 'view', ['id' => $this->context->post->id]) ?>"> <a href="<?php echo \Chibi\UrlHelper::route('post', 'view', ['id' => $this->context->post->id]) ?>">
<img class="thumb" src="<?php echo \Chibi\UrlHelper::route('post', 'thumb', ['id' => $this->context->post->id]) ?>" alt="@<?php echo $this->context->post->id ?>"/> <img class="thumb" src="<?php echo \Chibi\UrlHelper::route('post', 'thumb', ['name' => $this->context->post->name]) ?>" alt="@<?php echo $this->context->post->id ?>"/>
</a> </a>
<div class="info-bar"> <div class="info-bar">
<i class="icon-comments"></i> <span><?php echo $this->context->post->countOwn('comment') ?></span> <i class="icon-comments"></i> <span><?php echo $this->context->post->countOwn('comment') ?></span>