[Rt-commit] rt branch, 4.4/clear-charts-cache, created. rt-4.4.2-38-gc7d7426

Jim Brandt jbrandt at bestpractical.com
Thu Sep 14 13:42:30 EDT 2017


The branch, 4.4/clear-charts-cache has been created
        at  c7d7426eafa2b8013d77f6b7c3ed33afbe10dced (commit)

- Log -----------------------------------------------------------------
commit c7d7426eafa2b8013d77f6b7c3ed33afbe10dced
Author: Jim Brandt <jbrandt at bestpractical.com>
Date:   Thu Sep 14 13:35:50 2017 -0400

    Add charts_cache timestamp and clear old cache entries
    
    In typical usage, the charts_cache in the session is cleared
    as it is used when a chart is displayed. However, for session stores
    that don't support locking, multiple requests can access the session
    at the same time and cached charts can end up being written back to
    the session after they are deleted.
    
    Add a timestamp for charts_cache entries and a function to clear
    entries older than one minute. This doesn't solve the root cause,
    but it does provide one more opportunity to clear old cache entries
    to prevent them for growing for the lifetime of the session.

diff --git a/lib/RT/Interface/Web.pm b/lib/RT/Interface/Web.pm
index 5d6b39e..b5de195 100644
--- a/lib/RT/Interface/Web.pm
+++ b/lib/RT/Interface/Web.pm
@@ -369,6 +369,8 @@ sub HandleRequest {
     $HTML::Mason::Commands::session{'home_refresh_interval'} = $ARGS->{'HomeRefreshInterval'}
         if ( $ARGS->{'HomeRefreshInterval'} );
 
+    RT::Interface::Web::Session::ClearExpiredChartsCache(\%HTML::Mason::Commands::session);
+
     # Process per-page global callbacks
     $HTML::Mason::Commands::m->callback( %$ARGS, CallbackName => 'Default', CallbackPage => '/autohandler' );
 
diff --git a/lib/RT/Interface/Web/Session.pm b/lib/RT/Interface/Web/Session.pm
index 03cc325..33c2b67 100644
--- a/lib/RT/Interface/Web/Session.pm
+++ b/lib/RT/Interface/Web/Session.pm
@@ -316,6 +316,31 @@ sub ClearByUser {
     $self->ClearOrphanLockFiles if $deleted;
 }
 
+=head3 ClearExpiredChartsCache
+
+Check the timestamp on the charts_cache in the session and clear any
+older than one minute.
+
+For most typical use, the charts_cache should be empty as it is cleared as
+cached items are used. However, for DBs without session locking, some keys
+can be added back as multiple processes access the session.
+
+=cut
+
+sub ClearExpiredChartsCache {
+    my $session = shift;
+
+    foreach my $cache_key ( keys %{$session->{'charts_cache'}} ){
+        my $cache_time = $session->{'charts_cache'}{$cache_key}{'timestamp'};
+        if ( (time - $cache_time) > 60 ){
+            RT::Logger->debug("Clearing expired cache key $cache_key");
+            delete $session->{'charts_cache'}{$cache_key};
+            $session->{'i'}++;
+        }
+    }
+    return;
+}
+
 sub TIEHASH {
     my $self = shift;
     my $id = shift;
diff --git a/share/html/Search/Elements/Chart b/share/html/Search/Elements/Chart
index 6c0cd87..0899102 100644
--- a/share/html/Search/Elements/Chart
+++ b/share/html/Search/Elements/Chart
@@ -73,7 +73,7 @@ my $query_string = $m->comp('/Elements/QueryString', %ARGS, GroupBy => \@GroupBy
 <% loc('Graphical charts are not available.') %><br />
 % } else {
 % my $key = Digest::MD5::md5_hex( rand(1024) );
-% $session{'charts_cache'}{$key} = { columns => \%columns, report => $report->Serialize };
+% $session{'charts_cache'}{$key} = { columns => \%columns, report => $report->Serialize, timestamp => time };
 % $session{'i'}++;
 <img src="<% RT->Config->Get('WebPath') %>/Search/Chart?Cache=<% $key |un %>&<% $query_string |n %>" />
 % }

-----------------------------------------------------------------------


More information about the rt-commit mailing list