2 /*------------------------------------------------------------
3 * CarbonPHP framework (C) Tom Bell
4 * http://tombell.org.uk
5 *------------------------------------------------------------*/
7 if (!defined('CARBON_PATH'))
9 exit('Direct script access is not allowed.');
12 class Carbon_Database_Cache
16 public function __construct()
18 $this->carbon
=& get_instance();
19 $this->carbon
->load
->utility('filesystem');
22 public function check_path($path = '')
26 if ($this->carbon
->db
->cachedir
== '')
28 return $this->carbon
->db
->cache_off();
31 $path = $this->carbon
->db
->cachedir
;
34 $path = preg_replace("/(.+?)\/*$/", "\\1/", $path);
36 if (!is_dir($path) ||
!is_writable($path))
38 if ($this->carbon
->db
->db_debug
)
40 return $this->carbon
->db
->display_error('database_invalid_cache_path');
43 return $this->carbon
->db
->cache_off();
46 $this->cache
->db
->cachedir
= $path;
51 public function read($sql)
53 if (!$this->check_path())
55 return $this->carbon
->db
->cache_off();
58 $uri = ($this->carbon
->uri
->get_segment(1) == false) ?
'default.' : $this->carbon
->uri
->get_segment(1) . '+';
59 $uri .= ($this->carbon
->uri
->get_segment(2) == false) ?
'index' : $this->carbon
->uri
->get_segment(2);
61 $filepath = $uri . '/' . md5($sql);
63 $cachedata = read_file($this->carbon
->db
->cachedir
. $filepath);
65 if ($cachedata === false)
70 return unserialize($cachedata);
73 public function write($sql, $object)
75 if (!$this->check_path())
77 return $this->carbon
->db
->cache_off();
80 $uri = ($this->carbon
->uri
->get_segment(1) == false) ?
'default.' : $this->carbon
->uri
->get_segment(1) . '+';
81 $uri .= ($this->carbon
->uri
->get_segment(2) == false) ?
'index' : $this->carbon
->uri
->get_segment(2);
83 $dir_path = $this->carbon
->db
->cachedir
. $uri . '/';
84 $filename = md5($sql);
86 if (!@is_dir
($dir_path))
88 if (!@mkdir
($dir_path, 0777))
93 @chmod
($dir_path, 0777);
96 if (write_file($dir_path . $filename, serialize($object)) === false)
101 @chmod
($dir_path, $filename, 0777);
106 public function delete($segment_one = '', $segment_two = '')
108 if ($segment_one == '')
110 $segment_one = ($this->carbon
->uri
->get_segment(1) == false) ?
'default' : $this->carbon
->uri
->get_segment(2);
113 if ($segment_two == '')
115 $segment_two = ($this->carbon
->uri
->get_segment(2) == false) ?
'index' : $this->carbon
->uri
->get_segment(2);
118 $dir_path = $this->carbon
->db
->cachedir
. $segment_one . '+' . $segment_two . '/';
120 delete_files($dir_path, true);
123 public function delete_all()
125 delete_files($this->carbon
->db
->cachedir
, true);