示例#1
0
        public override async Task <Resource> Load(int id)
        {
            if (ScopedCache.ContainsKey(id))
            {
                return(ScopedCache[id]);
            }

            var resource = await FromCache(id);

            if (resource == null)
            {
                resource = await DbContext.Resources
                           .Include(c => c.Secrets)
                           .Include(c => c.Managers)
                           .Where(c => c.Id == id)
                           .SingleOrDefaultAsync();

                await ToCache(resource);
            }
            else
            {
                DbContext.Resources.Attach(resource);
            }

            ScopedCache.Add(id, resource);

            return(resource);
        }
示例#2
0
        public override async Task <Client> Load(int id)
        {
            if (ScopedCache.ContainsKey(id))
            {
                return(ScopedCache[id]);
            }

            var client = await FromCache(id);

            if (client == null)
            {
                client = await DbContext.Clients
                         .Include(c => c.Claims)
                         .Include(c => c.Secrets)
                         .Include(c => c.Events)
                         .Include(c => c.EventHandlers).ThenInclude(e => e.ClientEvent).ThenInclude(e => e.Client)
                         .Include(c => c.Managers)
                         .Include(c => c.Urls)
                         .Where(c => c.Id == id)
                         .SingleOrDefaultAsync();

                await ToCache(client);
            }
            else
            {
                DbContext.Clients.Attach(client);
            }

            ScopedCache.Add(id, client);

            return(client);
        }
 public static IServiceRepository ResetScope(this IServiceRepository repository)
 {
     if (ScopedCache.TryGetValue(repository, out var serviceMap))
     {
         serviceMap.Clear();
         ScopedCache.Remove(repository);
     }
     return(repository);
 }
示例#4
0
 public IEnumerable<SearchItem> Search(string query)
 {
     var getter = new Func<List<SearchItem>>(() => {
         var googleSearch = new CustomSearch(Config.GoogleAPIKey, Config.GoogleCX);
         return googleSearch.Query(query).Items;
     });
     using (var googleCache = new ScopedCache(CacheType.Google)) {
         return googleCache.Get(query, MemCache.MidnightExpiration, getter);
     }
 }
示例#5
0
 public List<TopProject> GetTopProjects(string query, DateTime date)
 {
     var getter = (Func<List<TopProject>>)(() => {
         using (var warehouse = new DataWarehouseEntities()) {
             return warehouse.GetTopProjects(query, date).ToList();
         }
     });
     using (var dataWarehouse = new ScopedCache(CacheType.DataWarehouse)) {
         return dataWarehouse.Get(query + date.ToShortDateString(), TimeSpan.FromMinutes(60), getter);
     }
 }
示例#6
0
 public CountyExtent GetCountyExtent(string query)
 {
     var getter = (Func<CountyExtent>)(() => {
         using (var warehouse = new DataWarehouseEntities()) {
             return warehouse.GetCountyExtents(query).FirstOrDefault();
         }
     });
     using (var dataWarehouse = new ScopedCache(CacheType.DataWarehouse)) {
         return dataWarehouse.Get(query, TimeSpan.FromMinutes(60), getter);
     }
 }
 /// <summary>
 /// Handles the disposal of resources. Derived from abstract class <see cref="DisposableObject"/> which handles common required locking logic.
 /// </summary>
 protected override void DisposeResources()
 {
     ScopedFinalizer.IfNotNull(x =>
     {
         x.FinalizeScope();
         x.Dispose();
     });
     ScopedCache.IfNotNull(x =>
     {
         x.ScopeComplete();
         x.Dispose();
     });
 }
示例#8
0
        public virtual async Task <TEntity> Load(int id)
        {
            if (ScopedCache.ContainsKey(id))
            {
                return(ScopedCache[id]);
            }

            var entity = await FromCache(id);

            if (entity == null)
            {
                entity = await DbContext.Set <TEntity>().FindAsync(id);
                await ToCache(entity);
            }
            else
            {
                DbContext.Set <TEntity>().Attach(entity);
            }

            ScopedCache.Add(id, entity);
            return(entity);
        }
示例#9
0
        public virtual async Task <TEntity> Load(int id, Func <IQueryable <TEntity>, IQueryable <TEntity> > includes = null)
        {
            if (id == 0)
            {
                return(null);
            }

            if (ScopedCache.ContainsKey(id))
            {
                return(ScopedCache[id]);
            }

            var entity = await FromCache(id);

            if (entity == null)
            {
                IQueryable <TEntity> query = DbContext.Set <TEntity>();

                if (includes != null)
                {
                    query = includes(query);
                }

                entity = await query.Where(e => e.Id == id).SingleOrDefaultAsync();

                await ToCache(entity);
            }
            else
            {
                DbContext.Set <TEntity>().Attach(entity);
            }

            ScopedCache.Add(id, entity);

            return(entity);
        }