mirror of
https://github.com/danpros/htmly.git
synced 2026-04-22 21:46:22 +05:30
Performance boost
Cache the widget and refresh it when any content edited or new content added.
This commit is contained in:
parent
ccd912b667
commit
ffb46f9981
3 changed files with 128 additions and 41 deletions
|
|
@ -801,7 +801,6 @@ get('/admin/popular', function () {
|
||||||
|
|
||||||
config('views.root', 'system/admin/views');
|
config('views.root', 'system/admin/views');
|
||||||
if ($role === 'admin') {
|
if ($role === 'admin') {
|
||||||
|
|
||||||
config('views.root', 'system/admin/views');
|
config('views.root', 'system/admin/views');
|
||||||
$page = from($_GET, 'page');
|
$page = from($_GET, 'page');
|
||||||
$page = $page ? (int)$page : 1;
|
$page = $page ? (int)$page : 1;
|
||||||
|
|
@ -1221,7 +1220,7 @@ get('/tag/:tag', function ($tag) {
|
||||||
|
|
||||||
$posts = get_tag($tag, $page, $perpage, false);
|
$posts = get_tag($tag, $page, $perpage, false);
|
||||||
|
|
||||||
$total = get_tagcount($tag, 'filename');
|
$total = get_tagcount($tag, 'basename');
|
||||||
|
|
||||||
if (empty($posts) || $page < 1) {
|
if (empty($posts) || $page < 1) {
|
||||||
// a non-existing page
|
// a non-existing page
|
||||||
|
|
@ -1253,7 +1252,7 @@ get('/archive/:req', function ($req) {
|
||||||
|
|
||||||
$posts = get_archive($req, $page, $perpage);
|
$posts = get_archive($req, $page, $perpage);
|
||||||
|
|
||||||
$total = get_count($req, 'filename');
|
$total = get_count($req, 'basename');
|
||||||
|
|
||||||
if (empty($posts) || $page < 1) {
|
if (empty($posts) || $page < 1) {
|
||||||
// a non-existing page
|
// a non-existing page
|
||||||
|
|
|
||||||
|
|
@ -311,7 +311,7 @@ function render($view, $locals = null, $layout = null)
|
||||||
$dir = 'cache/page';
|
$dir = 'cache/page';
|
||||||
$cachefile = $dir . '/' . $c . '.cache';
|
$cachefile = $dir . '/' . $c . '.cache';
|
||||||
if (is_dir($dir) === false) {
|
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";
|
$layout = "{$view_root}/{$layout}.html.php";
|
||||||
|
|
||||||
header('Content-type: text/html; charset=utf-8');
|
header('Content-type: text/html; charset=utf-8');
|
||||||
|
|
||||||
ob_start();
|
ob_start();
|
||||||
|
$time = microtime();
|
||||||
|
$time = explode(' ', $time);
|
||||||
|
$time = $time[1] + $time[0];
|
||||||
|
$start = $time;
|
||||||
require $layout;
|
require $layout;
|
||||||
|
$time = microtime();
|
||||||
|
$time = explode(' ', $time);
|
||||||
|
$time = $time[1] + $time[0];
|
||||||
|
$finish = $time;
|
||||||
|
$total_time = round(($finish - $start), 4);
|
||||||
|
echo "\n" . '<!-- Dynamic page generated in '.$total_time.' seconds. -->';
|
||||||
if (!$login) {
|
if (!$login) {
|
||||||
if (!file_exists($cachefile)) {
|
if (!file_exists($cachefile)) {
|
||||||
|
echo "\n" . '<!-- Cached page generated on '.date('Y-m-d H:i:s').' -->';
|
||||||
file_put_contents($cachefile, ob_get_contents());
|
file_put_contents($cachefile, ob_get_contents());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -112,7 +112,6 @@ function get_zip_files()
|
||||||
function get_draft_posts()
|
function get_draft_posts()
|
||||||
{
|
{
|
||||||
static $_draft = array();
|
static $_draft = array();
|
||||||
|
|
||||||
if (empty($_draft)) {
|
if (empty($_draft)) {
|
||||||
$tmp = array();
|
$tmp = array();
|
||||||
$tmp = glob('content/*/draft/*.md', GLOB_NOSORT);
|
$tmp = glob('content/*/draft/*.md', GLOB_NOSORT);
|
||||||
|
|
@ -129,7 +128,7 @@ function get_draft_posts()
|
||||||
// usort function. Sort by filename.
|
// usort function. Sort by filename.
|
||||||
function sortfile($a, $b)
|
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.
|
// usort function. Sort by date.
|
||||||
|
|
@ -188,6 +187,11 @@ function rebuilt_cache($type)
|
||||||
rebuilt_cache('subpage');
|
rebuilt_cache('subpage');
|
||||||
rebuilt_cache('author');
|
rebuilt_cache('author');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
foreach (glob('cache/widget/*.cache', GLOB_NOSORT) as $file) {
|
||||||
|
unlink($file);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return blog posts.
|
// Return blog posts.
|
||||||
|
|
@ -456,7 +460,7 @@ function get_tag($tag, $page, $perpage, $random)
|
||||||
$tmp = array();
|
$tmp = array();
|
||||||
|
|
||||||
foreach ($posts as $index => $v) {
|
foreach ($posts as $index => $v) {
|
||||||
$url = $v['filename'];
|
$url = $v['basename'];
|
||||||
$str = explode('_', $url);
|
$str = explode('_', $url);
|
||||||
$mtag = explode(',', rtrim($str[1], ','));
|
$mtag = explode(',', rtrim($str[1], ','));
|
||||||
$etag = explode(',', $tag);
|
$etag = explode(',', $tag);
|
||||||
|
|
@ -487,7 +491,7 @@ function get_archive($req, $page, $perpage)
|
||||||
$tmp = array();
|
$tmp = array();
|
||||||
|
|
||||||
foreach ($posts as $index => $v) {
|
foreach ($posts as $index => $v) {
|
||||||
$url = $v['filename'];
|
$url = $v['basename'];
|
||||||
$str = explode('_', $url);
|
$str = explode('_', $url);
|
||||||
if (strpos($str[0], "$req") !== false) {
|
if (strpos($str[0], "$req") !== false) {
|
||||||
$tmp[] = $v;
|
$tmp[] = $v;
|
||||||
|
|
@ -703,7 +707,7 @@ function get_keyword($keyword, $page, $perpage)
|
||||||
$words = explode(' ', $keyword);
|
$words = explode(' ', $keyword);
|
||||||
|
|
||||||
foreach ($posts as $index => $v) {
|
foreach ($posts as $index => $v) {
|
||||||
$arr = explode('_', $v['filename']);
|
$arr = explode('_', $v['basename']);
|
||||||
$filter = $arr[1] . ' ' . $arr[2];
|
$filter = $arr[1] . ' ' . $arr[2];
|
||||||
foreach ($words as $word) {
|
foreach ($words as $word) {
|
||||||
if (stripos($filter, $word) !== false) {
|
if (stripos($filter, $word) !== false) {
|
||||||
|
|
@ -814,7 +818,7 @@ function keyword_count($keyword)
|
||||||
$words = explode(' ', $keyword);
|
$words = explode(' ', $keyword);
|
||||||
|
|
||||||
foreach ($posts as $index => $v) {
|
foreach ($posts as $index => $v) {
|
||||||
$arr = explode('_', $v['filename']);
|
$arr = explode('_', $v['basename']);
|
||||||
$filter = $arr[1] . ' ' . $arr[2];
|
$filter = $arr[1] . ' ' . $arr[2];
|
||||||
foreach ($words as $word) {
|
foreach ($words as $word) {
|
||||||
if (strpos($filter, strtolower($word)) !== false) {
|
if (strpos($filter, strtolower($word)) !== false) {
|
||||||
|
|
@ -831,7 +835,6 @@ function keyword_count($keyword)
|
||||||
// Return recent posts lists
|
// Return recent posts lists
|
||||||
function recent_posts($custom = null, $count = null)
|
function recent_posts($custom = null, $count = null)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (empty($count)) {
|
if (empty($count)) {
|
||||||
$count = config('recent.count');
|
$count = config('recent.count');
|
||||||
if (empty($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)) {
|
if (!empty($custom)) {
|
||||||
return $posts;
|
return $posts;
|
||||||
|
|
@ -877,14 +900,39 @@ function popular_posts($custom = null, $count = null)
|
||||||
$_views = json_decode(file_get_contents($filename), true);
|
$_views = json_decode(file_get_contents($filename), true);
|
||||||
if(is_array($_views)) {
|
if(is_array($_views)) {
|
||||||
arsort($_views);
|
arsort($_views);
|
||||||
|
$i = 1;
|
||||||
foreach ($_views as $key => $val) {
|
foreach ($_views as $key => $val) {
|
||||||
if (file_exists($key)) {
|
if (file_exists($key)) {
|
||||||
if (strpos($key, 'blog') !== false) {
|
if (strpos($key, 'blog') !== false) {
|
||||||
$tmp[] = pathinfo($key);
|
$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)) {
|
if (empty($custom)) {
|
||||||
echo '<ul>';
|
echo '<ul>';
|
||||||
foreach ($posts as $post) {
|
foreach ($posts as $post) {
|
||||||
|
|
@ -922,37 +970,56 @@ function popular_posts($custom = null, $count = null)
|
||||||
// Return an archive list, categorized by year and month.
|
// Return an archive list, categorized by year and month.
|
||||||
function archive_list($custom = null)
|
function archive_list($custom = null)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
$dir = "cache/widget";
|
||||||
|
$filename = "cache/widget/archive.cache";
|
||||||
|
$ar = array();
|
||||||
|
|
||||||
|
if (is_dir($dir) === false) {
|
||||||
|
mkdir($dir, 0775, true);
|
||||||
|
}
|
||||||
|
|
||||||
$posts = get_post_unsorted();
|
$posts = get_post_unsorted();
|
||||||
$by_year = array();
|
$by_year = array();
|
||||||
$col = array();
|
$col = array();
|
||||||
|
|
||||||
if (!empty($posts)) {
|
if (!empty($posts)) {
|
||||||
|
|
||||||
|
if (!file_exists($filename)) {
|
||||||
|
foreach ($posts as $index => $v) {
|
||||||
|
|
||||||
foreach ($posts as $index => $v) {
|
$arr = explode('_', $v);
|
||||||
|
|
||||||
$arr = explode('_', $v);
|
// Replaced string
|
||||||
|
$str = $arr[0];
|
||||||
|
$replaced = substr($str, 0, strrpos($str, '/')) . '/';
|
||||||
|
|
||||||
// Replaced string
|
$date = str_replace($replaced, '', $arr[0]);
|
||||||
$str = $arr[0];
|
$data = explode('-', $date);
|
||||||
$replaced = substr($str, 0, strrpos($str, '/')) . '/';
|
$col[] = $data;
|
||||||
|
}
|
||||||
|
|
||||||
$date = str_replace($replaced, '', $arr[0]);
|
foreach ($col as $row) {
|
||||||
$data = explode('-', $date);
|
|
||||||
$col[] = $data;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($col as $row) {
|
$y = $row['0'];
|
||||||
|
$m = $row['1'];
|
||||||
$y = $row['0'];
|
$by_year[$y][] = $m;
|
||||||
$m = $row['1'];
|
}
|
||||||
$by_year[$y][] = $m;
|
|
||||||
|
$ar = serialize($by_year);
|
||||||
|
file_put_contents($filename, print_r($ar, true));
|
||||||
|
|
||||||
|
} else {
|
||||||
|
$by_year = unserialize(file_get_contents($filename));
|
||||||
}
|
}
|
||||||
|
|
||||||
# Most recent year first
|
# Most recent year first
|
||||||
krsort($by_year);
|
krsort($by_year);
|
||||||
|
|
||||||
# Iterate for display
|
# Iterate for display
|
||||||
$i = 0;
|
$i = 0;
|
||||||
$len = count($by_year);
|
$len = count($by_year);
|
||||||
|
|
||||||
if (empty($custom)) {
|
if (empty($custom)) {
|
||||||
foreach ($by_year as $year => $months) {
|
foreach ($by_year as $year => $months) {
|
||||||
if ($i == 0) {
|
if ($i == 0) {
|
||||||
|
|
@ -997,24 +1064,36 @@ EOF;
|
||||||
// Return tag cloud.
|
// Return tag cloud.
|
||||||
function tag_cloud($custom = null)
|
function tag_cloud($custom = null)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
$dir = "cache/widget";
|
||||||
|
$filename = "cache/widget/tags.cache";
|
||||||
|
$tg = array();
|
||||||
|
|
||||||
|
if (is_dir($dir) === false) {
|
||||||
|
mkdir($dir, 0775, true);
|
||||||
|
}
|
||||||
|
|
||||||
$posts = get_post_unsorted();
|
$posts = get_post_unsorted();
|
||||||
$tags = array();
|
$tags = array();
|
||||||
|
|
||||||
if (!empty($posts)) {
|
if (!empty($posts)) {
|
||||||
|
|
||||||
foreach ($posts as $index => $v) {
|
if (!file_exists($filename)) {
|
||||||
|
foreach ($posts as $index => $v) {
|
||||||
$arr = explode('_', $v);
|
$arr = explode('_', $v);
|
||||||
|
$data = rtrim($arr[1], ',');
|
||||||
$data = rtrim($arr[1], ',');
|
$mtag = explode(',', $data);
|
||||||
$mtag = explode(',', $data);
|
foreach ($mtag as $etag) {
|
||||||
foreach ($mtag as $etag) {
|
$tags[] = $etag;
|
||||||
$tags[] = $etag;
|
}
|
||||||
}
|
}
|
||||||
|
$tag_collection = array_count_values($tags);
|
||||||
|
ksort($tag_collection);
|
||||||
|
$tg = serialize($tag_collection);
|
||||||
|
file_put_contents($filename, print_r($tg, true));
|
||||||
|
} else {
|
||||||
|
$tag_collection = unserialize(file_get_contents($filename));
|
||||||
}
|
}
|
||||||
|
|
||||||
$tag_collection = array_count_values($tags);
|
|
||||||
ksort($tag_collection);
|
|
||||||
|
|
||||||
if(empty($custom)) {
|
if(empty($custom)) {
|
||||||
echo '<ul class="taglist">';
|
echo '<ul class="taglist">';
|
||||||
|
|
@ -2061,7 +2140,6 @@ function file_cache($request)
|
||||||
|
|
||||||
$c = str_replace('/', '#', str_replace('?', '~', $request));
|
$c = str_replace('/', '#', str_replace('?', '~', $request));
|
||||||
$cachefile = 'cache/page/' . $c . '.cache';
|
$cachefile = 'cache/page/' . $c . '.cache';
|
||||||
|
|
||||||
if (file_exists($cachefile)) {
|
if (file_exists($cachefile)) {
|
||||||
header('Content-type: text/html; charset=utf-8');
|
header('Content-type: text/html; charset=utf-8');
|
||||||
readfile($cachefile);
|
readfile($cachefile);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue