@@ -96,27 +96,33 @@ function add( $key, &$var, $expire = 0, $group = '' ) {
9696	/** 
9797	 * Sets data 
9898	 * 
99- 	 * @param string   $key 
100- 	 * @param mixed   $var  
101- 	 * @param integer $ expire 
102- 	 * @param string   $group  Used to differentiate between groups of cache values 
103- 	 * @return boolean  
99+ 	 * @param string $key        An MD5 of the DB query.  
100+ 	 * @param mixed  $content    Data to be cached.  
101+ 	 * @param int    $expiration Time to  expire.  If 0, then the data will never expire.  
102+ 	 * @param string $group       Used to differentiate between groups of cache values.  
103+ 	 * @return bool  
104104	 */ 
105- 	function  set ( $ key , $ var , $ expire  = 0 , $ group  = ''  ) {
105+ 	function  set ( $ key , $ content , $ expiration  = 0 , $ group  = ''  ) {
106+ 		/** 
107+ 		 * Get the file pointer of the cache file. 
108+ 		 * The $key is transformed to a storage key (format "w3tc_INSTANCEID_HOST_BLOGID_dbcache_HASH"). 
109+ 		 * The file path is in the format: CACHEDIR/db/BLOGID/GROUP/[0-9a-f]{3}/[0-9a-f]{3}/[0-9a-f]{32}. 
110+ 		 */ 
106111		$ fp  = $ this  ->fopen_write ( $ key , $ group , 'wb '  );
112+ 
107113		if  ( !$ fp  )
108114			return  false ;
109115
110116		if  ( $ this  ->_locking  )
111117			@flock ( $ fp , LOCK_EX  );
112118
113- 		if  ( $ expire   <= 0  || $ expire   > W3TC_CACHE_FILE_EXPIRE_MAX  )
114- 			$ expire   = W3TC_CACHE_FILE_EXPIRE_MAX ;
119+ 		if  ( $ expiration   <= 0  || $ expiration   > W3TC_CACHE_FILE_EXPIRE_MAX  )
120+ 			$ expiration   = W3TC_CACHE_FILE_EXPIRE_MAX ;
115121
116- 		$ expires_at  = time () + $ expire  ;
122+ 		$ expires_at  = time () + $ expiration  ;
117123		@fputs ( $ fp , pack ( 'L ' , $ expires_at  ) );
118124		@fputs ( $ fp , '<?php exit; ?> '  );
119- 		@fputs ( $ fp , @serialize ( $ var   ) );
125+ 		@fputs ( $ fp , @serialize ( $ content   ) );
120126		@fclose ( $ fp  );
121127
122128		if  ( $ this  ->_locking  )
@@ -332,16 +338,17 @@ function mtime( $key, $group = '' ) {
332338	}
333339
334340	/** 
335- 	 * Returns file path  for key  
341+ 	 * Returns subpath  for the cache file (format: [0-9a-f]{3}/[0-9a-f]{3}/[0-9a-f]{32}).  
336342	 * 
337- 	 * @param string  $key 
343+ 	 * @param string $key Storage key (format: "w3tc_INSTANCEID_HOST_BLOGID_dbcache_HASH"). 
344+ 	 * @param string $group Used to differentiate between groups of cache values. 
338345	 * @return string 
339346	 */ 
340347	function  _get_path ( $ key , $ group  = ''  ) {
341348		if  ( $ this  ->_use_wp_hash  && function_exists ( 'wp_hash '  ) )
342- 			$ hash  = wp_hash ( $ key  );
349+ 			$ hash  = wp_hash ( $ key  );  // Most common. 
343350		else 
344- 			$ hash  = md5 ( $ key  );
351+ 			$ hash  = md5 ( $ key  );  // Less common, but still used in some cases. 
345352
346353		return  ( $ group  ? $ group  . DIRECTORY_SEPARATOR  : ''  ) . sprintf ( '%s/%s/%s.php ' , substr ( $ hash , 0 , 3  ), substr ( $ hash , 3 , 3  ), $ hash  );
347354	}
@@ -463,21 +470,34 @@ public function counter_get( $key ) {
463470		return  $ count ;
464471	}
465472
473+ 	/** 
474+ 	 * Open the cache file for writing and return the file pointer. 
475+ 	 * 
476+ 	 * @param string $key An MD5 of the DB query. 
477+ 	 * @param string $group Cache group. 
478+ 	 * @param string $mode File mode.  For example: 'wb' for write binary. 
479+ 	 * @return resource|false File pointer on success, false on failure. 
480+ 	 */ 
466481	private  function  fopen_write ( $ key , $ group , $ mode  ) {
482+ 		// Get the storage key (format: "w3tc_INSTANCEID_HOST_BLOGID_dbcache_$key"). 
467483		$ storage_key  = $ this  ->get_item_key ( $ key  );
468484
485+ 		// Get the subpath for the cache file (format: [0-9a-f]{3}/[0-9a-f]{3}/[0-9a-f]{32}). 
469486		$ sub_path  = $ this  ->_get_path ( $ storage_key , $ group  );
487+ 
488+ 		// Ge the entire path of the cache file. 
470489		$ path  = $ this  ->_cache_dir  . DIRECTORY_SEPARATOR  . $ sub_path ;
471490
491+ 		// Create the directory if it does not exist. 
472492		$ dir  = dirname ( $ path  );
473493
474494		if  ( !@is_dir ( $ dir  ) ) {
475495			if  ( !Util_File::mkdir_from ( $ dir , dirname ( W3TC_CACHE_DIR  ) ) )
476496				return  false ;
477497		}
478498
479- 		$ fp  = @ fopen (  $ path ,  $ mode  ); 
480- 		return  $ fp  ;
499+ 		// Open the cache file for writing. 
500+ 		return  @ fopen (  $ path ,  $ mode  ) ;
481501	}
482502
483503	/** 
0 commit comments