Permalink
Browse files

Added writer cache.

  • Loading branch information...
1 parent 198bfc2 commit e4fcee484bac4d29d0cc06e20a62794de74c4b8c @gabordemooij committed Nov 21, 2012
Showing with 89 additions and 0 deletions.
  1. +48 −0 RedBean/QueryWriter/AQueryWriter.php
  2. +40 −0 testing/RedUNIT/Base/Writecache.php
  3. +1 −0 testing/helpers/selector.php
@@ -49,6 +49,26 @@
*/
protected $quoteCharacter = '';
+ /**
+ * Flag indicating whether we are using caching.
+ * @var boolean
+ */
+ protected $flagUseCache = false;
+
+ /**
+ * Flush key. A hash-like string that acts as a lock to verify
+ * no intermediate queries have taken place.
+ * @var string
+ */
+ protected $flushKey = '';
+
+ /**
+ * A storage for cached databasse result rows.
+ * Each entry is a row.
+ * @var array
+ */
+ protected $cache = array();
+
/**
* Constructor
@@ -240,6 +260,19 @@ protected function insertRecord( $table, $insertcolumns, $insertvalues ) {
*/
public function selectRecord( $type, $conditions, $addSql=null, $delete=null, $inverse=false, $all=false ) {
if (!is_array($conditions)) throw new Exception('Conditions must be an array');
+ if (!$delete && $this->flagUseCache) {
+ $key = serialize(array($type,$conditions,$addSql,$inverse,$all));
+ if ($this->flushKey!==$this->adapter->getSQL()) {
+ //If SQL has been taken place outside of this method then something else then
+ //a select query might have happened!
+ $this->cache = array();
+ }
+ else {
+ if (isset($this->cache[$key])) {
+ return $this->cache[$key];
+ }
+ }
+ }
$table = $this->safeTable($type);
$sqlConditions = array();
$bindings=array();
@@ -293,6 +326,10 @@ public function selectRecord( $type, $conditions, $addSql=null, $delete=null, $i
}
$sql = (($delete) ? 'DELETE FROM ' : 'SELECT * FROM ').$table.$sql;
$rows = $this->adapter->get($sql,$bindings);
+ if (!$delete && $this->flagUseCache) {
+ $this->flushKey = $this->adapter->getSQL();
+ $this->cache[$key] = $rows;
+ }
return $rows;
}
@@ -477,5 +514,16 @@ protected function startsWithZeros($value) {
public function getValue(){
return $this->svalue;
}
+
+ /**
+ * Turns caching on or off. Default: off.
+ * If caching is turned on retrieval queries fired after eachother will
+ * use a result row cache.
+ *
+ * @param boolean $yesNo
+ */
+ public function setUseCache($yesNo) {
+ $this->flagUseCache = (boolean) $yesNo;
+ }
}
@@ -0,0 +1,40 @@
+<?php
+/**
+ * RedUNIT_Base_Writecace
+ *
+ * @file RedUNIT/Base/Writecache.php
+ * @description Tests the Query Writer cache implemented in the
+ * abstract RedBean_QueryWriter_AQueryWriter class.
+ * @author Gabor de Mooij
+ * @license BSD
+ *
+ *
+ * (c) G.J.G.T. (Gabor) de Mooij
+ * This source file is subject to the BSD/GPLv2 License that is bundled
+ * with this source code in the file license.txt.
+ */
+class RedUNIT_Base_Writecache extends RedUNIT_Base {
+
+ /**
+ * Begin testing.
+ * This method runs the actual test pack.
+ *
+ * @return void
+ */
+ public function run(){
+ R::nuke();
+ $logger = RedBean_Plugin_QueryLogger::getInstanceAndAttach(R::$adapter);
+ $book = R::dispense('book')->setAttr('title','ABC');
+ $id = R::store($book);
+ $logger->clear();
+ $book = R::load('book',$id);
+ $book = R::load('book',$id);
+ asrt(count($logger->grep('SELECT')),2);
+ R::$writer->setUseCache(true);
+ $logger->clear();
+ $book = R::load('book',$id);
+ $book = R::load('book',$id);
+ asrt(count($logger->grep('SELECT')),1);
+ R::$writer->setUseCache(false);
+ }
+}
@@ -98,6 +98,7 @@ function activate_driver($d) {
'Base/Dup',
'Base/Update',
'Base/Batch',
+ 'Base/Writecache',
'Base/Relations',
'Base/Association',
'Base/Aliasing',

0 comments on commit e4fcee4

Please sign in to comment.