DB_cache.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author EllisLab Dev Team
  9. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc.
  10. * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
  11. * @license http://codeigniter.com/user_guide/license.html
  12. * @link http://codeigniter.com
  13. * @since Version 1.0
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. /**
  18. * Database Cache Class
  19. *
  20. * @category Database
  21. * @author EllisLab Dev Team
  22. * @link http://codeigniter.com/user_guide/database/
  23. */
  24. class CI_DB_Cache {
  25. var $CI;
  26. var $db; // allows passing of db object so that multiple database connections and returned db objects can be supported
  27. /**
  28. * Constructor
  29. *
  30. * Grabs the CI super object instance so we can access it.
  31. *
  32. */
  33. function __construct(&$db)
  34. {
  35. // Assign the main CI object to $this->CI
  36. // and load the file helper since we use it a lot
  37. $this->CI =& get_instance();
  38. $this->db =& $db;
  39. $this->CI->load->helper('file');
  40. }
  41. // --------------------------------------------------------------------
  42. /**
  43. * Set Cache Directory Path
  44. *
  45. * @access public
  46. * @param string the path to the cache directory
  47. * @return bool
  48. */
  49. function check_path($path = '')
  50. {
  51. if ($path == '')
  52. {
  53. if ($this->db->cachedir == '')
  54. {
  55. return $this->db->cache_off();
  56. }
  57. $path = $this->db->cachedir;
  58. }
  59. // Add a trailing slash to the path if needed
  60. $path = preg_replace("/(.+?)\/*$/", "\\1/", $path);
  61. if ( ! is_dir($path) OR ! is_really_writable($path))
  62. {
  63. // If the path is wrong we'll turn off caching
  64. return $this->db->cache_off();
  65. }
  66. $this->db->cachedir = $path;
  67. return TRUE;
  68. }
  69. // --------------------------------------------------------------------
  70. /**
  71. * Retrieve a cached query
  72. *
  73. * The URI being requested will become the name of the cache sub-folder.
  74. * An MD5 hash of the SQL statement will become the cache file name
  75. *
  76. * @access public
  77. * @return string
  78. */
  79. function read($sql)
  80. {
  81. if ( ! $this->check_path())
  82. {
  83. return $this->db->cache_off();
  84. }
  85. $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
  86. $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
  87. $filepath = $this->db->cachedir.$segment_one.'+'.$segment_two.'/'.md5($sql);
  88. if (FALSE === ($cachedata = read_file($filepath)))
  89. {
  90. return FALSE;
  91. }
  92. return unserialize($cachedata);
  93. }
  94. // --------------------------------------------------------------------
  95. /**
  96. * Write a query to a cache file
  97. *
  98. * @access public
  99. * @return bool
  100. */
  101. function write($sql, $object)
  102. {
  103. if ( ! $this->check_path())
  104. {
  105. return $this->db->cache_off();
  106. }
  107. $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
  108. $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
  109. $dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/';
  110. $filename = md5($sql);
  111. if ( ! @is_dir($dir_path))
  112. {
  113. if ( ! @mkdir($dir_path, DIR_WRITE_MODE))
  114. {
  115. return FALSE;
  116. }
  117. @chmod($dir_path, DIR_WRITE_MODE);
  118. }
  119. if (write_file($dir_path.$filename, serialize($object)) === FALSE)
  120. {
  121. return FALSE;
  122. }
  123. @chmod($dir_path.$filename, FILE_WRITE_MODE);
  124. return TRUE;
  125. }
  126. // --------------------------------------------------------------------
  127. /**
  128. * Delete cache files within a particular directory
  129. *
  130. * @access public
  131. * @return bool
  132. */
  133. function delete($segment_one = '', $segment_two = '')
  134. {
  135. if ($segment_one == '')
  136. {
  137. $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
  138. }
  139. if ($segment_two == '')
  140. {
  141. $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
  142. }
  143. $dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/';
  144. delete_files($dir_path, TRUE);
  145. }
  146. // --------------------------------------------------------------------
  147. /**
  148. * Delete all existing cache files
  149. *
  150. * @access public
  151. * @return bool
  152. */
  153. function delete_all()
  154. {
  155. delete_files($this->db->cachedir, TRUE);
  156. }
  157. }
  158. /* End of file DB_cache.php */
  159. /* Location: ./system/database/DB_cache.php */