Esempio n. 1
0
 private static bool IsExpired(CachedDataHolder ent)
 {
     if (ent == null)
     {
         return(false);
     }
     return(DateTime.UtcNow > ent.ExpirationDate.ToUniversalTime());
 }
Esempio n. 2
0
        /// <summary>
        /// Triggers the parsing of the current user agent
        /// </summary>
        public void Parse()
        {
            if (IsParsed())
            {
                return;
            }

            parsed = true;

            // skip parsing for empty useragents or those not containing any letter
            if (string.IsNullOrEmpty(userAgent) || !GetRegexEngine().Match(userAgent, "([a-z])"))
            {
                return;
            }

            var useCache = DeviceDetectorSettings.ParseCacheDBExpiration != TimeSpan.Zero;

            if (useCache)
            {
                var key = $"{userAgent}_{skipBotDetection}_{discardBotInformation}_{_versionTruncation}";

                var cachedData = col.FindById(key);
                if (IsExpired(cachedData))
                {
                    col.Delete(key);
                    cachedData = null;
                }

                if (!string.IsNullOrEmpty(cachedData?.Json))
                {
                    var data = JsonConvert.DeserializeObject <DeviceDetectorCachedData>(cachedData.Json, jsonSettings);
                    device = data.Device;
                    brand  = data.Brand;
                    parsed = data.Parsed;
                    model  = data.Model;
                    bot    = data.Bot;
                    client = data.Client;
                    os     = data.Os;
                    return;
                }

                ParseBase();

                var ent = new DeviceDetectorCachedData()
                {
                    Device = device,
                    Brand  = brand,
                    Parsed = parsed,
                    Model  = model,
                    Bot    = bot,
                    Client = client,
                    Os     = os
                };

                cachedData = new CachedDataHolder()
                {
                    Id             = key,
                    Json           = JsonConvert.SerializeObject(ent, jsonSettings),
                    ExpirationDate = useCache ? DateTime.UtcNow.Add(DeviceDetectorSettings.ParseCacheDBExpiration) : DateTime.MaxValue
                };

                col.Upsert(cachedData);
            }
            else
            {
                ParseBase();
            }
        }