public static function getAndModify(FileSystemCacheKey $key, \Closure $callback, $resetTtl=false) {
		$filename = $key->getFileName();
		
		if(!file_exists($filename)) return false;

		//open a file handle
		$fh = self::getFileHandle($filename,'c+');
		if(!$fh) return false;
		
		//get the data
		$data = self::getContents($fh,$key);
		if(!$data) return false;
		
		//get new value from callback function
		$old_value = $data->value;
		$data->value = $callback($data->value);
		
		//if the callback function returns false
		if($data->value === false) {
			self::closeFile($fh);

			//delete the cache file
			self::invalidate($key);
			return false;
		}
		
		//if value didn't change
		if(!$resetTtl && $data->value === $old_value) {
			self::closeFile($fh);			
			return $data->value;
		}
		
		//if we're resetting the ttl to now
		if($resetTtl) {
			$data->created = time();
			if($data->ttl) {
				$data->expires = $data->created + $data->ttl;
			}
		}

		if(!self::emptyFile($fh)) return false;

		//write contents and close the file handle
		self::putContents($fh,$data);
		
		//return the new value after modifying
		return $data->value;
	}
public static function getAndModify(FileSystemCacheKey $key, \Closure $callback, $resetTtl=false) {
		$filename = $key->getFileName();
		
		if(!file_exists($filename)) return false;

		//open a file handle
		$fh = self::getFileHandle($filename,'c+');
		if(!$fh) return false;
		
		//get the data
		$data = self::getContents($fh,$key);
		if(!$data) return false;
		
		//get new value from callback function
		$old_value = $data->value;
		$data->value = $callback($data->value);
		
		//if the callback function returns false
		if($data->value === false) {
			self::closeFile($fh);

			//delete the cache file
			self::invalidate($key);
			return false;
		}
		
		//if value didn't change
		if(!$resetTtl && $data->value === $old_value) {
			self::closeFile($fh);			
			return $data->value;
		}
		
		//if we're resetting the ttl to now
		if($resetTtl) {
			$data->created = time();
			if($data->ttl) {
				$data->expires = $data->created + $data->ttl;
			}
		}

		if(!self::emptyFile($fh)) return false;

		//write contents and close the file handle
		self::putContents($fh,$data);
		
		//return the new value after modifying
		return $data->value;
	}
public static function getAndModify ( FileSystemCacheKey $ key , \ Closure $ callback , $ resetTtl = false ) { $ filename = $ key -> getFileName ( ) ; if ( ! file_exists ( $ filename ) ) return false ; //open a file handle $ fh = self :: getFileHandle ( $ filename , 'c+' ) ; if ( ! $ fh ) return false ; //get the data $ data = self :: getContents ( $ fh , $ key ) ; if ( ! $ data ) return false ; //get new value from callback function $ old_value = $ data -> value ; $ data -> value = $ callback ( $ data -> value ) ; //if the callback function returns false if ( $ data -> value === false ) { self :: closeFile ( $ fh ) ; //delete the cache file self :: invalidate ( $ key ) ; return false ; } //if value didn't change if ( ! $ resetTtl && $ data -> value === $ old_value ) { self :: closeFile ( $ fh ) ; return $ data -> value ; } //if we're resetting the ttl to now if ( $ resetTtl ) { $ data -> created = time ( ) ; if ( $ data -> ttl ) { $ data -> expires = $ data -> created + $ data -> ttl ; } } if ( ! self :: emptyFile ( $ fh ) ) return false ; //write contents and close the file handle self :: putContents ( $ fh , $ data ) ; //return the new value after modifying return $ data -> value ; }
Atomically retrieve data from cache, modify it, and store it back
@param FileSystemCacheKey $key The cache key
@param Closure $callback A closure function to modify the cache value.
Takes the old value as an argument and returns new value.
If this function returns false, the cached value will be invalidated.
@param bool $resetTtl If set to true, the expiration date will be recalculated using the previous TTL
@return mixed The new value if it was stored successfully or false if it wasn't
@throws Exception If an invalid callback method is given
Atomically retrieve data from cache modify it and store it back