Skip to content

Large file Cache Issue #1

@kmrichterandrew

Description

@kmrichterandrew

Suggested Changes:

  1. UTF-8 Encoding Enforcement
    Location: In simplecache_pagestart() function.
    Code Change:

header('Content-Type: text/html; charset=UTF-8'); // Enforce UTF-8 encoding
Benefit: Ensures content is rendered correctly and consistently, avoiding encoding issues.
2. Improved Gzip Compression Handling
Location: In simplecache_pagestart() function.
Code Change:

if (!ob_start('ob_gzhandler')) ob_start(); // Fallback if gzip handler fails
Benefit: Ensures output buffering works even if gzip compression fails or is not supported.
3. Output Buffer Handling and Flushing
Location: In simplecache_pageend() function.
Code Change:

ob_end_flush(); // Send output to the browser and flush buffer
Benefit: Ensures that large pages are fully sent to the browser without buffering issues.
4. Cache Folder Handling and Permissions
Location: In simplecache_pageend() function.
Code Change:

if (!is_dir($simplecache_dir) && !mkdir($simplecache_dir, 0755, true)) {
error_log('Unable to create cache folder: ' . $simplecache_dir);
return;
}
Benefit: Ensures the cache directory is created with proper permissions, avoiding folder-related failures.
5. Cache Expiration Logic (TTL)
Location: In simplecache_pagestart() function.

Code Change:

if ($simplecache_conf['ttl'] == 0 || (time() - $simplecache_conf['ttl'] * 60) < $cachetime) {
include($simplecache_file);
exit;
}
Benefit: Properly handles page cache expiration or allows disabling expiration with TTL set to 0.
6. Improved Error Logging
Location: Throughout the code, especially in cache-related operations.
Code Change Examples:

error_log('Unable to write cache file: ' . $simplecache_file);
error_log('Failed to delete cache file: ' . $simplecache_file);

Benefit: Helps troubleshoot cache issues by providing detailed error logs.
7. Cache Invalidation Improvements
Location: In simplecache_flushpage() function.

code changes:
unlink($simplecache_file) or error_log('Failed to delete cache file: ' . $simplecache_file);
Benefit: Ensures cache files are properly deleted when a page is edited.

updated code would look like this:

"); fclose($fp); } else { error_log('Unable to write cache file: ' . $simplecache_file); } ob_end_flush(); // Send output to browser } } } /*********************************************************************************** * * Helper functions * ***********************************************************************************/ function simplecache_flushall() { $simplecache_dir = GSDATAOTHERPATH . 'simplecache_cache/'; if (is_dir($simplecache_dir)) { foreach (scandir($simplecache_dir) as $file) { if (!in_array($file, ['.', '..', '.htaccess']) && is_file($simplecache_dir . $file)) { unlink($simplecache_dir . $file); } } } } function simplecache_flushpage($page = null) { $simplecache_dir = GSDATAOTHERPATH . 'simplecache_cache/'; $page = $page ?? md5($_POST['post-id']); $simplecache_file = $simplecache_dir . $page . '.cache'; if (file_exists($simplecache_file)) { unlink($simplecache_file) or error_log('Failed to delete cache file: ' . $simplecache_file); } } /*********************************************************************************** * * Backend management page * ***********************************************************************************/ function simplecache_manage() { global $simplecache_conf; $simplecache_dir = GSDATAOTHERPATH . 'simplecache_cache/'; $message = ''; if (isset($_GET['cache_flush']) && $_GET['cache_flush'] === 'Y') { simplecache_flushall(); $message = 'All cache files deleted.'; } if (isset($_POST['submit_settings'])) { $simplecache_conf['enabled'] = isset($_POST['enabled']) ? 'Y' : 'N'; $simplecache_conf['ttl'] = is_numeric($_POST['ttl']) ? min(1400, max(0, (int)$_POST['ttl'])) : $simplecache_conf['ttl']; simplecache_saveconf(); $message = 'Settings updated.'; } if ($simplecache_conf['enabled'] === 'N') { echo '
SimpleCache is currently disabled!
'; } if ($message) { echo '
' . $message . '
'; } echo '

SimpleCache Settings

'; echo ''; echo ' Enable caching
'; echo 'Cache TTL (minutes):
'; echo ''; echo ''; } /*********************************************************************************** * * Config functions * ***********************************************************************************/ function simplecache_saveconf() { global $simplecache_conf; $configfile = GSDATAOTHERPATH . 'simplecache.xml'; $xml = new SimpleXMLElement(''); $xml->addChild('enabled', $simplecache_conf['enabled']); $xml->addChild('ttl', $simplecache_conf['ttl']); $nocache = $xml->addChild('nocache'); foreach ($simplecache_conf['nocache'] as $slug) { $nocache->addChild('slug', $slug); } $xml->asXML($configfile); } function simplecache_loadconf() { $configfile = GSDATAOTHERPATH . 'simplecache.xml'; if (!file_exists($configfile)) { $default = new SimpleXMLElement('Y60'); $default->asXML($configfile); chmod($configfile, 0755); } $xml = simplexml_load_file($configfile); return [ 'enabled' => (string) $xml->enabled, 'ttl' => (int) $xml->ttl, 'nocache' => array_map('strval', iterator_to_array($xml->nocache->slug)), ]; } ?>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions