4100acda45394fa4e8efc805a486b732

Single function that wraps *any* other function, caching the results. Uses file_put_contents, unserialize and call_user_func_array. Variable timeout.

function cache_function($buildCallback, array $args= array (), $timeoutMinutes= 60) {
	if (is_array($buildCallback)) {
		$cacheKey= get_class($buildCallback[0]) . $buildCallback[1] . ':' . implode(':', $args);
	} else
		$cacheKey= $buildCallback . ':' . implode(':', $args);

	$file_path= CACHE_PATH . md5($cacheKey);
	@ $file_time= filemtime($file_path);
	$rebuild= $file_time < time() - ($timeoutMinutes * 60);

	//$rebuild= false; // FORCE REBUILD SKIP for WINDOWS

	if ($rebuild or (!$rebuild and !is_readable($file_path))) {
		$build= call_user_func_array($buildCallback, $args);
		file_put_contents($file_path, serialize($build), LOCK_EX);
		return $build;
	}
	return unserialize(file_get_contents($file_path));
}

Refactorings

No refactoring yet !

D41d8cd98f00b204e9800998ecf8427e

EllisGL

January 14, 2008, January 14, 2008 22:33, permalink

1 rating. Login to rate!

Just one optimization here.

function cache_function($buildCallback, array $args=array(), $timeoutMinutes= 60)
 {
  $cacheKey   = $buildCallback . ':' . implode(':', $args); // No need for ELSE{}
  
  if(is_array($buildCallback))
   {
		$cacheKey = get_class($buildCallback[0]) . $buildCallback[1] . ':' . implode(':', $args);
	 }

	$file_path  = CACHE_PATH . md5($cacheKey);
	@$file_time = filemtime($file_path);
	$rebuild    = $file_time < time() - ($timeoutMinutes * 60);
  
  // FORCE REBUILD SKIP for WINDOWS
  //$rebuild= false; 

	if($rebuild  || (!$rebuild and !is_readable($file_path)))
   {
    $build = call_user_func_array($buildCallback, $args);
    file_put_contents($file_path, serialize($build), LOCK_EX);
    return $build;
   }
	return unserialize(file_get_contents($file_path));
 }
4100acda45394fa4e8efc805a486b732

firesalamanders.openid.com

January 15, 2008, January 15, 2008 01:26, permalink

1 rating. Login to rate!

Even more compression of the first line and removing redundant parts.

function cache_function($buildCallback, array $args = array (), $timeoutMinutes = 60) {
  $cacheKey = (is_array($buildCallback) ? get_class($buildCallback[0]) . $buildCallback[1] : $buildCallback) .
  implode(':', $args);

  $file_path = CACHE_PATH . md5($cacheKey);
  @ $file_time = filemtime($file_path);
  $rebuild = $file_time < time() - ($timeoutMinutes * 60);

  //$rebuild= false; // FORCE REBUILD SKIP for WINDOWS

  if ($rebuild or (!$rebuild and !is_readable($file_path))) {
    $build = call_user_func_array($buildCallback, $args);
    file_put_contents($file_path, serialize($build), LOCK_EX);
    return $build;
  }
  return unserialize(file_get_contents($file_path));
}
D33ae6c5f3c870d7f83ef7d581528ca2

Gerry

August 16, 2008, August 16, 2008 03:25, permalink

1 rating. Login to rate!

Disagree with comment #1: the conditional is checked regardless in your code, you have just added a redundant assignment.
Disagree with comment #2: Just makes the code more difficult to read with little gain.

Code changes:
- The "//$rebuild= false; // FORCE REBUILD SKIP for WINDOWS" line doesn't make any sense as adding it would just mean that the cache file is never created or updated.
- Removed need for the problematic @ operator which I personally believe causes more trouble than it's worth in the long run.
- Changed the two return statements into one so as not to have "Spaghetti Code".
- Reduced the chance of accidental conflict arising in the $cacheKey (although still far from perfect)
- Changed the $timeout from minutes to seconds to reduce code complexity and provide more flexibility.
- Added PHP tags to the code so that the syntax highlighter would work.

I still think this code is far from perfect though, especially with possible conflicts of the cache key. There is probably a much better way of creating it.

<?php
	function cache_function($buildCallback, array $args = array(), $timeoutSeconds = 3600){
		// Set up the filename for the cache file 
		if(is_array($buildCallback)){
			$cacheKey = get_class($buildCallback[0]) .'::'. $buildCallback[1];
		}else{
			$cacheKey = $buildCallback . ':' . implode(':', $args);
		}
		$cacheKey .= ':' . implode(':', $args);
		$file_path = CACHE_PATH . md5($cacheKey);
	
		// If the file hasn't yet been created or is out of date then call the require function and store it's result.
		if(!file_exists($file_path) OR filemtime($file_path) < (time() - $timeoutSeconds)){
			$result = call_user_func_array($buildCallback, $args);
			file_put_contents($file_path, serialize($result), LOCK_EX);
		// Else, grab the result from the cache.
		}else{
			$result = unserialize(file_get_contents($file_path));
		}
		
		return $result;
	}
?>
35ce1d4eb0f666cd136987d34f64aedc

gbhv

April 30, 2010, April 30, 2010 09:44, permalink

No rating. Login to rate!

b

<?php
	function cache_function($buildCallback, array $args = array(), $timeoutSeconds = 3600){
		// Set up the filename for the cache file 
		if(is_array($buildCallback)){
			$cacheKey = get_class($buildCallback[0]) .'::'. $buildCallback[1];
		}else{
			$cacheKey = $buildCallback . ':' . implode(':', $args);
		}
		$cacheKey .= ':' . implode(':', $args);
		$file_path = CACHE_PATH . md5($cacheKey);
	
		// If the file hasn't yet been created or is out of date then call the require function and store it's result.
		if(!file_exists($file_path) OR filemtime($file_path) < (time() - $timeoutSeconds)){
			$result = call_user_func_array($buildCallback, $args);
			file_put_contents($file_path, serialize($result), LOCK_EX);
		// Else, grab the result from the cache.
		}else{
			$result = unserialize(file_get_contents($file_path));
		}
		
		return $result;
	}
?>

Your refactoring





Format Copy from initial code

or Cancel