[Slim-Checkins] r10884 - in /trunk/server/Slim/Music: Info.pm TitleFormatter.pm

adrian at svn.slimdevices.com adrian at svn.slimdevices.com
Wed Dec 6 13:54:47 PST 2006


Author: adrian
Date: Wed Dec  6 13:54:46 2006
New Revision: 10884

URL: http://svn.slimdevices.com?rev=10884&view=rev
Log:
Bug: N/A
Description: make displayText cache more inteligent:
- don't cache CURRTIME/SHORTDATE/LONGDATE
- add flag to addFormat for additional formats which should not be cached
- add ability to selectively prune formats to clearFormatDisplayCache

Modified:
    trunk/server/Slim/Music/Info.pm
    trunk/server/Slim/Music/TitleFormatter.pm

Modified: trunk/server/Slim/Music/Info.pm
URL: http://svn.slimdevices.com/trunk/server/Slim/Music/Info.pm?rev=10884&r1=10883&r2=10884&view=diff
==============================================================================
--- trunk/server/Slim/Music/Info.pm (original)
+++ trunk/server/Slim/Music/Info.pm Wed Dec  6 13:54:46 2006
@@ -164,17 +164,34 @@
 }
 
 sub clearFormatDisplayCache {
-
-	%currentTitles   = ();
-	%currentBitrates = ();
-
-	$musicInfoTextCache = undef;
-
-	foreach my $client ( Slim::Player::Client::clients() ) {
-		$client->musicInfoTextCache(undef);
-	}
-
-	return 1;
+	my $format = shift; # if set only clear cached formats including this string
+
+	if ($format) {
+		# prune matching entries from non client cache
+		for my $key ( keys %$musicInfoTextCache ) {
+			delete $musicInfoTextCache->{$key} if ($key =~ /$format/);
+		}
+
+		# prune matching entries from client caches
+		for my $client ( Slim::Player::Client::clients() ) {
+			if (my $cache = $client->musicInfoTextCache) {
+				for my $key ( keys %$cache ) {
+					delete $cache->{$key} if ($key =~ /$format/);
+				}
+			}
+		}
+
+	} else {
+		# remove all cached entries
+		$musicInfoTextCache = undef;
+
+		foreach my $client ( Slim::Player::Client::clients() ) {
+			$client->musicInfoTextCache(undef);
+		}
+
+		%currentTitles   = ();
+		%currentBitrates = ();
+	}
 }
 
 sub updateCacheEntry {
@@ -513,17 +530,24 @@
 
 		if (exists $cache->{$format}) {
 			return $cache->{$format};
+
+		} elsif (Slim::Music::TitleFormatter::cacheFormat($format)) {
+			return $cache->{$format} = Slim::Music::TitleFormatter::infoFormat($obj, $format);
+
 		} else {
-			return $cache->{$format} = Slim::Music::TitleFormatter::infoFormat($obj, $format);
-		}
-	}
-
-	my $text = Slim::Music::TitleFormatter::infoFormat($obj, $format);	
+			return Slim::Music::TitleFormatter::infoFormat($obj, $format);
+		}
+	}
+
+	my $text = Slim::Music::TitleFormatter::infoFormat($obj, $format);
 
 	# Clear the cache first.
 	$cache = {};
 	$cache->{'url'}   = $url;
-	$cache->{$format} = $text;
+
+	if (Slim::Music::TitleFormatter::cacheFormat($format)) {
+		$cache->{$format} = $text;
+	}
 
 	$client ? $client->musicInfoTextCache($cache) : $musicInfoTextCache = $cache;
 

Modified: trunk/server/Slim/Music/TitleFormatter.pm
URL: http://svn.slimdevices.com/trunk/server/Slim/Music/TitleFormatter.pm?rev=10884&r1=10883&r2=10884&view=diff
==============================================================================
--- trunk/server/Slim/Music/TitleFormatter.pm (original)
+++ trunk/server/Slim/Music/TitleFormatter.pm Wed Dec  6 13:54:46 2006
@@ -28,7 +28,7 @@
 use Slim::Utils::Strings qw(string);
 use Slim::Utils::Unicode;
 
-our ($elemstring, @elements, $elemRegex, %parsedFormats);
+our ($elemstring, @elements, $elemRegex, %parsedFormats, $nocacheRegex, @noCache);
 
 my $log = logger('database.info');
 
@@ -296,6 +296,12 @@
 		return (defined $output ? $output : '');
 	};
 
+	# Define built in formats which should not be cached
+	@noCache = qw( LONGDATE SHORTDATE CURRTIME );
+
+	my $nocache = join "|", @noCache;
+	$nocacheRegex = qr/$nocache/;
+
 	return 1;
 }
 
@@ -303,6 +309,7 @@
 sub addFormat {
 	my $format = shift;
 	my $formatSubRef = shift;
+	my $nocache = shift;
 
 	# only add format if it is not already defined
 	if (!defined $parsedFormats{$format}) {
@@ -316,6 +323,13 @@
 			push @elements, $format;
 			$elemstring = join "|", @elements;
 			$elemRegex = qr/$elemstring/;
+		}
+
+		if ($nocache) {
+			# format must not be cached per track
+			push @noCache, $format;
+			my $nocache = join "|", @noCache;
+			$nocacheRegex = qr/$nocache/;
 		}
 
 	} else {
@@ -482,6 +496,12 @@
 	return $parsedFormats{$formatparsed};
 }
 
+sub cacheFormat {
+	my $format = shift;
+	# return if format result is valid for duration of a track and hence can be cached
+	return ($format !~ $nocacheRegex);
+}
+
 sub infoFormat {
 	my $fileOrObj = shift; # item whose information will be formatted
 	my $str       = shift; # format string to use



More information about the checkins mailing list