I am trying to create a class that stores and retrieves data from both the filesystem and APC simultaneously. This requires that I extend the ezcCacheStorageFile class and overwrite the the restore, store, and delete functions to insert the mirroring logic for APC and the filesystem.
When I do this I get the following error:
{
Fatal error: Call to private method ezcCacheStorageFile::search() from context 'ezcCacheStorageFileApcArray'
}
Obviously the issue is that the search function is being declared visible as "private." This makes it impossible to overwrite the restore, store, and delete functions since they all rely on searching. The restore, store, and delete functions are all visible as "protected."
In my code example below, nevermind the APC wrapper class I wrote "ApcCache." It is a nearly direct interface to the documented APC functions.
class ezcCacheStorageFileApcArray extends ezcCacheStorageFile
{
/**
* Store data to the cache storage.
* This overrides this function in the storage class. We need to try to store things in APC for the data to keep it close but mirror to the filesystem which is shared over NFS to multiple servers.
*/
public function store( $id, $data, $attributes = array() )
{
// Generates the identifier
$filename = $this->properties['location'] . $this->generateIdentifier( $id, $attributes );
// Deletes the files if it already exists on the filesystem
if ( file_exists( $filename ) )
{
if ( unlink( $filename ) === false )
{
throw new ezcBaseFilePermissionException( $filename, ezcBaseFileException::WRITE, 'Could not delete existsing cache file.' );
}
}
// Deletes the data from APC if it already exists
ApcCache::delete( $filename );
// Prepares the data for filesystem storage
$dataStr = $this->prepareData( $data );
// Tries to create the directory on the filesystem
$dirname = dirname( $filename );
if ( !is_dir( $dirname ) && !mkdir( $dirname, 0777, true ) )
{
throw new ezcBaseFilePermissionException( $dirname, ezcBaseFileException::WRITE, 'Could not create directory to stor cache file.' );
}
// Tries to write the file the filesystem
if ( file_put_contents( $filename, $dataStr ) !== strlen( $dataStr ) )
{
throw new ezcBaseFileIoException( $filename, ezcBaseFileException::WRITE, 'Could not write data to cache file.' );
}
// Tries to set the file permissions
if ( ezcBaseFeatures::os() !== "Windows" )
{
chmod( $filename, $this->options->permissions );
}
// Prepares the data for APC storage
$dataObj = $this->prepareData( $data, true );
$dataObj->mtime = filemtime( $filename );
// Stores it in APC
ApcCache::store( $filename, $dataObj, $this->properties['options']['ttl'] );
// Returns the ID for no good reason
return $id;
}
}