Skip to content

Commit

Permalink
fix: cached file get contents in persistent cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Thulin committed Oct 10, 2023
1 parent 292793d commit a34a76e
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions source/php/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,16 +410,39 @@ private function cachedFileExists($path) {
*/
private function cachedFileGetContents($path) {
$id = md5($path);
if(isset(self::$cache['fileGetContents'][$id])) {

if (isset(self::$cache['fileGetContents'][$id])) {
// If the content is already in the static cache, return it.
return self::$cache['fileGetContents'][$id];
}

if($content = file_get_contents($path)) {
return self::$cache['fileGetContents'][$id] = $content;

if (function_exists('wp_cache_get')) {
// If the wp_cache_get function exists (i.e., running inside WordPress),
// attempt to retrieve the content from the cache.
$cachedContent = wp_cache_get($id, 'fileGetContents');
if ($cachedContent !== false) {
// Cache the content in the static variable for future use.
self::$cache['fileGetContents'][$id] = $cachedContent;
return $cachedContent;
}
}

return false;

// If not in cache, use file_get_contents
$content = file_get_contents($path);

if (function_exists('wp_cache_set')) {
// If the wp_cache_set function exists (i.e., running inside WordPress),
// cache the content using WordPress's cache.
wp_cache_set($id, $content, 'fileGetContents');
}

// Cache the content in the static variable for future use.
self::$cache['fileGetContents'][$id] = $content;

return $content !== false ? $content : false;
}



/**
* Check if a file exists using cached results.
Expand Down

0 comments on commit a34a76e

Please sign in to comment.