From 008cc4253b8ec051d0061fcffd646720530b0946 Mon Sep 17 00:00:00 2001 From: Shish Date: Sat, 23 Jun 2012 23:57:34 +0100 Subject: [PATCH] wibble database / cache connection functions, make them repeatable, and set persistant DB connections as an option --- core/database.class.php | 39 +++++++++++++++++++++++++-------------- core/sys_config.inc.php | 1 + 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/core/database.class.php b/core/database.class.php index 3691ff6f..d8841be7 100644 --- a/core/database.class.php +++ b/core/database.class.php @@ -276,6 +276,30 @@ class Database { * stored in the config file */ public function Database() { + $this->connect_cache(); + $this->connect_db(); + } + + private function connect_cache() { + if(!is_null($this->cache)) return; + + $matches = array(); + if(defined("CACHE_DSN") && CACHE_DSN && preg_match("#(memcache|apc)://(.*)#", CACHE_DSN, $matches)) { + if($matches[1] == "memcache") { + $this->cache = new MemcacheCache($matches[2]); + } + else if($matches[1] == "apc") { + $this->cache = new APCCache($matches[2]); + } + } + else { + $this->cache = new NoCache(); + } + } + + private function connect_db() { + if(!is_null($this->db)) return; + # FIXME: detect ADODB URI, automatically translate PDO DSN /* @@ -289,7 +313,7 @@ class Database { if(preg_match("/password=([^;]*)/", DATABASE_DSN, $matches)) $db_pass=$matches[1]; $db_params = array( - PDO::ATTR_PERSISTENT => true, + PDO::ATTR_PERSISTENT => DATABASE_KA, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ); if(defined("HIPHOP")) $this->db = new PDO(DATABASE_DSN, $db_user, $db_pass); @@ -309,19 +333,6 @@ class Database { die('Unknown PDO driver: '.$db_proto); } - $matches = array(); - if( defined("CACHE_DSN") && CACHE_DSN && preg_match("#(memcache|apc)://(.*)#", CACHE_DSN, $matches)) { - if($matches[1] == "memcache") { - $this->cache = new MemcacheCache($matches[2]); - } - else if($matches[1] == "apc") { - $this->cache = new APCCache($matches[2]); - } - } - else { - $this->cache = new NoCache(); - } - $this->engine->init($this->db); } diff --git a/core/sys_config.inc.php b/core/sys_config.inc.php index 8fa88dcf..6473ebc5 100644 --- a/core/sys_config.inc.php +++ b/core/sys_config.inc.php @@ -20,6 +20,7 @@ */ function _d($name, $value) {if(!defined($name)) define($name, $value);} _d("DATABASE_DSN", null); // string PDO database connection details +_d("DATABASE_KA", true); // string Keep database connection alive _d("CACHE_DSN", null); // string cache connection details _d("DEBUG", false); // boolean print various debugging details _d("DEBUG_SQL", false); // boolean dump SQL queries to data/sql.log