Index: /branches/release-39/lib/MT/Core.pm
===================================================================
--- /branches/release-39/lib/MT/Core.pm (revision 2377)
+++ /branches/release-39/lib/MT/Core.pm (revision 2505)
@@ -774,4 +774,11 @@
             },
         },
+        'RemoveExpiredSearchCaches' => {
+            label => 'Remove Expired Search Caches',
+            frequency => 60 * 60 * 24,   # once a day
+            code => sub {
+                MT::Core->remove_expired_search_caches;
+            },
+        },
     };
 }
@@ -803,4 +810,13 @@
         $s->remove if !$s->get('remember');
     }
+    return '';
+}
+
+sub remove_expired_search_caches {
+    require MT::Session;
+
+    MT::Session->remove(
+        { kind => 'CS', start => [ undef, time - 60 * 60 ] },
+        { range => { start => 1 } } );
     return '';
 }
Index: /branches/release-39/lib/MT/App/Search.pm
===================================================================
--- /branches/release-39/lib/MT/App/Search.pm (revision 2463)
+++ /branches/release-39/lib/MT/App/Search.pm (revision 2505)
@@ -173,5 +173,8 @@
         return;
     }
-    $app->{cache_driver} = $cache_driver->new( ttl => $app->config->SearchCacheTTL );
+    $app->{cache_driver} = $cache_driver->new(
+        ttl => $app->config->SearchCacheTTL,
+        kind => 'CS',
+    );
 }
 
@@ -877,4 +880,7 @@
     my ( $cb, $app ) = @_;
     alarm(0) if $app->{__have_throttle};
+    if ( my $cache_driver = $app->{cache_driver} ) {
+        $cache_driver->purge_stale( 2 * $app->config->SearchCacheTTL );
+    }
     1;
 }
Index: /branches/release-39/lib/MT/Memcached.pm
===================================================================
--- /branches/release-39/lib/MT/Memcached.pm (revision 2253)
+++ /branches/release-39/lib/MT/Memcached.pm (revision 2505)
@@ -51,4 +51,9 @@
         $driver_class->disconnect_all;
     }
+}
+
+# method for MT::Cache interface
+sub purge_stale {
+    1;
 }
 
Index: /branches/release-39/lib/MT/Cache/Null.pm
===================================================================
--- /branches/release-39/lib/MT/Cache/Null.pm (revision 2027)
+++ /branches/release-39/lib/MT/Cache/Null.pm (revision 2505)
@@ -54,4 +54,9 @@
 }
 
+sub purge_stale {
+    my MT::Cache::Null $self = shift;
+    return;
+}
+
 sub flush_all {
     my MT::Cache::Null $self = shift;
Index: /branches/release-39/lib/MT/Cache/Session.pm
===================================================================
--- /branches/release-39/lib/MT/Cache/Session.pm (revision 1596)
+++ /branches/release-39/lib/MT/Cache/Session.pm (revision 2505)
@@ -14,4 +14,5 @@
     my (%param) = @_;
     $param{'ttl'} ||= 0;
+    $param{'kind'} ||= 'CO';
     my $self = bless \%param, $class;
     return $self;
@@ -25,8 +26,8 @@
     if ( $self->{ttl} ) {
         $record = MT::Session::get_unexpired_value(
-            $self->{ttl}, { id => $key, kind => 'CO' } );
+            $self->{ttl}, { id => $key, kind => $self->{kind} } );
     }
     else {
-        $record = MT::Session->load( { id => $key, kind => 'CO' } );
+        $record = MT::Session->load( { id => $key, kind => $self->{kind} } );
     }
     return unless $record;
@@ -38,5 +39,5 @@
     my @keys = @_;
 
-    my @tmp = MT::Session->load( { id => \@keys, kind => 'CO' } )
+    my @tmp = MT::Session->load( { id => \@keys, kind => $self->{kind} } )
         or return;
 
@@ -61,5 +62,5 @@
     my MT::Cache::Session $self = shift;
     my ($key, $time) = @_;
-    MT::Session->remove( { id => $key, kind => 'CO' } )
+    MT::Session->remove( { id => $key, kind => $self->{kind} } )
         or return 0;
     1;
@@ -86,5 +87,5 @@
     my $cache = MT::Session->load({
         id   => $key,
-        kind => 'CO',
+        kind => $self->{kind},
     });
     $cache->remove() if $cache;
@@ -92,5 +93,5 @@
     $cache->set_values({
         id    => $key,
-        kind  => 'CO',
+        kind  => $self->{kind},
         start => time,
         data  => $val,
@@ -101,5 +102,15 @@
 sub flush_all {
     my MT::Cache::Session $self = shift;
-    MT::Session->remove({ kind => 'CO' });
+    MT::Session->remove({ kind => $self->{kind} });
+}
+
+sub purge_stale {
+    my MT::Cache::Session $self = shift;
+    my ( $ttl ) = @_;
+    MT::Session->remove(
+        { kind => $self->{kind}, start => [ undef, time - $ttl ] },
+        { range => { start => 1 } }
+    );
+    1;
 }
 
@@ -150,4 +161,9 @@
 Empty all the caches.
 
+=head2 $cache->purge_stale
+
+Deletes all the cached objects that are already too old.
+Takes one argument: $ttl specifies the duration that is considered stale.
+
 =head1 AUTHOR & COPYRIGHT
 
