123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- <?php
- defined('BASEPATH') OR exit('No direct script access allowed');
- class CI_DB_Cache {
-
- public $CI;
-
- public $db;
-
-
- public function __construct(&$db)
- {
-
- $this->CI =& get_instance();
- $this->db =& $db;
- $this->CI->load->helper('file');
- $this->check_path();
- }
-
-
- public function check_path($path = '')
- {
- if ($path === '')
- {
- if ($this->db->cachedir === '')
- {
- return $this->db->cache_off();
- }
- $path = $this->db->cachedir;
- }
-
- $path = realpath($path)
- ? rtrim(realpath($path), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR
- : rtrim($path, '/').'/';
- if ( ! is_dir($path))
- {
- log_message('debug', 'DB cache path error: '.$path);
-
- return $this->db->cache_off();
- }
- if ( ! is_really_writable($path))
- {
- log_message('debug', 'DB cache dir not writable: '.$path);
-
- return $this->db->cache_off();
- }
- $this->db->cachedir = $path;
- return TRUE;
- }
-
-
- public function read($sql)
- {
- $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
- $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
- $filepath = $this->db->cachedir.$segment_one.'+'.$segment_two.'/'.md5($sql);
- if (FALSE === ($cachedata = @file_get_contents($filepath)))
- {
- return FALSE;
- }
- return unserialize($cachedata);
- }
-
-
- public function write($sql, $object)
- {
- $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
- $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
- $dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/';
- $filename = md5($sql);
- if ( ! is_dir($dir_path) && ! @mkdir($dir_path, 0750))
- {
- return FALSE;
- }
- if (write_file($dir_path.$filename, serialize($object)) === FALSE)
- {
- return FALSE;
- }
- chmod($dir_path.$filename, 0640);
- return TRUE;
- }
-
-
- public function delete($segment_one = '', $segment_two = '')
- {
- if ($segment_one === '')
- {
- $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
- }
- if ($segment_two === '')
- {
- $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
- }
- $dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/';
- delete_files($dir_path, TRUE);
- }
-
-
- public function delete_all()
- {
- delete_files($this->db->cachedir, TRUE, TRUE);
- }
- }
|