Example #1
0
        public async Task DeleteAsync(string siteId)
        {
            var builder = Builders <CacheItem> .Filter;
            var filter  = builder.Eq(p => p.SiteId, siteId);
            await _collection.DeleteManyAsync(filter);

            var itemsToRemove = Items.Where(s => s.Value.SiteId == siteId).ToList();

            foreach (var cacheItem in itemsToRemove)
            {
                // TODO
                CacheItem ci;
                Items.TryRemove(CacheItem.GetKey(cacheItem.Value), out ci);
            }
        }
Example #2
0
        public async Task <IActionResult> StarResult(StarSearch search)
        {
            int threads = 4;

            if (ModelState.IsValid)
            {
                search.SearchDays  = Math.Min(search.SearchDays, 14);
                search.Destination = search.Destination.MyTrim();
                search.Origin      = search.Origin.MyTrim();
                if (search.Origin.MyLength() == 3 && search.Destination.MyLength() == 3)
                {
                    var dates = System.Linq.Enumerable.Range(0, search.SearchDays).Select(i => search.OutDate.AddDays(i)).ToList();
                    dates.Shuffle();
                    var res = new System.Collections.Concurrent.ConcurrentDictionary <DateTime, FlysasLib.SearchResult>();
                    await Dasync.Collections.ParallelForEachExtensions.ParallelForEachAsync <DateTime>(dates,
                                                                                                       async date =>
                    {
                        if (!res.ContainsKey(date))    //this looks smart, but doesn't realy save a lot of calls...
                        {
                            var q = new SASQuery
                            {
                                OutDate = date,
                                From    = search.Origin,
                                To      = search.Destination,
                                Adults  = search.Pax,
                                Mode    = SASQuery.SearhMode.STAR
                            };
                            var c = new FlysasLib.SASRestClient();
                            FlysasLib.SearchResult searchResult = await c.SearchAsync(q);

                            if (searchResult.tabsInfo != null && searchResult.tabsInfo.outboundInfo != null)
                            {
                                foreach (var dayWithNoSeats in searchResult.tabsInfo.outboundInfo.Where(tab => tab.points == 0))
                                {
                                    res.TryAdd(dayWithNoSeats.date, null);
                                }
                            }

                            res.TryAdd(date, searchResult);
                        }
                    },
                                                                                                       threads,
                                                                                                       false
                                                                                                       );

                    search.Results = res.Where(r => r.Value?.outboundFlights != null).SelectMany(r => r.Value.outboundFlights).ToList();
                    if (search.MaxLegs > 0)
                    {
                        search.Results = search.Results.Where(r => r.segments.Count() <= search.MaxLegs).ToList();
                    }
                }
                else
                {
                    search.Results = new List <FlysasLib.FlightBaseClass>();
                }
            }
            return(View(nameof(Star), search));
        }
Example #3
0
 private static void ExecuteMaintenance(object state)
 {
     // check if a step is already executing
     if (System.Threading.Interlocked.CompareExchange(ref m_executing, 1, 0) != 0)
     {
         return;
     }
     // try to fire OnExpiration event
     try
     {
         // stop timed task if queue is empty
         if (m_cacheMap.Count == 0)
         {
             StopMaintenance();
             // check again if the queue is empty
             if (m_cacheMap.Count != 0)
             {
                 StartMaintenance();
             }
         }
         else
         {
             CachedItem item;
             DateTime   oldThreshold = DateTime.UtcNow - m_timeout;
             // make a local copy of our event
             var localOnExpiration = OnExpiration;
             // select elegible records
             var expiredItems = m_cacheMap.Where(i => i.Value.Updated < oldThreshold).Select(i => i.Key);
             // remove from cache and fire OnExpiration event
             foreach (var key in expiredItems)
             {
                 m_cacheMap.TryRemove(key, out item);
                 if (localOnExpiration != null)
                 {
                     localOnExpiration(key, item.Data);
                 }
             }
         }
     }
     finally
     {
         // release lock
         System.Threading.Interlocked.Exchange(ref m_executing, 0);
     }
 }