diff --git a/inc/cache.php b/inc/cache.php index c5865fa9..1f6b14fa 100755 --- a/inc/cache.php +++ b/inc/cache.php @@ -90,7 +90,14 @@ class Cache { case 'redis': if (!self::$cache) self::init(); - self::$cache->setex($key, $expires, json_encode($value)); + + 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; diff --git a/inc/config.php b/inc/config.php index d5bdbfe5..201d3d03 100644 --- a/inc/config.php +++ b/inc/config.php @@ -1192,10 +1192,22 @@ // Website favicon. // $config['url_favicon'] = '/favicon.gif'; - + // 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. diff --git a/inc/functions.php b/inc/functions.php index 18e4b06d..f3db3190 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -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)
\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,54 +643,65 @@ function file_write($path, $data, $simple = false, $skip_purge = false) { } } - /* - if (!$fp = fopen($path, $simple ? 'w' : 'c')) - error('Unable to open file for writing: ' . $path); - - // File locking - if (!$simple && !flock($fp, LOCK_EX)) { - error('Unable to lock file: ' . $path); - } - - // Truncate file - if (!$simple && !ftruncate($fp, 0)) - error('Unable to truncate file: ' . $path); - - // Write data - if (($bytes = fwrite($fp, $data)) === false) - error('Unable to write to file: ' . $path); - - // Unlock - if (!$simple) - flock($fp, LOCK_UN); - - // Close - if (!fclose($fp)) - error('Unable to close file: ' . $path); - */ - /** - * Create gzipped file. - * - * When writing into a file foo.bar and the size is larger or equal to 1 - * KiB, this also produces the gzipped version foo.bar.gz - * - * This is useful with nginx with gzip_static on. - */ - /* - if ($config['gzip_static']) { - $gzpath = "$path.gz"; - - if ($bytes & ~0x3ff) { // if ($bytes >= 1024) - if (file_put_contents($gzpath, gzencode($data), $simple ? 0 : LOCK_EX) === false) - error("Unable to write to file: $gzpath"); - //if (!touch($gzpath, filemtime($path), fileatime($path))) - // error("Unable to touch file: $gzpath"); - } - else { - @unlink($gzpath); + 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); + + // File locking + if (!$simple && !flock($fp, LOCK_EX)) { + error('Unable to lock file: ' . $path); + } + + // Truncate file + if (!$simple && !ftruncate($fp, 0)) + error('Unable to truncate file: ' . $path); + + // Write data + if (($bytes = fwrite($fp, $data)) === false) + error('Unable to write to file: ' . $path); + + // Unlock + if (!$simple) + flock($fp, LOCK_UN); + + // Close + if (!fclose($fp)) + error('Unable to close file: ' . $path); + + /** + * Create gzipped file. + * + * When writing into a file foo.bar and the size is larger or equal to 1 + * KiB, this also produces the gzipped version foo.bar.gz + * + * This is useful with nginx with gzip_static on. + */ + + if ($config['gzip_static']) { + $gzpath = "$path.gz"; + + if ($bytes & ~0x3ff) { // if ($bytes >= 1024) + if (file_put_contents($gzpath, gzencode($data), $simple ? 0 : LOCK_EX) === false) + error("Unable to write to file: $gzpath"); + //if (!touch($gzpath, filemtime($path), fileatime($path))) + // error("Unable to touch file: $gzpath"); + } + else { + @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)
\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']) {