PageRenderTime 50ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/system/database/DB_cache.php

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