Skip to content

Commit

Permalink
File cache support
Browse files Browse the repository at this point in the history
File cache support. Any visited pages by anonymous user will generating
.cache file inside cache/page folder.
  • Loading branch information
danpros committed Jun 27, 2014
1 parent 58d0324 commit 16143d3
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 23 deletions.
2 changes: 2 additions & 0 deletions COPYRIGHT.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Features
- Lightbox
- User role
- Online backup
- File cache

Requirements
------------
Expand Down Expand Up @@ -107,7 +108,7 @@ server {
}
location / {
try_files $uri $uri/ /index.php;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
Expand Down
2 changes: 2 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
1 change: 1 addition & 0 deletions robots.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Disallow: /content/
Disallow: /system/
Disallow: /themes/
Disallow: /vendor/
Disallow: /cache/
# Files
Disallow: /changelog.txt
Disallow: /composer.json
Expand Down
121 changes: 115 additions & 6 deletions system/admin/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,15 @@ 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);

// The post URL
$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");
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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");
}
Expand All @@ -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');
Expand 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');
Expand Down Expand Up @@ -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");
}

Expand Down Expand Up @@ -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 changes: 12 additions & 0 deletions system/admin/views/clear-cache.html.php
Original file line number Diff line number Diff line change
@@ -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!';

?>
7 changes: 0 additions & 7 deletions system/admin/views/rebuilt-cache.html.php

This file was deleted.

42 changes: 36 additions & 6 deletions system/htmly.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
// 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');
Expand Down Expand Up @@ -99,6 +103,10 @@
// 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'];
Expand Down Expand Up @@ -140,6 +148,7 @@
'next' => has_next($next),
'type' => 'blogpost',
));

});

// Edit blog post
Expand Down Expand Up @@ -280,6 +289,10 @@
// 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');
Expand Down Expand Up @@ -560,6 +573,11 @@
die;
}
else {

if(!login()) {
file_cache($_SERVER['REQUEST_URI']);
}

$post = get_static_post($static);

if(!$post){
Expand Down Expand Up @@ -895,14 +913,14 @@
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 {
Expand All @@ -916,6 +934,10 @@
// 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');
Expand All @@ -942,6 +964,10 @@
// 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');
Expand Down Expand Up @@ -986,6 +1012,10 @@
// 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');
Expand Down
21 changes: 19 additions & 2 deletions system/includes/dispatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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) {
Expand Down Expand Up @@ -496,4 +513,4 @@ function dispatch() {

route(method(), "/{$uri}");
}
?>
?>
Loading

0 comments on commit 16143d3

Please sign in to comment.