Commit 4c3e0057 authored by Byron Jones's avatar Byron Jones

Bug 811280: Adds a caching mechanism to Bugzilla::Object to avoid querying the…

Bug 811280: Adds a caching mechanism to Bugzilla::Object to avoid querying the database repeatedly for the same information r=dkl,a=LpSolit
parent da12cec6
...@@ -144,7 +144,7 @@ sub bug { ...@@ -144,7 +144,7 @@ sub bug {
my $self = shift; my $self = shift;
require Bugzilla::Bug; require Bugzilla::Bug;
$self->{bug} ||= Bugzilla::Bug->new($self->bug_id); $self->{bug} ||= Bugzilla::Bug->new({ id => $self->bug_id, cache => 1 });
return $self->{bug}; return $self->{bug};
} }
......
...@@ -318,9 +318,13 @@ sub new { ...@@ -318,9 +318,13 @@ sub new {
# If we get something that looks like a word (not a number), # If we get something that looks like a word (not a number),
# make it the "name" param. # make it the "name" param.
if (!defined $param || (!ref($param) && $param !~ /^\d+$/)) { if (!defined $param
|| (!ref($param) && $param =~ /\D/)
|| (ref($param) && $param->{id} =~ /\D/))
{
if ($param) { if ($param) {
$param = { name => $param }; $param = { name => ref($param) ? $param->{id} : $param,
cache => ref($param) ? $param->{cache} : 0 };
} }
else { else {
# We got something that's not a number. # We got something that's not a number.
...@@ -354,6 +358,13 @@ sub new { ...@@ -354,6 +358,13 @@ sub new {
return $self; return $self;
} }
sub cache_key {
my $class = shift;
my $key = $class->SUPER::cache_key(@_)
|| return;
return $key . ',' . Bugzilla->user->id;
}
sub check { sub check {
my $class = shift; my $class = shift;
my ($id, $field) = @_; my ($id, $field) = @_;
...@@ -3216,7 +3227,8 @@ sub component_obj { ...@@ -3216,7 +3227,8 @@ sub component_obj {
my ($self) = @_; my ($self) = @_;
return $self->{component_obj} if defined $self->{component_obj}; return $self->{component_obj} if defined $self->{component_obj};
return {} if $self->{error}; return {} if $self->{error};
$self->{component_obj} = new Bugzilla::Component($self->{component_id}); $self->{component_obj} =
new Bugzilla::Component({ id => $self->{component_id}, cache => 1 });
return $self->{component_obj}; return $self->{component_obj};
} }
...@@ -3424,7 +3436,8 @@ sub product { ...@@ -3424,7 +3436,8 @@ sub product {
sub product_obj { sub product_obj {
my $self = shift; my $self = shift;
return {} if $self->{error}; return {} if $self->{error};
$self->{product_obj} ||= new Bugzilla::Product($self->{product_id}); $self->{product_obj} ||=
new Bugzilla::Product({ id => $self->{product_id}, cache => 1 });
return $self->{product_obj}; return $self->{product_obj};
} }
......
...@@ -130,7 +130,8 @@ sub is_about_attachment { ...@@ -130,7 +130,8 @@ sub is_about_attachment {
sub attachment { sub attachment {
my ($self) = @_; my ($self) = @_;
return undef if not $self->is_about_attachment; return undef if not $self->is_about_attachment;
$self->{attachment} ||= new Bugzilla::Attachment($self->extra_data); $self->{attachment} ||=
new Bugzilla::Attachment({ id => $self->extra_data, cache => 1 });
return $self->{attachment}; return $self->{attachment};
} }
......
...@@ -59,6 +59,8 @@ sub new { ...@@ -59,6 +59,8 @@ sub new {
sub _init { sub _init {
my $class = shift; my $class = shift;
my ($param) = @_; my ($param) = @_;
my $object = $class->_cache_get($param);
return $object if $object;
my $dbh = Bugzilla->dbh; my $dbh = Bugzilla->dbh;
my $columns = join(',', $class->_get_db_columns); my $columns = join(',', $class->_get_db_columns);
my $table = $class->DB_TABLE; my $table = $class->DB_TABLE;
...@@ -69,7 +71,6 @@ sub _init { ...@@ -69,7 +71,6 @@ sub _init {
if (ref $param eq 'HASH') { if (ref $param eq 'HASH') {
$id = $param->{id}; $id = $param->{id};
} }
my $object;
if (defined $id) { if (defined $id) {
# We special-case if somebody specifies an ID, so that we can # We special-case if somebody specifies an ID, so that we can
...@@ -112,9 +113,37 @@ sub _init { ...@@ -112,9 +113,37 @@ sub _init {
"SELECT $columns FROM $table WHERE $condition", undef, @values); "SELECT $columns FROM $table WHERE $condition", undef, @values);
} }
$class->_cache_set($param, $object) if $object;
return $object; return $object;
} }
# Provides a mechanism for objects to be cached in the request_cahce
sub _cache_get {
my $class = shift;
my ($param) = @_;
my $cache_key = $class->cache_key($param)
|| return;
return Bugzilla->request_cache->{$cache_key};
}
sub _cache_set {
my $class = shift;
my ($param, $object) = @_;
my $cache_key = $class->cache_key($param)
|| return;
Bugzilla->request_cache->{$cache_key} = $object;
}
sub cache_key {
my $class = shift;
my ($param) = @_;
if (ref($param) && $param->{cache} && ($param->{id} || $param->{name})) {
return $class . ',' . ($param->{id} || $param->{name});
} else {
return;
}
}
sub check { sub check {
my ($invocant, $param) = @_; my ($invocant, $param) = @_;
my $class = ref($invocant) || $invocant; my $class = ref($invocant) || $invocant;
......
...@@ -810,8 +810,8 @@ sub flag_types { ...@@ -810,8 +810,8 @@ sub flag_types {
sub classification { sub classification {
my $self = shift; my $self = shift;
$self->{'classification'} ||= $self->{'classification'} ||=
new Bugzilla::Classification($self->classification_id); new Bugzilla::Classification({ id => $self->classification_id, cache => 1 });
return $self->{'classification'}; return $self->{'classification'};
} }
......
...@@ -284,7 +284,7 @@ sub get_attachment_link { ...@@ -284,7 +284,7 @@ sub get_attachment_link {
my $dbh = Bugzilla->dbh; my $dbh = Bugzilla->dbh;
$user ||= Bugzilla->user; $user ||= Bugzilla->user;
my $attachment = new Bugzilla::Attachment($attachid); my $attachment = new Bugzilla::Attachment({ id => $attachid, cache => 1 });
if ($attachment) { if ($attachment) {
my $title = ""; my $title = "";
...@@ -334,10 +334,10 @@ sub get_bug_link { ...@@ -334,10 +334,10 @@ sub get_bug_link {
$options->{user} ||= Bugzilla->user; $options->{user} ||= Bugzilla->user;
my $dbh = Bugzilla->dbh; my $dbh = Bugzilla->dbh;
if (defined $bug) { if (defined $bug && $bug ne '') {
if (!blessed($bug)) { if (!blessed($bug)) {
require Bugzilla::Bug; require Bugzilla::Bug;
$bug = new Bugzilla::Bug($bug); $bug = new Bugzilla::Bug({ id => $bug, cache => 1 });
} }
return $link_text if $bug->{error}; return $link_text if $bug->{error};
} }
......
...@@ -140,7 +140,7 @@ sub validateID { ...@@ -140,7 +140,7 @@ sub validateID {
{ attach_id => scalar $cgi->param($param) }); { attach_id => scalar $cgi->param($param) });
# Make sure the attachment exists in the database. # Make sure the attachment exists in the database.
my $attachment = new Bugzilla::Attachment($attach_id) my $attachment = new Bugzilla::Attachment({ id => $attach_id, cache => 1 })
|| ThrowUserError("invalid_attach_id", { attach_id => $attach_id }); || ThrowUserError("invalid_attach_id", { attach_id => $attach_id });
return $attachment if ($dont_validate_access || check_can_access($attachment)); return $attachment if ($dont_validate_access || check_can_access($attachment));
...@@ -152,7 +152,7 @@ sub check_can_access { ...@@ -152,7 +152,7 @@ sub check_can_access {
my $user = Bugzilla->user; my $user = Bugzilla->user;
# Make sure the user is authorized to access this attachment's bug. # Make sure the user is authorized to access this attachment's bug.
Bugzilla::Bug->check($attachment->bug_id); Bugzilla::Bug->check({ id => $attachment->bug_id, cache => 1 });
if ($attachment->isprivate && $user->id != $attachment->attacher->id if ($attachment->isprivate && $user->id != $attachment->attacher->id
&& !$user->is_insider) && !$user->is_insider)
{ {
...@@ -414,7 +414,7 @@ sub diff { ...@@ -414,7 +414,7 @@ sub diff {
# HTML page. # HTML page.
sub viewall { sub viewall {
# Retrieve and validate parameters # Retrieve and validate parameters
my $bug = Bugzilla::Bug->check(scalar $cgi->param('bugid')); my $bug = Bugzilla::Bug->check({ id => scalar $cgi->param('bugid'), cache => 1 });
my $attachments = Bugzilla::Attachment->get_attachments_by_bug($bug); my $attachments = Bugzilla::Attachment->get_attachments_by_bug($bug);
# Ignore deleted attachments. # Ignore deleted attachments.
......
...@@ -47,7 +47,7 @@ Bugzilla->switch_to_shadow_db unless $user->id; ...@@ -47,7 +47,7 @@ Bugzilla->switch_to_shadow_db unless $user->id;
if ($single) { if ($single) {
my $id = $cgi->param('id'); my $id = $cgi->param('id');
push @bugs, Bugzilla::Bug->check($id); push @bugs, Bugzilla::Bug->check({ id => $id, cache => 1 });
if (defined $cgi->param('mark')) { if (defined $cgi->param('mark')) {
foreach my $range (split ',', $cgi->param('mark')) { foreach my $range (split ',', $cgi->param('mark')) {
if ($range =~ /^(\d+)-(\d+)$/) { if ($range =~ /^(\d+)-(\d+)$/) {
...@@ -67,7 +67,7 @@ if ($single) { ...@@ -67,7 +67,7 @@ if ($single) {
foreach my $bug_id (@ids) { foreach my $bug_id (@ids) {
next unless $bug_id; next unless $bug_id;
my $bug = new Bugzilla::Bug($bug_id); my $bug = new Bugzilla::Bug({ id => $bug_id, cache => 1 });
if (!$bug->{error}) { if (!$bug->{error}) {
push(@check_bugs, $bug); push(@check_bugs, $bug);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment