Browse Source

File cache support

File cache support. Any visited pages by anonymous user will generating
.cache file inside cache/page folder.
pull/56/head v2.0
Danang Probo Sayekti 11 years ago
parent
commit
16143d33db
10 changed files with 203 additions and 23 deletions
  1. +2
    -0
      COPYRIGHT.txt
  2. +2
    -1
      README.md
  3. +2
    -0
      changelog.txt
  4. +1
    -0
      robots.txt
  5. +115
    -6
      system/admin/admin.php
  6. +12
    -0
      system/admin/views/clear-cache.html.php
  7. +0
    -7
      system/admin/views/rebuilt-cache.html.php
  8. +36
    -6
      system/htmly.php
  9. +19
    -2
      system/includes/dispatch.php
  10. +14
    -1
      system/includes/functions.php

+ 2
- 0
COPYRIGHT.txt View File

@ -35,3 +35,5 @@ license, including:
Pagedown modifications and bugfixes (c) 2009-2013 Stack Exchange Inc.
Lightbox2 (c) Lokesh Dhakar <lokeshdhakar.com>
jQuery (c) The jQuery Foundation

+ 2
- 1
README.md View File

@ -37,6 +37,7 @@ Features
- Lightbox
- User role
- Online backup
- File cache
Requirements
------------
@ -107,7 +108,7 @@ server {
}
location / {
try_files $uri $uri/ /index.php;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {


+ 2
- 0
changelog.txt View File

@ -1,3 +1,5 @@
2014-06-27: File cache support
2014-06-14: Foreign Char Support
2014-02-25: HTTPS Support
2014-02-15: HTMLy v1.2
2014-02-08: HTMLy v1.1.


+ 1
- 0
robots.txt View File

@ -24,6 +24,7 @@ Disallow: /content/
Disallow: /system/
Disallow: /themes/
Disallow: /vendor/
Disallow: /cache/
# Files
Disallow: /changelog.txt
Disallow: /composer.json


+ 115
- 6
system/admin/admin.php View File

@ -67,7 +67,7 @@ function edit_post($title, $tag, $url, $content, $oldfile, $destination = null)
$t = str_replace('-','',$dt);
$time = new DateTime($t);
$timestamp= $time->format("Y-m-d");
// The post date
$postdate = strtotime($timestamp);
@ -75,6 +75,7 @@ function edit_post($title, $tag, $url, $content, $oldfile, $destination = null)
$posturl = site_url().date('Y/m', $postdate).'/'.$post_url;
rebuilt_cache('all');
clear_post_cache($dt, $post_tag, $post_url, $newfile);
if ($destination == 'post') {
header("Location: $posturl");
@ -113,7 +114,7 @@ function edit_page($title, $url, $content, $oldfile, $destination = null) {
$posturl = site_url() . $post_url;
rebuilt_cache('all');
clear_page_cache($post_url);
if ($destination == 'post') {
header("Location: $posturl");
}
@ -150,7 +151,7 @@ function add_post($title, $tag, $url, $content, $user) {
}
rebuilt_cache('all');
clear_post_cache($post_date, $post_tag, $post_url, $dir . $filename);
$redirect = site_url() . 'admin/mine';
header("Location: $redirect");
}
@ -179,7 +180,7 @@ function add_page($title, $url, $content) {
}
rebuilt_cache('all');
clear_page_cache($post_url);
$redirect = site_url() . 'admin';
header("Location: $redirect");
}
@ -189,6 +190,13 @@ function add_page($title, $url, $content) {
// Delete blog post
function delete_post($file, $destination) {
$deleted_content = $file;
// Get cache file
$arr = explode('_', $file);
$replaced = substr($arr[0], 0,strrpos($arr[0], '/')) . '/';
$dt = str_replace($replaced,'',$arr[0]);
clear_post_cache($dt, $arr[1], str_replace('.md','',$arr[2]), $file);
if(!empty($deleted_content)) {
unlink($deleted_content);
rebuilt_cache('all');
@ -206,6 +214,18 @@ function delete_post($file, $destination) {
// Delete static page
function delete_page($file, $destination) {
$deleted_content = $file;
if (!empty($menu)) {
foreach(glob('cache/page/*.cache', GLOB_NOSORT) as $file) {
unlink($file);
}
}
else {
$replaced = substr($file, 0, strrpos($file, '/')) . '/';
$url = str_replace($replaced,'',$file);
clear_page_cache($url);
}
if(!empty($deleted_content)) {
unlink($deleted_content);
rebuilt_cache('all');
@ -273,8 +293,8 @@ function migrate($title, $time, $tags, $content, $url, $user, $source) {
mkdir($dir, 0777, true);
file_put_contents($dir . $filename, print_r($post_content, true));
}
rebuilt_cache('all');
$redirect = site_url() . 'admin/mine';
$redirect = site_url() . 'admin/clear-cache';
header("Location: $redirect");
}
@ -429,4 +449,93 @@ function get_backup_files () {
echo 'No available backup!';
}
}
}
function clear_post_cache($post_date, $post_tag, $post_url, $filename) {
$b = str_replace('/', '#', site_path() . '/');
$t = explode('-', $post_date);
$c = explode(',', $post_tag);
$p = 'cache/page/'.$b.$t[0].'#'.$t[1].'#'.$post_url.'.cache';
// Delete post
if (file_exists($p)) {
unlink($p);
}
// Delete homepage
$yd = 'cache/page/'.$b.'.cache';
if (file_exists($yd)) {
unlink($yd);
}
foreach(glob('cache/page/'.$b.'~*.cache', GLOB_NOSORT) as $file) {
unlink($file);
}
// Delete year
$yd = 'cache/page/'.$b.'archive#'.$t[0].'.cache';
if (file_exists($yd)) {
unlink($yd);
}
foreach(glob('cache/page/'.$b.'archive#'.$t[0].'~*.cache', GLOB_NOSORT) as $file) {
unlink($file);
}
// Delete year-month
$yd = 'cache/page/'.$b.'archive#'.$t[0].'-'.$t[1].'.cache';
if (file_exists($yd)) {
unlink($yd);
}
foreach(glob('cache/page/'.$b.'archive#'.$t[0].'-'.$t[1].'~*.cache', GLOB_NOSORT) as $file) {
unlink($file);
}
// Delete year-month-day
$yd = 'cache/page/'.$b.'archive#'.$t[0].'-'.$t[1].'-'.$t[2].'.cache';
if (file_exists($yd)) {
unlink($yd);
}
foreach(glob('cache/page/'.$b.'archive#'.$t[0].'-'.$t[1].'-'.$t[2].'~*.cache', GLOB_NOSORT) as $file) {
unlink($file);
}
// Delete tag
foreach($c as $tag) {
$yd = 'cache/page/'.$b.'tag#'.$tag.'.cache';
if (file_exists($yd)) {
unlink($yd);
}
foreach(glob('cache/page/'.$b.'tag#'.$tag.'~*.cache', GLOB_NOSORT) as $file) {
unlink($file);
}
}
// Delete search
foreach(glob('cache/page/'.$b.'search#*.cache', GLOB_NOSORT) as $file) {
unlink($file);
}
// Get cache post author
$arr = explode('_', $filename);
$replaced = substr($arr[0], 0,strrpos($arr[0], '/')) . '/';
$str = explode('/', $replaced);
$author = $str[count($str)-3];
// Delete author post list cache
$a = 'cache/page/'.$b.'author#'.$author.'.cache';
if (file_exists($a)) {
unlink($a);
}
foreach(glob('cache/page/'.$b.'author#'.$author.'~*.cache', GLOB_NOSORT) as $file) {
unlink($file);
}
}
function clear_page_cache($url) {
$b = str_replace('/', '#', site_path() . '/');
$p = 'cache/page/'.$b.$url.'.cache';
if (file_exists($p)) {
unlink($p);
}
}

+ 12
- 0
system/admin/views/clear-cache.html.php View File

@ -0,0 +1,12 @@
<?php
rebuilt_cache('all');
foreach(glob('cache/page/*.cache', GLOB_NOSORT) as $file) {
unlink($file);
}
echo 'All cache has been deleted!';
?>

+ 0
- 7
system/admin/views/rebuilt-cache.html.php View File

@ -1,7 +0,0 @@
<?php
rebuilt_cache('all');
echo 'All cache has been rebuilt!';
?>

+ 36
- 6
system/htmly.php View File

@ -18,6 +18,10 @@ config('source', 'config/config.ini');
// This will match the root url
get('/index', function () {
if(!login()) {
file_cache($_SERVER['REQUEST_URI']);
}
$page = from($_GET, 'page');
$page = $page ? (int)$page : 1;
$perpage = config('posts.perpage');
@ -99,6 +103,10 @@ post('/login', function() {
// The blog post page
get('/:year/:month/:name', function($year, $month, $name){
if(!login()) {
file_cache($_SERVER['REQUEST_URI']);
}
$post = find_post($year, $month, $name);
$current = $post['current'];
@ -140,6 +148,7 @@ get('/:year/:month/:name', function($year, $month, $name){
'next' => has_next($next),
'type' => 'blogpost',
));
});
// Edit blog post
@ -280,6 +289,10 @@ post('/:year/:month/:name/delete', function() {
// The author page
get('/author/:profile', function($profile){
if(!login()) {
file_cache($_SERVER['REQUEST_URI']);
}
$page = from($_GET, 'page');
$page = $page ? (int)$page : 1;
$perpage = config('profile.perpage');
@ -560,6 +573,11 @@ get('/:static', function($static){
die;
}
else {
if(!login()) {
file_cache($_SERVER['REQUEST_URI']);
}
$post = get_static_post($static);
if(!$post){
@ -895,14 +913,14 @@ get('/admin/backup-start',function(){
die;
});
// Create Zip file
get('/admin/rebuilt-cache',function(){
// Delete all cache
get('/admin/clear-cache',function(){
if(login()) {
config('views.root', 'system/admin/views');
render('rebuilt-cache', array(
'head_contents' => head_contents('Rebuilt cache started - ' . blog_title(), blog_description(), site_url()),
'bodyclass' => 'rebuiltcache',
'breadcrumb' => '<a href="' . site_url() . '">' .config('breadcrumb.home'). '</a> &#187; Rebuilt cache started'
render('clear-cache', array(
'head_contents' => head_contents('Clearing cache started - ' . blog_title(), blog_description(), site_url()),
'bodyclass' => 'clearcache',
'breadcrumb' => '<a href="' . site_url() . '">' .config('breadcrumb.home'). '</a> &#187; Clearing cache started'
));
}
else {
@ -916,6 +934,10 @@ get('/admin/rebuilt-cache',function(){
// The tag page
get('/tag/:tag',function($tag){
if(!login()) {
file_cache($_SERVER['REQUEST_URI']);
}
$page = from($_GET, 'page');
$page = $page ? (int)$page : 1;
$perpage = config('tag.perpage');
@ -942,6 +964,10 @@ get('/tag/:tag',function($tag){
// The archive page
get('/archive/:req',function($req){
if(!login()) {
file_cache($_SERVER['REQUEST_URI']);
}
$page = from($_GET, 'page');
$page = $page ? (int)$page : 1;
$perpage = config('archive.perpage');
@ -986,6 +1012,10 @@ get('/archive/:req',function($req){
// The search page
get('/search/:keyword', function($keyword){
if(!login()) {
file_cache($_SERVER['REQUEST_URI']);
}
$page = from($_GET, 'page');
$page = $page ? (int)$page : 1;
$perpage = config('search.perpage');


+ 19
- 2
system/includes/dispatch.php View File

@ -273,6 +273,15 @@ function content($value = null) {
function render($view, $locals = null, $layout = null) {
if(!login()) {
$c = str_replace('/', '#', str_replace('?', '~', $_SERVER['REQUEST_URI']));
$dir = 'cache/page';
$cachefile = $dir. '/' . $c . '.cache';
if(is_dir($dir) === false) {
mkdir($dir, 0777, true);
}
}
if (is_array($locals) && count($locals)) {
extract($locals, EXTR_SKIP);
}
@ -297,11 +306,19 @@ function render($view, $locals = null, $layout = null) {
ob_start();
require $layout;
echo trim(ob_get_clean());
if(!login()) {
if (!file_exists($cachefile)) {
file_put_contents($cachefile, ob_get_contents());
}
}
echo trim(ob_get_clean());
} else {
echo content();
}
}
function json($obj, $code = 200) {
@ -496,4 +513,4 @@ function dispatch() {
route(method(), "/{$uri}");
}
?>
?>

+ 14
- 1
system/includes/functions.php View File

@ -1685,8 +1685,21 @@ EOF;
echo '<li><a href="'.$base.'edit/profile">Edit profile</a></li>';
echo '<li><a href="'.$base.'admin/import">Import</a></li>';
echo '<li><a href="'.$base.'admin/backup">Backup</a></li>';
echo '<li><a href="'.$base.'admin/rebuilt-cache">Rebuilt cache</a></li>';
echo '<li><a href="'.$base.'admin/clear-cache">Clear cache</a></li>';
echo '<li><a href="'.$base.'logout">Logout</a></li>';
echo '</ul></div>';
}
// File cache
function file_cache($request) {
$c = str_replace('/', '#', str_replace('?', '~', $request));
$cachefile = 'cache/page/' . $c . '.cache';
if (file_exists($cachefile)) {
header('Content-type: text/html; charset=utf-8');
readfile($cachefile);
die;
}
}

Loading…
Cancel
Save