~tijlvdb/coral

28680d6a5b95d2b307523f2887768c7777fdbdff — Tijl Van den Brugghen 6 months ago c798e2c
Add full support for caching
M app/Controllers/StaticFiles.php => app/Controllers/StaticFiles.php +3 -2
@@ 6,8 6,9 @@ namespace Controllers;
use Controller;

/**
 * This Controller acts as a forwarder for static files. That said, it is only here as a fallback and shouldn't really
 * be used. The web server should take care of it instead (Nginx, Apache)
 * Acts as a forwarder for static files.
 *
 * See https://coral.tijlvdb.me/installation/static-files
 */
class StaticFiles extends Controller
{

M app/Controllers/WebhookReceiver.php => app/Controllers/WebhookReceiver.php +3 -0
@@ 120,6 120,9 @@ class WebhookReceiver extends Controller
			$this->error(500);
		}
		
		// Clear cache
		\Cache::instance()->reset();
		
		echo 'Success';
	}
	

M app/Helpers/Git.php => app/Helpers/Git.php +1 -0
@@ 1,4 1,5 @@
<?php
// SPDX-License-Identifier: AGPL-3.0-or-later

namespace Helpers;


M app/Helpers/Wiki.php => app/Helpers/Wiki.php +21 -3
@@ 14,12 14,20 @@ class Wiki extends \Prefab
	{
		$f3 = \Base::instance();
		$local_path = $this->resolvePagePath($path);
		$cache_key = 'cached_page_' . sha1($local_path);
		$full_path = $f3->get('config.data_dir') . $local_path;
		
		
		if ($local_path === null or substr($full_path, -3) !== '.md' or !is_file($full_path)) {
			throw new \Exception('Page not found');
		}
		
		// Try to get the value from cache
		if (($cached = $f3->get($cache_key)) and filemtime($full_path) === $cached['mtime']) {
			$cached['result']['cached'] = true;
			return $cached['result'];
		}
		
		$markdown = file_get_contents($full_path);
		
		$file_info = [];


@@ 28,12 36,21 @@ class Wiki extends \Prefab
			$file_info = $git->fileInformation($full_path);
		}
		
		return array_merge($file_info, [
		$result = array_merge($file_info, [
			'title' => $this->extractPageTitle($markdown),
			'file_path' => $local_path,
			'markdown' => $markdown,
			'html' => $this->renderPage($markdown)
			'html' => $this->renderPage($markdown),
			'cached' => false
		]);
		
		// Cache for a day
		$f3->set($cache_key, [
			'mtime' => filemtime($full_path),
			'result' => $result
		], 60 * 60 * 24);
		
		return $result;
	}
	
	/*


@@ 75,7 92,8 @@ class Wiki extends \Prefab
		return 'No title';
	}
	
	protected function renderPage(string $markdown) {
	protected function renderPage(string $markdown)
	{
		$parsedown = new ParsedownCheckbox();
		return $parsedown->parse($markdown);
	}

M config.example.php => config.example.php +7 -0
@@ 16,6 16,13 @@ return [
	'theme_dir' => __DIR__ . '/theme/',
	'git_dir' => false,
	
	// Caching configuration. Possible backends are folder, redis, memcache, memcached, apc, wincache, xcache
	//
	// Recommended: redis, memcached. Use folder if no other caching method is available.
	//
	// Don't forget the trailing slash!
	'cache' => 'folder=' . __DIR__ . '/tmp/cache/',
	
	// Settings for the automatic updates with Git and webhooks
	//
	// To disable, set 'git' to `false` or set an empty 'webhook_type'

M index.php => index.php +1 -0
@@ 13,6 13,7 @@ $f3->mset([

$f3->set('config', require 'config.php');
$f3->set('UI', $f3->get('config.theme_dir'));
$f3->set('CACHE', $f3->get('config.cache'));
$f3->set('ONERROR', function (Base $f3) {
	echo Template::instance()->render('error.html');
});

M theme/page.html => theme/page.html +1 -0
@@ 37,6 37,7 @@
                    <abbr title="{{ format_unix_time(@page.updated_at) }}">
                        {{ format_unix_time_diff(@page.updated_at) }} ago
                    </abbr>.
                    <check if="{{ @page.cached }}">Page served from cache.</check>
                </check>
            </footer>
        </div>