Browse Source

cache_config preliminary release

pull/40/head
czaks 8 years ago
parent
commit
dc2928a14d
  1. 5
      inc/config.php
  2. 71
      inc/functions.php

5
inc/config.php

@ -137,6 +137,11 @@
// Tinyboard to use.
$config['cache']['redis'] = array('localhost', 6379, '', 1);
// EXPERIMENTAL: Should we cache configs? Warning: this changes board behaviour, i'd say, a lot.
// If you have any lambdas/includes present in your config, you should move them to instance-functions.php
// (this file will be explicitly loaded during cache hit, but not during cache miss).
$config['cache_config'] = false;
/*
* ====================
* Cookie settings

71
inc/functions.php

@ -50,15 +50,40 @@ $current_locale = 'en';
function loadConfig() {
global $board, $config, $__ip, $debug, $__version, $microtime_start, $current_locale;
global $board, $config, $__ip, $debug, $__version, $microtime_start, $current_locale, $events;
$error = function_exists('error') ? 'error' : 'basic_error_function_because_the_other_isnt_loaded_yet';
reset_events();
$boardsuffix = isset($board['uri']) ? $board['uri'] : '';
if (!isset($_SERVER['REMOTE_ADDR']))
$_SERVER['REMOTE_ADDR'] = '0.0.0.0';
if (file_exists('tmp/cache/cache_config.php')) {
require_once('tmp/cache/cache_config.php');
}
if (isset($config['cache_config']) &&
$config['cache_config'] &&
$config = Cache::get('config_' . $boardsuffix ) ) {
$events = Cache::get('events_' . $boardsuffix );
if (file_exists('inc/instance-functions.php')) {
require_once('inc/instance-functions.php');
}
if ($config['locale'] != $current_locale) {
$current_locale = $config['locale'];
init_locale($config['locale'], $error);
}
}
else {
$config = array();
// We will indent that later.
reset_events();
$arrays = array(
'db',
'api',
@ -86,7 +111,6 @@ function loadConfig() {
'dashboard_links'
);
$config = array();
foreach ($arrays as $key) {
$config[$key] = array();
}
@ -99,7 +123,7 @@ function loadConfig() {
// Those calls are expensive. Unfortunately, our cache system is not initialized at this point.
// So, we may store the locale in a tmp/ filesystem.
if (file_exists($fn = 'tmp/cache/locale_' . ( isset($board['uri']) ? $board['uri'] : '' ) ) ) {
if (file_exists($fn = 'tmp/cache/locale_' . $boardsuffix ) ) {
$config['locale'] = file_get_contents($fn);
}
else {
@ -138,8 +162,6 @@ function loadConfig() {
init_locale($config['locale'], $error);
}
date_default_timezone_set($config['timezone']);
if (!isset($config['global_message']))
$config['global_message'] = false;
@ -219,8 +241,22 @@ function loadConfig() {
if (!isset($config['user_flags']))
$config['user_flags'] = array();
if (!isset($__version))
$__version = file_exists('.installed') ? trim(file_get_contents('.installed')) : false;
$config['version'] = $__version;
if ($config['allow_roll'])
event_handler('post', 'diceRoller');
if (is_array($config['anonymous']))
$config['anonymous'] = $config['anonymous'][array_rand($config['anonymous'])];
}
// Effectful config processing below:
date_default_timezone_set($config['timezone']);
if ($config['root_file']) {
chdir($config['root_file']);
}
@ -233,10 +269,6 @@ function loadConfig() {
if (preg_match('/^\:\:(ffff\:)?(\d+\.\d+\.\d+\.\d+)$/', $__ip, $m))
$_SERVER['REMOTE_ADDR'] = $m[2];
if (!isset($__version))
$__version = file_exists('.installed') ? trim(file_get_contents('.installed')) : false;
$config['version'] = $__version;
if ($config['verbose_errors']) {
set_error_handler('verbose_error_handler');
error_reporting(E_ALL);
@ -260,13 +292,22 @@ function loadConfig() {
event_handler('post', 'postHandler');
}
if (is_array($config['anonymous']))
$config['anonymous'] = $config['anonymous'][array_rand($config['anonymous'])];
event('load-config');
if ($config['allow_roll'])
event_handler('post', 'diceRoller');
if ($config['cache_config'] && !isset ($config['cache_config_loaded'])) {
file_put_contents('tmp/cache/cache_config.php', '<?php '.
'$config = array();'.
'$config[\'cache\'] = '.
var_export($config['cache'], true).';'.
'$config[\'cache_config\'] = true;'.
'require_once(\'inc/cache.php\');'
);
event('load-config');
$config['cache_config_loaded'] = true;
Cache::set('config_'.$boardsuffix, $config);
Cache::set('events_'.$boardsuffix, $events);
}
if ($config['debug']) {
if (!isset($debug)) {

Loading…
Cancel
Save