From 7e75a7e0c73381072766b52b49240e3aac9bec18 Mon Sep 17 00:00:00 2001 From: admin Date: Wed, 4 Oct 2006 18:43:24 +0000 Subject: --- system/database/DB_cache.php | 187 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 187 insertions(+) create mode 100644 system/database/DB_cache.php (limited to 'system') diff --git a/system/database/DB_cache.php b/system/database/DB_cache.php new file mode 100644 index 000000000..135be3ce9 --- /dev/null +++ b/system/database/DB_cache.php @@ -0,0 +1,187 @@ +obj + // and load the file helper since we use it a lot + $this->obj =& get_instance(); + $this->obj->load->helper('file'); + } + + // -------------------------------------------------------------------- + + /** + * Set Cache Directory Path + * + * @access public + * @param string the path to the cache directory + * @return bool + */ + function check_path($path = '') + { + if ($path == '') + { + if ($this->obj->db->cachedir == '') + { + return $this->obj->db->cache_off(); + } + + $path = $this->obj->db->cachedir; + } + + // Add a trailing slash to the path if needed + $path = preg_replace("/(.+?)\/*$/", "\\1/", $path); + + if ( ! is_dir($path) OR ! is_writable($path)) + { + if ($this->obj->db->db_debug) + { + return $this->obj->db->display_error('db_invalid_cache_path'); + } + + // If the path is wrong we'll turn off caching + return $this->obj->db->cache_off(); + } + + $this->obj->db->cachedir = $path; + return TRUE; + } + + // -------------------------------------------------------------------- + + /** + * Retreive 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 + * + * @access public + * @return string + */ + function read($sql) + { + if ( ! $this->check_path()) + { + return $this->obj->db->cache_off(); + } + + $uri = ($this->obj->uri->segment(1) == FALSE) ? 'base' : $this->obj->uri->segment(2); + $uri .= ($this->obj->uri->segment(2) == FALSE) ? 'index' : $this->obj->uri->segment(2); + + $filepath = md5($uri).'/'.md5($sql); + + if (FALSE === ($cachedata = read_file($this->obj->db->cachedir.$filepath))) + { + return FALSE; + } + + return unserialize($cachedata); + } + + // -------------------------------------------------------------------- + + /** + * Write a query to a cache file + * + * @access public + * @return bool + */ + function write($sql, $object) + { + if ( ! $this->check_path()) + { + return $this->obj->db->cache_off(); + } + + $uri = ($this->obj->uri->segment(1) == FALSE) ? 'base' : $this->obj->uri->segment(2); + $uri .= ($this->obj->uri->segment(2) == FALSE) ? 'index' : $this->obj->uri->segment(2); + + $dir_path = $this->obj->db->cachedir.md5($uri).'/'; + + $filename = md5($sql); + + if ( ! @is_dir($dir_path)) + { + if ( ! @mkdir($dir_path, 0777)) + { + return FALSE; + } + + @chmod($dir_path, 0777); + } + + if (write_file($dir_path.$filename, serialize($object)) === FALSE) + { + return FALSE; + } + + @chmod($dir_path.$filename, 0777); + return TRUE; + } + + // -------------------------------------------------------------------- + + /** + * Delete cache files within a particular directory + * + * @access public + * @return bool + */ + function delete() + { + $uri = ($this->obj->uri->segment(1) == FALSE) ? 'base' : $this->obj->uri->segment(2); + $uri .= ($this->obj->uri->segment(2) == FALSE) ? 'index' : $this->obj->uri->segment(2); + + $dir_path = $this->obj->db->cachedir.md5($uri).'/'; + + delete_files($dir_path, TRUE); + } + + // -------------------------------------------------------------------- + + /** + * Delete all existing cache files + * + * @access public + * @return bool + */ + function delete_all() + { + delete_files($this->obj->db->cachedir, TRUE); + } + +} + +?> \ No newline at end of file -- cgit v1.2.3-24-g4f1b