public void UpdateCache <TDbEntity>(List <TDbEntity> entities)
 {
     try
     {
         CacheEntity type        = CacheEntityMap.GetEntityTypeForDBEntity <TDbEntity>();
         CacheStatus cacheStatus = Uow.CacheStatusesRepository.GetCacheStatus(type);
         cacheStatus.Status      = CacheEntryStatus.UpdateInProgress;
         cacheStatus.LastUpdated = DateTime.UtcNow;
         // try to get DB lock;
         if (Uow.CacheStatusesRepository.TryUpdate(cacheStatus))
         {
             ICachedEntitiesRepository <TDbEntity> entityRepository = Uow.GetRepository <ICachedEntitiesRepository <TDbEntity> >();
             Task task = entityRepository.UpdateCacheAsync(entities);
             task.ContinueWith(task1 =>
             {
                 using (IOptionsPlayUow uow = ObjectFactory.GetInstance <IOptionsPlayUow>())
                 {
                     cacheStatus             = uow.CacheStatusesRepository.GetCacheStatus(type);
                     cacheStatus.Status      = CacheEntryStatus.Active;
                     cacheStatus.LastUpdated = DateTime.UtcNow;
                     uow.CacheStatusesRepository.Update(cacheStatus);
                     uow.Commit();
                 }
             });
         }
     }
     catch (Exception ex)
     {
         Logging.Logger.Debug("thread ID:" + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString() + ", UpdateCache Exception is:" + ex.StackTrace.ToString() + ", class is DatabaseCacheService");
     }
 }
Ejemplo n.º 2
0
        //public static IQueryable<OptionBasicInformation> Get(out DBCacheStatus status)
        //{
        //    if (LastUpdated.Value.Add(TimeSpan.FromMinutes(60)) <= DateTime.UtcNow)
        //    {
        //        status = DBCacheStatus.Ok;
        //    }
        //    else
        //    {
        //        status = DBCacheStatus.Expired;
        //    }
        //    return OptionBasicInformationCache;
        //}

        public IQueryable <TEntity> Get <TEntity>(out DBCacheStatus status)
        {
            CacheEntity type        = CacheEntityMap.GetEntityTypeForDBEntity <TEntity>();
            CacheStatus cacheStatus = Uow.CacheStatusesRepository.GetCacheStatus(type);

            if (cacheStatus.Status == CacheEntryStatus.Empty || cacheStatus.Status == CacheEntryStatus.UpdateInProgress)
            {
                status = DBCacheStatus.Empty;
                return(null);
            }

            TimeSpan expiration = GetExpirationForEntity(type);
            ICachedEntitiesRepository <TEntity> entityRepository = Uow.GetRepository <ICachedEntitiesRepository <TEntity> >();

            if (!cacheStatus.LastUpdated.HasValue || cacheStatus.LastUpdated.Value.Add(expiration) <= DateTime.UtcNow)
            {
                status = DBCacheStatus.Expired;
                return(entityRepository.GetAll());
            }
            if (type == CacheEntity.OptionBasicInformation)
            {
                if (OptionBasicInformationCache == null)
                {
                    var repository = (IList <OptionBasicInformationCache>)entityRepository.GetAll().ToList();
                    OptionBasicInformationCache = (List <OptionBasicInformationCache>)repository;
                }
            }
            else
            {
                if (SecurityInformationCache == null)
                {
                    var repository = (IList <SecurityInformationCache>)entityRepository.GetAll().ToList();
                    SecurityInformationCache = (List <SecurityInformationCache>)entityRepository.GetAll();
                }
            }
            status = DBCacheStatus.Ok;
            return(entityRepository.GetAll());
        }
        public IQueryable <TDbEntity> Get <TDbEntity>(out DBCacheStatus status)
        {
            CacheEntity type        = CacheEntityMap.GetEntityTypeForDBEntity <TDbEntity>();
            CacheStatus cacheStatus = Uow.CacheStatusesRepository.GetCacheStatus(type);

            ICachedEntitiesRepository <TDbEntity> entityRepository = Uow.GetRepository <ICachedEntitiesRepository <TDbEntity> >();

            if (cacheStatus.Status == CacheEntryStatus.Empty || cacheStatus.Status == CacheEntryStatus.UpdateInProgress)
            {
                status = DBCacheStatus.Empty;
                return(null);
            }

            TimeSpan expiration = GetExpirationForEntity(type);

            if (!cacheStatus.LastUpdated.HasValue || cacheStatus.LastUpdated.Value.Add(expiration) <= DateTime.UtcNow)
            {
                status = DBCacheStatus.Expired;
                return(entityRepository.GetAll());
            }

            status = DBCacheStatus.Ok;
            return(entityRepository.GetAll());
        }