CI_DB_Cache Class Reference

List of all members.


Public Member Functions

 CI_DB_Cache (&$db)
 Constructor.
 check_path ($path= '')
 Set Cache Directory Path.
 read ($sql)
 Retrieve a cached query.
 write ($sql, $object)
 Write a query to a cache file.
 delete ($segment_one= '', $segment_two= '')
 Delete cache files within a particular directory.
 delete_all ()
 Delete all existing cache files.

Public Attributes

 $CI
 $db

Detailed Description

Definition at line 25 of file DB_cache.php.


Member Function Documentation

CI_DB_Cache::check_path ( path = ''  ) 

Set Cache Directory Path.

public

Parameters:
string the path to the cache directory
Returns:
bool

Definition at line 54 of file DB_cache.php.

References is_really_writable().

Referenced by read(), and write().

00055         {
00056                 if ($path == '')
00057                 {
00058                         if ($this->db->cachedir == '')
00059                         {
00060                                 return $this->db->cache_off();
00061                         }
00062                 
00063                         $path = $this->db->cachedir;
00064                 }
00065         
00066                 // Add a trailing slash to the path if needed
00067                 $path = preg_replace("/(.+?)\/*$/", "\\1/",  $path);
00068 
00069                 if ( ! is_dir($path) OR ! is_really_writable($path))
00070                 {
00071                         // If the path is wrong we'll turn off caching
00072                         return $this->db->cache_off();
00073                 }
00074                 
00075                 $this->db->cachedir = $path;
00076                 return TRUE;
00077         }

Here is the call graph for this function:

Here is the caller graph for this function:

CI_DB_Cache::CI_DB_Cache ( &$  db  ) 

Constructor.

Grabs the CI super object instance so we can access it.

Definition at line 36 of file DB_cache.php.

References $db, and get_instance().

00037         {
00038                 // Assign the main CI object to $this->CI
00039                 // and load the file helper since we use it a lot
00040                 $this->CI =& get_instance();
00041                 $this->db =& $db;
00042                 $this->CI->load->helper('file');        
00043         }

Here is the call graph for this function:

CI_DB_Cache::delete ( segment_one = '',
segment_two = '' 
)

Delete cache files within a particular directory.

public

Returns:
bool

Definition at line 159 of file DB_cache.php.

00160         {       
00161                 if ($segment_one == '')
00162                 {
00163                         $segment_one  = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
00164                 }
00165                 
00166                 if ($segment_two == '')
00167                 {
00168                         $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
00169                 }
00170                 
00171                 $dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/';
00172                 
00173                 delete_files($dir_path, TRUE);
00174         }

CI_DB_Cache::delete_all (  ) 

Delete all existing cache files.

public

Returns:
bool

Definition at line 184 of file DB_cache.php.

00185         {
00186                 delete_files($this->db->cachedir, TRUE);
00187         }

CI_DB_Cache::read ( sql  ) 

Retrieve a cached query.

The URI being requested will become the name of the cache sub-folder. An MD5 hash of the SQL statement will become the cache file name

public

Returns:
string

Definition at line 90 of file DB_cache.php.

References check_path().

00091         {
00092                 if ( ! $this->check_path())
00093                 {
00094                         return $this->db->cache_off();
00095                 }
00096         
00097                 $uri  = ($this->CI->uri->segment(1) == FALSE) ? 'default.'      : $this->CI->uri->segment(1).'+';
00098                 $uri .= ($this->CI->uri->segment(2) == FALSE) ? 'index'         : $this->CI->uri->segment(2);
00099                 
00100                 $filepath = $uri.'/'.md5($sql);
00101                 
00102                 if (FALSE === ($cachedata = read_file($this->db->cachedir.$filepath)))
00103                 {       
00104                         return FALSE;
00105                 }
00106                 
00107                 return unserialize($cachedata);                 
00108         }       

Here is the call graph for this function:

CI_DB_Cache::write ( sql,
object 
)

Write a query to a cache file.

public

Returns:
bool

Definition at line 118 of file DB_cache.php.

References check_path().

00119         {
00120                 if ( ! $this->check_path())
00121                 {
00122                         return $this->db->cache_off();
00123                 }
00124 
00125                 $uri  = ($this->CI->uri->segment(1) == FALSE) ? 'default.'      : $this->CI->uri->segment(1).'+';
00126                 $uri .= ($this->CI->uri->segment(2) == FALSE) ? 'index'         : $this->CI->uri->segment(2);
00127                 
00128                 $dir_path = $this->db->cachedir.$uri.'/';
00129                 
00130                 $filename = md5($sql);
00131         
00132                 if ( ! @is_dir($dir_path))
00133                 {
00134                         if ( ! @mkdir($dir_path, DIR_WRITE_MODE))
00135                         {
00136                                 return FALSE;
00137                         }
00138                         
00139                         @chmod($dir_path, DIR_WRITE_MODE);                      
00140                 }
00141                 
00142                 if (write_file($dir_path.$filename, serialize($object)) === FALSE)
00143                 {
00144                         return FALSE;
00145                 }
00146                 
00147                 @chmod($dir_path.$filename, DIR_WRITE_MODE);
00148                 return TRUE;
00149         }

Here is the call graph for this function:


Member Data Documentation

CI_DB_Cache::$CI

Definition at line 27 of file DB_cache.php.

CI_DB_Cache::$db

Definition at line 28 of file DB_cache.php.

Referenced by CI_DB_Cache().


The documentation for this class was generated from the following file: