From 7f2f037406455e19120630add3769592ec99f88f Mon Sep 17 00:00:00 2001 From: Deon George Date: Tue, 29 Apr 2014 14:26:56 +1000 Subject: [PATCH] Added ORM caching --- classes/Kohana/Database/LDAP/Result.php | 4 ++-- classes/Kohana/Database/LDAP/Result/Cached.php | 8 ++++++++ classes/Kohana/Database/LDAP/Search.php | 8 ++++++-- classes/Kohana/Database/LDAP/Search/Result.php | 2 +- tests/classes/LDAPCaching.php | 5 +++-- 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/classes/Kohana/Database/LDAP/Result.php b/classes/Kohana/Database/LDAP/Result.php index 55587ff..f497b96 100644 --- a/classes/Kohana/Database/LDAP/Result.php +++ b/classes/Kohana/Database/LDAP/Result.php @@ -83,7 +83,7 @@ abstract class Kohana_Database_LDAP_Result extends Database_Result { * @override We cant go backwards! */ public function prev() { - throw new Kohana_Exception('Cant go backwards'); + throw HTTP_Exception::factory(501,'Cant go backwards :method',array(':method'=>__METHOD__)); } /** @@ -164,7 +164,7 @@ abstract class Kohana_Database_LDAP_Result extends Database_Result { if ($key === NULL AND $value === NULL) { // Indexed rows foreach ($this as $dn => $row) - $result[$dn] = $row; + array_push($result,$row); } elseif ($key === NULL) { throw HTTP_Exception::factory(501,'Not implemented :method',array(':method'=>__METHOD__)); diff --git a/classes/Kohana/Database/LDAP/Result/Cached.php b/classes/Kohana/Database/LDAP/Result/Cached.php index cfc60cd..4e97b8e 100644 --- a/classes/Kohana/Database/LDAP/Result/Cached.php +++ b/classes/Kohana/Database/LDAP/Result/Cached.php @@ -10,6 +10,7 @@ * @license http://dev.phpldapadmin.org/license.html */ abstract class Kohana_Database_LDAP_Result_Cached extends Database_Result_Cached { + protected $cache_key = NULL; public function __construct(array $result, $sql, $as_object = NULL) { @@ -24,6 +25,13 @@ abstract class Kohana_Database_LDAP_Result_Cached extends Database_Result_Cached // Cached results do not use resources } + public function cache_key($key=NULL) { + if (is_null($key)) + return $this->cache_key; + + $this->cache_key = $key; + } + public function cached() { return $this; } diff --git a/classes/Kohana/Database/LDAP/Search.php b/classes/Kohana/Database/LDAP/Search.php index 86f1948..8639458 100644 --- a/classes/Kohana/Database/LDAP/Search.php +++ b/classes/Kohana/Database/LDAP/Search.php @@ -188,6 +188,9 @@ abstract class Kohana_Database_LDAP_Search { * @uses Kohana::$cache_life */ public function cached($lifetime=NULL,$force=FALSE) { + if (! Kohana::$caching) + $lifetime = NULL; + if ($lifetime === NULL) { // Use the global setting $lifetime = Kohana::$cache_life; @@ -239,14 +242,15 @@ abstract class Kohana_Database_LDAP_Search { $result = new Database_LDAP_Search_Result; foreach ($this->_base as $base) { - if ($this->_lifetime !== NULL AND $this->_db->caching()) { + if (($this->_lifetime !== NULL AND $this->_db->caching()) OR (Kohana::$caching AND $this->_db->caching() AND $this->cached())) { // Set the cache key based on the database instance name and SQL $cache_key = 'Database::query("'.$this->_db.'","'.$base.'","'.$this->_scope.'","'.$this->_filter.'")'; // Read the cache first to delete a possible hit with lifetime <= 0 if (($search = Kohana::cache($cache_key, NULL, $this->_lifetime)) !== NULL AND ! $this->_force_execute) { // Return a cached result - $result[$base] = new Database_LDAP_Result_Cached($search, array('b'=>$base,'s'=>$this->_scope,'f'=>$this->_filter), $as_object, $object_params); + $result[$base] = new Database_LDAP_Result_Cached($search, array('b'=>$base,'s'=>$this->_scope,'f'=>$this->_filter), $as_object); + $result[$base]->cache_key($cache_key); } } diff --git a/classes/Kohana/Database/LDAP/Search/Result.php b/classes/Kohana/Database/LDAP/Search/Result.php index ec47922..8ba54fc 100644 --- a/classes/Kohana/Database/LDAP/Search/Result.php +++ b/classes/Kohana/Database/LDAP/Search/Result.php @@ -75,7 +75,7 @@ abstract class Kohana_Database_LDAP_Search_Result implements ArrayAccess,Iterato } public function rewind() { - reset($this->result); + is_resource($this->result) ? rewind($this->result) : reset($this->result); if (! current($this->result)->valid()) $this->next(FALSE); diff --git a/tests/classes/LDAPCaching.php b/tests/classes/LDAPCaching.php index 26e4ac2..7595580 100644 --- a/tests/classes/LDAPCaching.php +++ b/tests/classes/LDAPCaching.php @@ -48,10 +48,11 @@ Class LDAPCaching extends Unittest_TestCase { ->cached($cached,$force) ->execute(); - if ($expect) + if (! Kohana::$caching OR $expect) { $this->assertInstanceOf('Database_LDAP_Result',$u['']); - else + } else { $this->assertInstanceOf('Database_LDAP_Result_Cached',$u['']); + } $x->disconnect(); }