Added ORM caching

This commit is contained in:
Deon George 2014-04-29 14:26:56 +10:00
parent 06bf21f1aa
commit 7f2f037406
5 changed files with 20 additions and 7 deletions

View File

@ -83,7 +83,7 @@ abstract class Kohana_Database_LDAP_Result extends Database_Result {
* @override We cant go backwards! * @override We cant go backwards!
*/ */
public function prev() { 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) { if ($key === NULL AND $value === NULL) {
// Indexed rows // Indexed rows
foreach ($this as $dn => $row) foreach ($this as $dn => $row)
$result[$dn] = $row; array_push($result,$row);
} elseif ($key === NULL) { } elseif ($key === NULL) {
throw HTTP_Exception::factory(501,'Not implemented :method',array(':method'=>__METHOD__)); throw HTTP_Exception::factory(501,'Not implemented :method',array(':method'=>__METHOD__));

View File

@ -10,6 +10,7 @@
* @license http://dev.phpldapadmin.org/license.html * @license http://dev.phpldapadmin.org/license.html
*/ */
abstract class Kohana_Database_LDAP_Result_Cached extends Database_Result_Cached { abstract class Kohana_Database_LDAP_Result_Cached extends Database_Result_Cached {
protected $cache_key = NULL;
public function __construct(array $result, $sql, $as_object = 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 // 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() { public function cached() {
return $this; return $this;
} }

View File

@ -188,6 +188,9 @@ abstract class Kohana_Database_LDAP_Search {
* @uses Kohana::$cache_life * @uses Kohana::$cache_life
*/ */
public function cached($lifetime=NULL,$force=FALSE) { public function cached($lifetime=NULL,$force=FALSE) {
if (! Kohana::$caching)
$lifetime = NULL;
if ($lifetime === NULL) { if ($lifetime === NULL) {
// Use the global setting // Use the global setting
$lifetime = Kohana::$cache_life; $lifetime = Kohana::$cache_life;
@ -239,14 +242,15 @@ abstract class Kohana_Database_LDAP_Search {
$result = new Database_LDAP_Search_Result; $result = new Database_LDAP_Search_Result;
foreach ($this->_base as $base) { 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 // Set the cache key based on the database instance name and SQL
$cache_key = 'Database::query("'.$this->_db.'","'.$base.'","'.$this->_scope.'","'.$this->_filter.'")'; $cache_key = 'Database::query("'.$this->_db.'","'.$base.'","'.$this->_scope.'","'.$this->_filter.'")';
// Read the cache first to delete a possible hit with lifetime <= 0 // 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) { if (($search = Kohana::cache($cache_key, NULL, $this->_lifetime)) !== NULL AND ! $this->_force_execute) {
// Return a cached result // 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);
} }
} }

View File

@ -75,7 +75,7 @@ abstract class Kohana_Database_LDAP_Search_Result implements ArrayAccess,Iterato
} }
public function rewind() { public function rewind() {
reset($this->result); is_resource($this->result) ? rewind($this->result) : reset($this->result);
if (! current($this->result)->valid()) if (! current($this->result)->valid())
$this->next(FALSE); $this->next(FALSE);

View File

@ -48,10 +48,11 @@ Class LDAPCaching extends Unittest_TestCase {
->cached($cached,$force) ->cached($cached,$force)
->execute(); ->execute();
if ($expect) if (! Kohana::$caching OR $expect) {
$this->assertInstanceOf('Database_LDAP_Result',$u['']); $this->assertInstanceOf('Database_LDAP_Result',$u['']);
else } else {
$this->assertInstanceOf('Database_LDAP_Result_Cached',$u['']); $this->assertInstanceOf('Database_LDAP_Result_Cached',$u['']);
}
$x->disconnect(); $x->disconnect();
} }