Permalink
Browse files

Cache the result of User_Model::groups() and Group_Model::users() and

invalidate it on save/delete for efficiency.  Fixes #1529.
  • Loading branch information...
1 parent c77cf83 commit c260f4386a2cafbc200b2bb8a235fbb78e01aa11 @bharat bharat committed Dec 16, 2010
Showing with 14 additions and 2 deletions.
  1. +7 −1 modules/user/models/group.php
  2. +7 −1 modules/user/models/user.php
@@ -19,6 +19,7 @@
*/
class Group_Model_Core extends ORM implements Group_Definition {
protected $has_and_belongs_to_many = array("users");
+ protected $users_cache = null;
/**
* @see ORM::delete()
@@ -28,10 +29,14 @@ public function delete($id=null) {
module::event("group_before_delete", $this);
parent::delete($id);
module::event("group_deleted", $old);
+ unset($this->users_cache);
}
public function users() {
- return $this->users->find_all();
+ if (!$this->users_cache) {
+ $this->users_cache = $this->users->find_all();
+ }
+ return $this->users_cache;
}
/**
@@ -60,6 +65,7 @@ public function save() {
module::event("group_updated", $original, $this);
}
+ unset($this->users_cache);
return $this;
}
@@ -20,6 +20,7 @@
class User_Model_Core extends ORM implements User_Definition {
protected $has_and_belongs_to_many = array("groups");
protected $password_length = null;
+ protected $groups_cache = null;
public function __set($column, $value) {
switch ($column) {
@@ -43,6 +44,7 @@ public function delete($id=null) {
module::event("user_before_delete", $this);
parent::delete($id);
module::event("user_deleted", $old);
+ unset($this->groups_cache);
}
/**
@@ -56,7 +58,10 @@ public function avatar_url($size=80, $default=null) {
}
public function groups() {
- return $this->groups->find_all();
+ if (!$this->groups_cache) {
+ $this->groups_cache = $this->groups->find_all();
+ }
+ return $this->groups_cache;
}
/**
@@ -108,6 +113,7 @@ public function save() {
module::event("user_updated", $original, $this);
}
+ unset($this->groups_cache);
return $this;
}

0 comments on commit c260f43

Please sign in to comment.