clean up the code a bit, fix a few bugs

This commit is contained in:
czaks 2016-01-26 01:44:56 +01:00
parent d918fddea1
commit ac4c2e0370
3 changed files with 85 additions and 102 deletions

View File

@ -90,7 +90,14 @@ class Cache {
case 'redis':
if (!self::$cache)
self::init();
if ($expires === FALSE) {
self::$cache->set($key, json_encode($value));
}
else {
self::$cache->setex($key, $expires, json_encode($value));
}
break;
case 'apc':
apc_store($key, $value, $expires);
@ -111,41 +118,6 @@ class Cache {
if ($config['debug'])
$debug['cached'][] = $key . ' (set)';
}
public static function store($key, $value) {
global $config, $debug;
$key = $config['cache']['prefix'] . $key;
switch ($config['cache']['enabled']) {
case 'memcached':
if (!self::$cache)
self::init();
self::$cache->set($key, $value);
break;
case 'redis':
if (!self::$cache)
self::init();
self::$cache->set($key, json_encode($value));
break;
case 'apc':
apc_store($key, $value);
break;
case 'xcache':
xcache_set($key, $value);
break;
case 'fs':
$key = str_replace('/', '::', $key);
$key = str_replace("\0", '', $key);
file_put_contents('tmp/cache/'.$key, json_encode($value));
break;
case 'php':
self::$cache[$key] = $value;
break;
}
if ($config['debug'])
$debug['cached'][] = $key . ' (set)';
}
public static function delete($key) {
global $config, $debug;

View File

@ -1196,6 +1196,18 @@
// Try not to build pages when we shouldn't have to.
$config['try_smarter'] = true;
// A reduced disk-IO option that does not write the HTML or JSON files to disk. It writes them to a
// cache which then can be quickly retrieved by a webserver without hitting the disk at all.
// See nginx's HttpRedis2Module module for a hint about a possible deployment.
// If you don't have HttpRedis2Module, i've included a 404.php that you can use with an htaccess like:
// RewriteEngine On
// RewriteRule ^[^/]+/$ %{REQUEST_URI}/../../404.php [NC,L]
// RewriteRule ^[^/]+/[^/]+$ %{REQUEST_URI}/../../404.php [NC,L]
// RewriteRule ^[^/]+/res/[^/]+$ %{REQUEST_URI}/../../404.php [NC,L]
// This example assumes your URL includes one directory level, i.e. http://localhost/vichan/
$config['files_in_cache'] = false;
// EXPERIMENTAL: Defer static HTML building to a moment, when a given file is actually accessed.
// Warning: This option won't run out of the box. You need to tell your webserver, that a file
// for serving 403 and 404 pages is /smart_build.php. Also, you need to turn off indexes.

View File

@ -630,16 +630,6 @@ function purge($uri) {
function file_write($path, $data, $simple = false, $skip_purge = false) {
global $config, $debug;
//echo "file_write($path, ", strlen($data), ", $simple, $skip_purge)<br>\n";
Cache::store('vichan_filecache_'.$path, $data, -1);
if ($config['gzip_static']) {
$bytes=strlen($data);
if ($bytes & ~0x3ff) {
Cache::store('vichan_filecache_'.$path.'.gz', gzencode($data), -1);
} else {
Cache::delete('vichan_filecache_'.$path.'.gz');
}
}
if (preg_match('/^remote:\/\/(.+)\:(.+)$/', $path, $m)) {
if (isset($config['remote'][$m[1]])) {
@ -653,7 +643,18 @@ function file_write($path, $data, $simple = false, $skip_purge = false) {
}
}
/*
if ($config['files_in_cache']) {
Cache::set('vichan_filecache_'.$path, $data);
if ($config['gzip_static']) {
$bytes=strlen($data);
if ($bytes & ~0x3ff) {
Cache::set('vichan_filecache_'.$path.'.gz', gzencode($data));
} else {
Cache::delete('vichan_filecache_'.$path.'.gz');
}
}
}
else {
if (!$fp = fopen($path, $simple ? 'w' : 'c'))
error('Unable to open file for writing: ' . $path);
@ -677,7 +678,7 @@ function file_write($path, $data, $simple = false, $skip_purge = false) {
// Close
if (!fclose($fp))
error('Unable to close file: ' . $path);
*/
/**
* Create gzipped file.
*
@ -686,7 +687,7 @@ function file_write($path, $data, $simple = false, $skip_purge = false) {
*
* This is useful with nginx with gzip_static on.
*/
/*
if ($config['gzip_static']) {
$gzpath = "$path.gz";
@ -700,7 +701,7 @@ function file_write($path, $data, $simple = false, $skip_purge = false) {
@unlink($gzpath);
}
}
*/
}
if (!$skip_purge && isset($config['purge'])) {
// Purge cache
@ -727,12 +728,6 @@ function file_write($path, $data, $simple = false, $skip_purge = false) {
function file_unlink($path) {
global $config, $debug;
//echo "file_unlink($path)<br>\n";
if ($config['gzip_static']) {
Cache::delete('vichan_filecache_'.$path);
} else {
Cache::delete('vichan_filecache_'.$path.'.gz');
}
if ($config['debug']) {
if (!isset($debug['unlink']))
@ -740,14 +735,18 @@ function file_unlink($path) {
$debug['unlink'][] = $path;
}
$ret=true;
/*
Cache::delete('vichan_filecache_'.$path);
if ($config['gzip_static']) {
Cache::delete('vichan_filecache_'.$path.'.gz');
}
$ret = @unlink($path);
if ($config['gzip_static']) {
$gzpath = "$path.gz";
@unlink($gzpath);
}
*/
if (isset($config['purge']) && $path[0] != '/' && isset($_SERVER['HTTP_HOST'])) {
// Purge cache
if (basename($path) == $config['file_index']) {