/** * Queries the database * * @param string query * @param array binded value * @return array|object */ public function query($query, array $binds = array()) { if (strpos(strtolower($query), 'insert into') === 0 || strpos(strtolower($query), 'update') === 0 || strpos(strtolower($query), 'delete from') === 0) { //get the table $table = str_replace('insert into ', '', strtolower($query)); $table = str_replace('update ', '', $table); $table = str_replace('delete from ', '', $table); list($table, $trash) = explode(' ', $table, 2); $keys = $this->_cache->getKeys(); //invalidate all selects foreach ($this->_results as $key => $result) { $lower = strtolower($key); if (strpos($lower, 'from ' . $table) !== false || strpos($lower, 'join ' . $table) !== false || preg_match("/from\\s[a-zA-z]+\\." . $table . "\\s/i", $lower) || preg_match("/join\\s[a-zA-z]+\\." . $table . "\\s/i", $lower)) { unset($this->_results[$key]); } } foreach ($keys as $key) { $lower = strtolower($key); if (strpos($lower, 'from ' . $table) !== false || strpos($lower, 'join ' . $table) !== false || preg_match("/from\\s[a-zA-z]+\\." . $table . "\\s/i", $lower) || preg_match("/join\\s[a-zA-z]+\\." . $table . "\\s/i", $lower)) { $this->_cache->remove($key); } } } if (strpos(strtolower($query), 'select') !== 0) { return parent::query($query, $binds); } $key = $this->_getCacheKey($query, $binds); if (isset($this->_results[$key])) { $this->_cacheCount[0]++; $this->_binds = array(); return $this->_results[$key]; } else { if ($this->_cache->keyExists($key)) { $this->_cacheCount[0]++; $this->_binds = array(); return $this->_cache->get($key); } } $results = parent::query($query, $binds); $this->_cacheCount[1]++; $this->_results[$key] = $results; $this->_cache->set($key, time() . '-' . front()->uid(), $results); return $results; }
/** * Sets up the default database connection * * @return this */ public function setDatabase(array $info) { $this->_database = Eden_Mysql::i($info['host'], $info['name'], $info['user'], $info['pass']); return $this; }