[Slim-Checkins] r10208 - in /branches/6.5/server: Slim/Utils/Cache.pm slimserver.pl

andy at svn.slimdevices.com andy at svn.slimdevices.com
Thu Oct 5 08:28:27 PDT 2006


Author: andy
Date: Thu Oct  5 08:28:25 2006
New Revision: 10208

URL: http://svn.slimdevices.com?rev=10208&view=rev
Log:
Instead of relying on FileCache's auto-purge, clean the cache up ourselves once an hour or so

Modified:
    branches/6.5/server/Slim/Utils/Cache.pm
    branches/6.5/server/slimserver.pl

Modified: branches/6.5/server/Slim/Utils/Cache.pm
URL: http://svn.slimdevices.com/branches/6.5/server/Slim/Utils/Cache.pm?rev=10208&r1=10207&r2=10208&view=diff
==============================================================================
--- branches/6.5/server/Slim/Utils/Cache.pm (original)
+++ branches/6.5/server/Slim/Utils/Cache.pm Thu Oct  5 08:28:25 2006
@@ -41,7 +41,21 @@
 use strict;
 use base qw(Class::Singleton);
 use Cache::FileCache ();
+use Slim::Utils::Misc;
 use Slim::Utils::Prefs;
+use Slim::Utils::Timers;
+
+my $PURGE_INTERVAL = 3600;
+
+sub init {
+	my $class = shift;
+	
+	# Clean up the cache at startup
+	__PACKAGE__->new->purge();
+	
+	# And continue to clean it up regularly
+	Slim::Utils::Timers::setTimer( undef, time() + $PURGE_INTERVAL, \&cleanup );
+}
 
 sub new { shift->instance(@_) }
 
@@ -49,11 +63,10 @@
 	my $class = shift;
 	
 	my $cache = Cache::FileCache->new( {
-		namespace           => 'FileCache',
-		default_expires_in  => $Cache::FileCache::EXPIRES_NEVER,
-		cache_root          => Slim::Utils::Prefs::get('cachedir'),
-		directory_umask     => umask(),
-		auto_purge_interval => '1 hour',
+		namespace          => 'FileCache',
+		default_expires_in => $Cache::FileCache::EXPIRES_NEVER,
+		cache_root         => Slim::Utils::Prefs::get('cachedir'),
+		directory_umask    => umask(),
 	} );
 	
 	my $self = bless {
@@ -78,4 +91,36 @@
 	return $self;
 }
 
+sub cleanup {
+	
+	# Use the same method the Scheduler uses to run only when idle
+	my $busy;
+	
+	for my $client ( Slim::Player::Client::clients() ) {
+
+		if (Slim::Player::Source::playmode($client) eq 'play' && 
+		    $client->isPlayer() && 
+		    $client->usage() < 0.5) {
+
+			$busy = 1;
+			last;
+		}
+	}
+	
+	if ( !$busy ) {
+		$::d_server && msg("Cache: Cleaning up expired items...\n");
+	
+		__PACKAGE__->new->purge();
+	
+		$::d_server && msg("Cache: Done\n");
+		
+		Slim::Utils::Timers::setTimer( undef, time() + $PURGE_INTERVAL, \&cleanup );
+	}
+	else {
+		# try again soon
+		$::d_server && msg("Cache: Skipping cleanup, server is busy\n");
+		Slim::Utils::Timers::setTimer( undef, time() + ($PURGE_INTERVAL / 6), \&cleanup );
+	}	
+}
+
 1;

Modified: branches/6.5/server/slimserver.pl
URL: http://svn.slimdevices.com/branches/6.5/server/slimserver.pl?rev=10208&r1=10207&r2=10208&view=diff
==============================================================================
--- branches/6.5/server/slimserver.pl (original)
+++ branches/6.5/server/slimserver.pl Thu Oct  5 08:28:25 2006
@@ -139,6 +139,7 @@
 use Slim::Player::Playlist;
 use Slim::Player::Sync;
 use Slim::Player::Source;
+use Slim::Utils::Cache;
 use Slim::Utils::Prefs;
 use Slim::Utils::Scanner;
 use Slim::Utils::Scheduler;
@@ -392,6 +393,9 @@
 
 	$::d_server && msg("Async Networking init...\n");
 	Slim::Networking::Async->init;
+	
+	$::d_server && msg("Cache init, cleaning up FileCache...\n");
+	Slim::Utils::Cache->init;
 	
 	unless ( $noupnp ) {
 		$::d_server && msg("UPnP init...\n");



More information about the checkins mailing list