/// <summary>
/// Gets the match from the cache for the useragent or if not
/// present in the cache from the detection provider.
/// </summary>
/// <param name="userAgent">The user agent whose properties are needed</param>
/// <returns>A results object for the user agent</returns>
private static InternalResult GetMatchByUserAgent(string userAgent)
{
InternalResult result;
if (_cacheUserAgent._itemsActive.TryGetValue(userAgent, out result) == false)
{
// The result wasn't in the cache so find it from the 51Degrees provider.
var match = _provider.Match(userAgent);
// Construct the results for the values and the profile Ids.
result = new InternalResult(
new SortedList <string, string>(_numberOfProperties),
match.ProfileIds.OrderBy(i =>
i.Key).SelectMany(i =>
BitConverter.GetBytes(i.Value)).ToArray());
// Load the values into the results.
foreach (var property in _requiredProperties.SelectMany(i =>
i.Value))
{
var values = match[property];
if (values != null)
{
result.Values.Add(property.Name, values.ToString());
}
}
// Load the dynamic values into the results.
foreach (var dynamicProperty in _dynamicProperties)
{
// Add special properties for the detection.
switch (dynamicProperty)
{
case DynamicProperties.Id:
result.Values.Add("Id", String.Join(
Constants.ProfileSeperator,
match.ProfileIds.OrderBy(i =>
i.Key).Select(i => i.Value.ToString())));
break;
case DynamicProperties.Difference:
result.Values.Add("Difference", match.Difference.ToString());
break;
case DynamicProperties.Method:
result.Values.Add("Method", match.Method.ToString());
break;
}
}
// Add the results to the cache.
_cacheUserAgent._itemsActive[userAgent] = result;
}
// Ensure the result is added to the background cache.
//_cacheUserAgent.SetBackground(userAgent, result);
_cacheUserAgent.AddRecent(userAgent, result);
return(result);
}