From ffb46f99813bc9664ad601653b3ef5922fccb1aa Mon Sep 17 00:00:00 2001 From: Danang Probo Sayekti Date: Mon, 17 Aug 2015 11:43:29 +0700 Subject: [PATCH] Performance boost Cache the widget and refresh it when any content edited or new content added. --- system/htmly.php | 5 +- system/includes/dispatch.php | 16 ++++- system/includes/functions.php | 148 ++++++++++++++++++++++++++++++++---------- 3 files changed, 128 insertions(+), 41 deletions(-) diff --git a/system/htmly.php b/system/htmly.php index 4160945..b8e49d5 100644 --- a/system/htmly.php +++ b/system/htmly.php @@ -801,7 +801,6 @@ get('/admin/popular', function () { config('views.root', 'system/admin/views'); if ($role === 'admin') { - config('views.root', 'system/admin/views'); $page = from($_GET, 'page'); $page = $page ? (int)$page : 1; @@ -1221,7 +1220,7 @@ get('/tag/:tag', function ($tag) { $posts = get_tag($tag, $page, $perpage, false); - $total = get_tagcount($tag, 'filename'); + $total = get_tagcount($tag, 'basename'); if (empty($posts) || $page < 1) { // a non-existing page @@ -1253,7 +1252,7 @@ get('/archive/:req', function ($req) { $posts = get_archive($req, $page, $perpage); - $total = get_count($req, 'filename'); + $total = get_count($req, 'basename'); if (empty($posts) || $page < 1) { // a non-existing page diff --git a/system/includes/dispatch.php b/system/includes/dispatch.php index 09906be..f1e2ee9 100644 --- a/system/includes/dispatch.php +++ b/system/includes/dispatch.php @@ -311,7 +311,7 @@ function render($view, $locals = null, $layout = null) $dir = 'cache/page'; $cachefile = $dir . '/' . $c . '.cache'; if (is_dir($dir) === false) { - mkdir($dir, 0777, true); + mkdir($dir, 0775, true); } } @@ -336,12 +336,22 @@ function render($view, $locals = null, $layout = null) $layout = "{$view_root}/{$layout}.html.php"; header('Content-type: text/html; charset=utf-8'); - + ob_start(); + $time = microtime(); + $time = explode(' ', $time); + $time = $time[1] + $time[0]; + $start = $time; require $layout; - + $time = microtime(); + $time = explode(' ', $time); + $time = $time[1] + $time[0]; + $finish = $time; + $total_time = round(($finish - $start), 4); + echo "\n" . ''; if (!$login) { if (!file_exists($cachefile)) { + echo "\n" . ''; file_put_contents($cachefile, ob_get_contents()); } } diff --git a/system/includes/functions.php b/system/includes/functions.php index bc40763..d26e947 100644 --- a/system/includes/functions.php +++ b/system/includes/functions.php @@ -112,7 +112,6 @@ function get_zip_files() function get_draft_posts() { static $_draft = array(); - if (empty($_draft)) { $tmp = array(); $tmp = glob('content/*/draft/*.md', GLOB_NOSORT); @@ -129,7 +128,7 @@ function get_draft_posts() // usort function. Sort by filename. function sortfile($a, $b) { - return $a['filename'] == $b['filename'] ? 0 : ($a['filename'] < $b['filename']) ? 1 : -1; + return $a['basename'] == $b['basename'] ? 0 : ($a['basename'] < $b['basename']) ? 1 : -1; } // usort function. Sort by date. @@ -188,6 +187,11 @@ function rebuilt_cache($type) rebuilt_cache('subpage'); rebuilt_cache('author'); } + + foreach (glob('cache/widget/*.cache', GLOB_NOSORT) as $file) { + unlink($file); + } + } // Return blog posts. @@ -456,7 +460,7 @@ function get_tag($tag, $page, $perpage, $random) $tmp = array(); foreach ($posts as $index => $v) { - $url = $v['filename']; + $url = $v['basename']; $str = explode('_', $url); $mtag = explode(',', rtrim($str[1], ',')); $etag = explode(',', $tag); @@ -487,7 +491,7 @@ function get_archive($req, $page, $perpage) $tmp = array(); foreach ($posts as $index => $v) { - $url = $v['filename']; + $url = $v['basename']; $str = explode('_', $url); if (strpos($str[0], "$req") !== false) { $tmp[] = $v; @@ -703,7 +707,7 @@ function get_keyword($keyword, $page, $perpage) $words = explode(' ', $keyword); foreach ($posts as $index => $v) { - $arr = explode('_', $v['filename']); + $arr = explode('_', $v['basename']); $filter = $arr[1] . ' ' . $arr[2]; foreach ($words as $word) { if (stripos($filter, $word) !== false) { @@ -814,7 +818,7 @@ function keyword_count($keyword) $words = explode(' ', $keyword); foreach ($posts as $index => $v) { - $arr = explode('_', $v['filename']); + $arr = explode('_', $v['basename']); $filter = $arr[1] . ' ' . $arr[2]; foreach ($words as $word) { if (strpos($filter, strtolower($word)) !== false) { @@ -831,7 +835,6 @@ function keyword_count($keyword) // Return recent posts lists function recent_posts($custom = null, $count = null) { - if (empty($count)) { $count = config('recent.count'); if (empty($count)) { @@ -839,7 +842,27 @@ function recent_posts($custom = null, $count = null) } } - $posts = get_posts(null, 1, $count); + $dir = "cache/widget"; + $filename = "cache/widget/recent.cache"; + $tmp = array(); + $posts = array(); + + if (is_dir($dir) === false) { + mkdir($dir, 0775, true); + } + + if (file_exists($filename)) { + $posts = unserialize(file_get_contents($filename)); + if (count($posts) != $count) { + $posts = get_posts(null, 1, $count); + $tmp = serialize($posts); + file_put_contents($filename, print_r($tmp, true)); + } + } else { + $posts = get_posts(null, 1, $count); + $tmp = serialize($posts); + file_put_contents($filename, print_r($tmp, true)); + } if (!empty($custom)) { return $posts; @@ -877,14 +900,39 @@ function popular_posts($custom = null, $count = null) $_views = json_decode(file_get_contents($filename), true); if(is_array($_views)) { arsort($_views); + $i = 1; foreach ($_views as $key => $val) { if (file_exists($key)) { if (strpos($key, 'blog') !== false) { $tmp[] = pathinfo($key); + if ($i++ >= $count) + break; } } } - $posts = get_posts($tmp, 1, $count); + + $dir = "cache/widget"; + $filecache = "cache/widget/popular.cache"; + $ar = array(); + $posts = array(); + + if (is_dir($dir) === false) { + mkdir($dir, 0775, true); + } + + if (file_exists($filecache)) { + $posts = unserialize(file_get_contents($filecache)); + if (count($posts) != $count) { + $posts = get_posts($tmp, 1, $count); + $ar = serialize($posts); + file_put_contents($filecache, print_r($ar, true)); + } + } else { + $posts = get_posts($tmp, 1, $count); + $ar = serialize($posts); + file_put_contents($filecache, print_r($ar, true)); + } + if (empty($custom)) { echo '