You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.4 KiB
52 lines
1.4 KiB
5 years ago
|
using System;
|
||
|
using System.Text.RegularExpressions;
|
||
|
using NzbDrone.Common.Cache;
|
||
|
using NzbDrone.Core.Profiles.Releases.TermMatchers;
|
||
|
|
||
|
namespace NzbDrone.Core.Profiles.Releases
|
||
|
{
|
||
|
public interface ITermMatcherService
|
||
|
{
|
||
|
bool IsMatch(string term, string value);
|
||
|
string MatchingTerm(string term, string value);
|
||
|
}
|
||
|
|
||
|
public class TermMatcherService : ITermMatcherService
|
||
|
{
|
||
|
private ICached<ITermMatcher> _matcherCache;
|
||
|
|
||
|
public TermMatcherService(ICacheManager cacheManager)
|
||
|
{
|
||
|
_matcherCache = cacheManager.GetCache<ITermMatcher>(GetType());
|
||
|
}
|
||
|
|
||
|
public bool IsMatch(string term, string value)
|
||
|
{
|
||
|
return GetMatcher(term).IsMatch(value);
|
||
|
}
|
||
|
|
||
|
public string MatchingTerm(string term, string value)
|
||
|
{
|
||
|
return GetMatcher(term).MatchingTerm(value);
|
||
|
}
|
||
|
|
||
|
public ITermMatcher GetMatcher(string term)
|
||
|
{
|
||
|
return _matcherCache.Get(term, () => CreateMatcherInternal(term), TimeSpan.FromHours(24));
|
||
|
}
|
||
|
|
||
|
private ITermMatcher CreateMatcherInternal(string term)
|
||
|
{
|
||
|
Regex regex;
|
||
|
if (PerlRegexFactory.TryCreateRegex(term, out regex))
|
||
|
{
|
||
|
return new RegexTermMatcher(regex);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return new CaseInsensitiveTermMatcher(term);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|