1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-04 10:02:40 +01:00

Added caching to seasonEpisodePattern matching

This commit is contained in:
Mark McDowall 2013-11-20 22:10:00 -08:00
parent 3db97e9d11
commit 2b682a4936

View File

@ -4,6 +4,7 @@
using System.Linq; using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using NLog; using NLog;
using NzbDrone.Common.Cache;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
using NzbDrone.Core.MediaFiles; using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.Tv; using NzbDrone.Core.Tv;
@ -21,6 +22,7 @@ public class FileNameBuilder : IBuildFileNames
{ {
private readonly IConfigService _configService; private readonly IConfigService _configService;
private readonly INamingConfigService _namingConfigService; private readonly INamingConfigService _namingConfigService;
private readonly ICached<EpisodeFormat> _patternCache;
private readonly Logger _logger; private readonly Logger _logger;
private static readonly Regex TitleRegex = new Regex(@"(?<token>\{(?:\w+)(?<separator>\s|\W|_)\w+\})", private static readonly Regex TitleRegex = new Regex(@"(?<token>\{(?:\w+)(?<separator>\s|\W|_)\w+\})",
@ -37,10 +39,14 @@ public class FileNameBuilder : IBuildFileNames
public static readonly Regex AirDateRegex = new Regex(@"\{Air(\s|\W|_)Date\}", RegexOptions.Compiled | RegexOptions.IgnoreCase); public static readonly Regex AirDateRegex = new Regex(@"\{Air(\s|\W|_)Date\}", RegexOptions.Compiled | RegexOptions.IgnoreCase);
public FileNameBuilder(INamingConfigService namingConfigService, IConfigService configService, Logger logger) public FileNameBuilder(INamingConfigService namingConfigService,
IConfigService configService,
ICacheManger cacheManger,
Logger logger)
{ {
_namingConfigService = namingConfigService; _namingConfigService = namingConfigService;
_configService = configService; _configService = configService;
_patternCache = cacheManger.GetCache<EpisodeFormat>(GetType());
_logger = logger; _logger = logger;
} }
@ -99,17 +105,10 @@ public string BuildFilename(IList<Episode> episodes, Series series, EpisodeFile
} }
} }
var seasonEpisode = SeasonEpisodePatternRegex.Match(pattern); var episodeFormat = GetSeasonEpisodePattern(pattern);
if (seasonEpisode.Success)
{
var episodeFormat = new EpisodeFormat
{
EpisodeSeparator = seasonEpisode.Groups["episodeSeparator"].Value,
Separator = seasonEpisode.Groups["separator"].Value,
EpisodePattern = seasonEpisode.Groups["episode"].Value,
SeasonEpisodePattern = seasonEpisode.Groups["seasonEpisode"].Value,
};
if (episodeFormat != null)
{
pattern = pattern.Replace(episodeFormat.SeasonEpisodePattern, "{Season Episode}"); pattern = pattern.Replace(episodeFormat.SeasonEpisodePattern, "{Season Episode}");
var seasonEpisodePattern = episodeFormat.SeasonEpisodePattern; var seasonEpisodePattern = episodeFormat.SeasonEpisodePattern;
@ -246,6 +245,28 @@ private string ReplaceNumberToken(string token, int value)
return value.ToString(split[1]); return value.ToString(split[1]);
} }
private EpisodeFormat GetSeasonEpisodePattern(string pattern)
{
return _patternCache.Get(pattern, () =>
{
var match = SeasonEpisodePatternRegex.Match(pattern);
if (match.Success)
{
return new EpisodeFormat
{
EpisodeSeparator = match.Groups["episodeSeparator"].Value,
Separator = match.Groups["separator"].Value,
EpisodePattern = match.Groups["episode"].Value,
SeasonEpisodePattern = match.Groups["seasonEpisode"].Value,
};
}
return null;
});
}
} }
public enum MultiEpisodeStyle public enum MultiEpisodeStyle