aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Pulverenti <luke.pulverenti@gmail.com>2013-06-10 22:34:55 -0400
committerLuke Pulverenti <luke.pulverenti@gmail.com>2013-06-10 22:34:55 -0400
commit2b4111d4fdd5042e49c51e693d0b15efe2ddc83f (patch)
tree98c7265fe98310b743060330790ee52bd807a9a3
parentc61ebf2b530d39956d545bf048ef81b7607afc50 (diff)
optimize ratings by caching
-rw-r--r--MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs2
-rw-r--r--MediaBrowser.Controller/Localization/ILocalizationManager.cs1
-rw-r--r--MediaBrowser.Server.Implementations/Localization/LocalizationManager.cs107
3 files changed, 77 insertions, 33 deletions
diff --git a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs
index ca18442a8..c29e2235d 100644
--- a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs
+++ b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs
@@ -121,7 +121,7 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
if ((DateTime.UtcNow - client.LastTimeout).TotalSeconds < 30)
{
- throw new HttpException(string.Format("Connection to {0} timed out", options.Url)) { IsTimedOut = true };
+ throw new HttpException(string.Format("Cancelling connection to {0} due to a previous timeout.", options.Url)) { IsTimedOut = true };
}
using (var message = GetHttpRequestMessage(options))
diff --git a/MediaBrowser.Controller/Localization/ILocalizationManager.cs b/MediaBrowser.Controller/Localization/ILocalizationManager.cs
index ecafec48a..dde2f7878 100644
--- a/MediaBrowser.Controller/Localization/ILocalizationManager.cs
+++ b/MediaBrowser.Controller/Localization/ILocalizationManager.cs
@@ -1,6 +1,7 @@
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Globalization;
using System.Collections.Generic;
+using System.Threading.Tasks;
namespace MediaBrowser.Controller.Localization
{
diff --git a/MediaBrowser.Server.Implementations/Localization/LocalizationManager.cs b/MediaBrowser.Server.Implementations/Localization/LocalizationManager.cs
index 1965531af..89891a162 100644
--- a/MediaBrowser.Server.Implementations/Localization/LocalizationManager.cs
+++ b/MediaBrowser.Server.Implementations/Localization/LocalizationManager.cs
@@ -5,6 +5,7 @@ using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Globalization;
using MoreLinq;
using System;
+using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
@@ -28,6 +29,9 @@ namespace MediaBrowser.Server.Implementations.Localization
/// </summary>
private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
+ private readonly ConcurrentDictionary<string, Dictionary<string, ParentalRating>> _allParentalRatings =
+ new ConcurrentDictionary<string, Dictionary<string, ParentalRating>>(StringComparer.OrdinalIgnoreCase);
+
/// <summary>
/// Initializes a new instance of the <see cref="LocalizationManager"/> class.
/// </summary>
@@ -92,7 +96,65 @@ namespace MediaBrowser.Server.Implementations.Localization
/// <returns>IEnumerable{ParentalRating}.</returns>
public IEnumerable<ParentalRating> GetParentalRatings()
{
- var path = GetRatingsFile();
+ return GetParentalRatingsDictionary().Values.ToList();
+ }
+
+ /// <summary>
+ /// Gets the parental ratings dictionary.
+ /// </summary>
+ /// <returns>Dictionary{System.StringParentalRating}.</returns>
+ private Dictionary<string, ParentalRating> GetParentalRatingsDictionary()
+ {
+ var countryCode = _configurationManager.Configuration.MetadataCountryCode;
+
+ if (string.IsNullOrEmpty(countryCode))
+ {
+ countryCode = "us";
+ }
+
+ var ratings = GetRatings(countryCode);
+
+ if (ratings == null)
+ {
+ ratings = GetRatings("us");
+ }
+
+ return ratings;
+ }
+
+ /// <summary>
+ /// Gets the ratings.
+ /// </summary>
+ /// <param name="countryCode">The country code.</param>
+ private Dictionary<string, ParentalRating> GetRatings(string countryCode)
+ {
+ Dictionary<string, ParentalRating> value;
+
+ if (!_allParentalRatings.TryGetValue(countryCode, out value))
+ {
+ value = LoadRatings(countryCode);
+
+ if (value != null)
+ {
+ _allParentalRatings.TryAdd(countryCode, value);
+ }
+ }
+
+ return value;
+ }
+
+ /// <summary>
+ /// Loads the ratings.
+ /// </summary>
+ /// <param name="countryCode">The country code.</param>
+ private Dictionary<string, ParentalRating> LoadRatings(string countryCode)
+ {
+ var path = GetRatingsFilePath(countryCode);
+
+ if (string.IsNullOrEmpty(path))
+ {
+ return null;
+ }
return File.ReadAllLines(path).Select(i =>
{
@@ -115,31 +177,14 @@ namespace MediaBrowser.Server.Implementations.Localization
})
.Where(i => i != null)
- .OrderBy(p => p.Value);
- }
-
- /// <summary>
- /// Gets the ratings file.
- /// </summary>
- /// <returns>System.String.</returns>
- private string GetRatingsFile()
- {
- var countryCode = _configurationManager.Configuration.MetadataCountryCode;
-
- if (string.IsNullOrEmpty(countryCode))
- {
- countryCode = "us";
- }
-
- return GetRatingsFile(countryCode).Result ?? GetRatingsFile("us").Result;
+ .ToDictionary(i => i.Name);
}
/// <summary>
/// Gets the ratings file.
/// </summary>
/// <param name="countryCode">The country code.</param>
- /// <returns>Task{System.String}.</returns>
- private async Task<string> GetRatingsFile(string countryCode)
+ private string GetRatingsFilePath(string countryCode)
{
countryCode = countryCode.ToLower();
@@ -166,9 +211,9 @@ namespace MediaBrowser.Server.Implementations.Localization
Directory.CreateDirectory(parentPath);
}
- using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, true))
+ using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read))
{
- await stream.CopyToAsync(fs).ConfigureAwait(false);
+ stream.CopyTo(fs);
}
}
}
@@ -179,9 +224,6 @@ namespace MediaBrowser.Server.Implementations.Localization
/// <summary>
/// Gets the rating level.
/// </summary>
- /// <param name="rating">The rating.</param>
- /// <returns>System.Int32.</returns>
- /// <exception cref="System.ArgumentNullException">rating</exception>
public int? GetRatingLevel(string rating)
{
if (string.IsNullOrEmpty(rating))
@@ -189,17 +231,18 @@ namespace MediaBrowser.Server.Implementations.Localization
throw new ArgumentNullException("rating");
}
- var ratingsDictionary = GetParentalRatings().ToDictionary(i => i.Name);
+ var ratingsDictionary = GetParentalRatingsDictionary();
- if (ratingsDictionary.ContainsKey(rating))
- return ratingsDictionary[rating].Value;
+ ParentalRating value;
- var stripped = StripCountry(rating);
+ if (!ratingsDictionary.TryGetValue(rating, out value))
+ {
+ var stripped = StripCountry(rating);
- if (ratingsDictionary.ContainsKey(stripped))
- return ratingsDictionary[stripped].Value;
+ ratingsDictionary.TryGetValue(stripped, out value);
+ }
- return null;
+ return value == null ? (int?)null : value.Value;
}
/// <summary>