private const int UserAgentsCacheTimeout = 60; //user agents cache expire time.(minutes) /// <summary> /// Returns ClientCapability based on HttpRequest /// </summary> public override IClientCapability GetClientCapability(string userAgent) { DeviceInfoClientCapability deviceInfoClientCapability = null; if (!string.IsNullOrEmpty(userAgent)) { bool found = false; //try to get content from cache var cachedUserAgents = DataCache.GetCache <SharedDictionary <string, DeviceInfoClientCapability> >(UserAgentsCacheKey); if (cachedUserAgents != null) { using (cachedUserAgents.GetReadLock()) { if (cachedUserAgents.ContainsKey(userAgent)) { deviceInfoClientCapability = cachedUserAgents[userAgent]; found = true; } } } if (!found) { var deviceInfo = WurflProvider.GetDeviceInfo(userAgent); if (deviceInfo != null) { deviceInfoClientCapability = new DeviceInfoClientCapability(deviceInfo); //update cache content if (cachedUserAgents == null) { cachedUserAgents = new SharedDictionary <string, DeviceInfoClientCapability>(); } using (cachedUserAgents.GetWriteLock()) { cachedUserAgents[userAgent] = deviceInfoClientCapability; } DataCache.SetCache(UserAgentsCacheKey, cachedUserAgents, TimeSpan.FromMinutes(UserAgentsCacheTimeout)); } } } var wurflClientCapability = new WURFLClientCapability(deviceInfoClientCapability); wurflClientCapability.UserAgent = userAgent; return(wurflClientCapability); }
private static IQueryable <DeviceInfoClientCapability> GetAllDevicesCallBack(CacheItemArgs cacheItem) { var devices = new List <DeviceInfoClientCapability>(); var allDevicesField = WurflProvider.GetType().GetField("AllDevices", BindingFlags.Instance | BindingFlags.NonPublic); var devicesInProvider = allDevicesField.GetValue(WurflProvider) as IDictionary <int, BaseDeviceInfo>; foreach (var device in devicesInProvider.Values) { if (!string.IsNullOrEmpty(device.UserAgent)) { devices.Add(new DeviceInfoClientCapability(device as DeviceInfo)); } } _allDevices = devices.AsQueryable(); return(_allDevices); }