LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-12-2012, 01:06 PM   #1
markings
Member
 
Registered: Nov 2011
Location: Penn
Distribution: OpenSuSE 13.1
Posts: 33

Rep: Reputation: 3
PHP, trying to write a custom class to do file caching


Hello,

One of issues that I always encounter on multiple writes and reads to one file, I sometimes get unexpected behavior, instead I decided to store the file contents in memory and work from there. Especially on performance. Well, I am trying to write a reliable class. The issue I am having now atm is trying to think of every possible scenario where this class may fail and come up with every possible solution. My questions: Am I going in the right direction with this? What do you think about improving on the code?

I am stuck

PHP Code:
class File 
{
    private 
$mHandle;
    private 
$mCacheHandle;
    private 
$mCaching false;
    private 
$mCacheSize;
    private 
$mPath;
    private 
$mMode;
    private 
$mContents;
    private 
$mContentsModified;
    
    public function 
__construct$file $mode $perm 0700 $owner null $group null )
    {
        
$rewind false;
        switch( 
$mode ) {
            case 
L_FILE_APPEND:
                
$l_mode 'a+';
                break;
            case 
L_FILE_APPEND_CACHING:
                
$l_mode 'w+';
                
$this->mCaching true;
                break;
            case 
L_FILE_WRITE:
                
$l_mode 'c+';
                break;
            case 
L_FILE_WRITE_CACHING:
                
$l_mode 'c+';
                
$this->mCaching true;
                
$rewind true;
                break;
        }
        
        if ( !
$this->mHandle fopen$file $l_mode ) ) {
            
$this->mHandle false// Just to make sure
            
return false;
        }
        
        if ( 
$this->mCaching ) {
            
$this->mCacheHandle fopen'php://temp''w+' );
            
// Move contents into memory
            
$this->mCacheSize filesize$file );
            if ( 
fwrite$this->mCacheHandle fread$this->mHandle $this->mCacheSize ) ) ) {
                
// Looks like I unable to write, so disable caching
                
$this->mCaching false;
                
$this->mCacheSize 0;
                
fclose$this->mCacheHandle );
            } else if ( 
$rewind ) {
                
rewind$this->mCacheHandle );
            }
        }
        
        
        
$this->mPath $file;
        
$this->mMode $mode;
    }
    
    public function 
Read()
    {
        if ( !
is_resource$this->mHandle ) ) 
            return 
false;
        if ( 
$this->mCaching )
            return 
fread$this->mCacheHandle $length );
        return 
fread$this->mHandle filesize$this->mPath ) );
    }
    
    public function 
Write$data )
    {
        if ( !
is_resource$this->mHandle ) )
            return 
false;
        if ( 
$this->mCaching ) {
            
$this->mCacheHandle += strlen$data );
            return 
fwrite$this->mCacheHandle $data );
        } else
            return 
fwrite$this->mHandle $data );
                
        
$this->mContentsModified true;
        return 
true;
    }
    
    public function 
GetMode()
    {
        return 
$this->mMode;
    }
    
    public function 
Rewind()
    {
        
//return rewind( $this->mHandle );
    
}
    
    public function 
Flush()
    {
        if ( 
$this->mCaching ) {
            
$data fread$this->mCacheHandle $length );
            return 
fwrite$this->mHandle $data );
        } else {
            
fflush$this->mHandle );
        }
        
    }

 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP: Calling class functions from another class. unreal128 Programming 5 11-24-2006 03:04 AM
Apache/PHP - can't write file Influx Linux - Software 3 10-23-2005 10:50 PM
PHP: how to use a method from a class in the same class ldp Programming 5 09-17-2004 09:52 AM
php projects: define each class in a seperate file? ldp Programming 4 09-16-2004 05:00 PM
Apache 1.3.28/PHP 4.3.3 can't write temp file tm_burningdog Linux - Software 0 06-10-2004 01:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration