Commit ddfeca8e authored by Ruslan Zakirov's avatar Ruslan Zakirov
Browse files

cache user preferences

we use LoadByCols which is cacheable, however it doesn't cache
negative hits. More and more Config options are overridable and
may be stored in DB, however often users use system's default.
Use our own cache to avoid constant queries for records that are
not there.
parent 79181011
......@@ -181,6 +181,8 @@ sub Create {
$args{'ContentType'} = 'storable';
}
delete $RT::User::PREFERENCES_CACHE{ $args{'ObjectId'} }{ $args{'Name'} }
if $args{'ObjectType'} eq 'RT::User';
$self->SUPER::Create(
Name => $args{'Name'},
......@@ -276,6 +278,11 @@ sub SetContent {
my $self = shift;
my $content = shift;
if ( $self->__Value('ObjectType') eq 'RT::User' ) {
delete $RT::User::PREFERENCES_CACHE
{ $self->__Value('ObjectId') }{ $self->__Value('Name') };
}
# Call __Value to avoid ACL check.
if ( ($self->__Value('ContentType')||'') eq 'storable' ) {
# We eval the serialization because it will lose on a coderef.
......
......@@ -190,6 +190,7 @@ sub CleanupRequest {
}
%RT::Ticket::MERGE_CACHE = ( effective => {}, merged => {} );
%RT::User::PREFERENCES_CACHE = ();
# RT::System persists between requests, so its attributes cache has to be
# cleared manually. Without this, for example, subject tags across multiple
......
......@@ -1262,7 +1262,7 @@ sub _PrefName {
$name = ref($name).'-'.$name->Id;
}
return 'Pref-'.$name;
return 'Pref-'. $name;
}
=head2 Preferences NAME/OBJ DEFAULT
......@@ -1273,15 +1273,24 @@ override the entries with user preferences.
=cut
our %PREFERENCES_CACHE = ();
sub Preferences {
my $self = shift;
my $name = _PrefName (shift);
my $name = _PrefName(shift);
my $default = shift;
my $content;
if ( exists $PREFERENCES_CACHE{ $self->id }{ $name } ) {
$content = $PREFERENCES_CACHE{ $self->id }{ $name };
}
else {
my $attr = RT::Attribute->new( $self->CurrentUser );
$attr->LoadByNameAndObject( Object => $self, Name => $name );
$PREFERENCES_CACHE{ $self->id }{ $name } = $content
= $attr->Id ? $attr->Content : undef;
}
my $content = $attr->Id ? $attr->Content : undef;
unless ( ref $content eq 'HASH' ) {
return defined $content ? $content : $default;
}
......@@ -1291,7 +1300,7 @@ sub Preferences {
exists $content->{$_} or $content->{$_} = $default->{$_};
}
} elsif (defined $default) {
$RT::Logger->error("Preferences $name for user".$self->Id." is hash but default is not");
$RT::Logger->error("Preferences $name for user #".$self->Id." is hash but default is not");
}
return $content;
}
......@@ -1310,6 +1319,8 @@ sub SetPreferences {
return (0, $self->loc("No permission to set preferences"))
unless $self->CurrentUserCanModify('Preferences');
# we clear cache in RT::Attribute
my $attr = RT::Attribute->new( $self->CurrentUser );
$attr->LoadByNameAndObject( Object => $self, Name => $name );
if ( $attr->Id ) {
......
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