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); }
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); }
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); } }
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); } }
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(); }); }
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); }
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); }