/// <inheritdoc cref="IViewManager.GetPageAsync(int, bool)" /> public async Task <PageInfo> GetPageAsync(int pageID, bool getAll = false) { Page page = null; string pageCacheKey = string.Format(PageConfiguration.PageCacheKey, pageID); if (_cacheEngine.GetCacheObject(pageCacheKey, out PageInfo pageInfo)) { return(pageInfo); } if (getAll) { page = await _dbContext.KastraPages.Include(p => p.PageTemplate.KastraPlaces) .SingleOrDefaultAsync(p => p.PageId == pageID); if (page is null) { return(null); } pageInfo = page.ToPageInfo(true, true); foreach (var place in pageInfo.PageTemplate.Places) { if (place.ModuleId.HasValue) { place.StaticModule = await GetModuleAsync(place.ModuleId.Value, true, false); } place.Modules = await GetModulesListByPlaceIdAsync(place.PlaceId, true); } } else { page = await _dbContext.KastraPages.SingleOrDefaultAsync(p => p.PageId == pageID); if (page is null) { return(null); } pageInfo = page.ToPageInfo(); } _cacheEngine.SetCacheObject(pageCacheKey, pageInfo); return(pageInfo); }
/// <inheritdoc cref="IParameterManager.GetSiteConfigurationAsync"/> public async Task <SiteConfigurationInfo> GetSiteConfigurationAsync() { if (!_cacheEngine.GetCacheObject(SiteConfiguration.SiteConfigCacheKey, out SiteConfigurationInfo siteConfig)) { siteConfig = _cacheEngine.SetCacheObject(SiteConfiguration.SiteConfigCacheKey, await LoadSiteConfigurationAsync()); if (siteConfig is not null && siteConfig.CacheActivated) { _cacheEngine.EnableCache(); } else { _cacheEngine.DisableCache(); } }
/// <summary> /// Loads the query string data in model objects. /// </summary> private void LoadQueryString() { string parameterValue; object moduleComponent = (object)this; ParameterAttribute parameterAttribute; PropertyInfo[] properties; string moduleParametersKey = string.Format(ModuleConfiguration.ModuleViewComponentParameters, moduleComponent.GetType().FullName); if (!_cacheEngine.GetCacheObject(moduleParametersKey, out properties)) { properties = _cacheEngine.SetCacheObject(moduleParametersKey, moduleComponent.GetType().GetProperties()); } if (properties == null) { return; } // Select all properties which have parameterattributes foreach (PropertyInfo property in properties) { if (!property.CanRead) { continue; } parameterAttribute = property.GetCustomAttribute(typeof(ParameterAttribute), false) as ParameterAttribute; if (parameterAttribute == null) { continue; } parameterValue = HttpContext.Request.Query[parameterAttribute.Name]; if (string.IsNullOrEmpty(parameterValue)) { continue; } property.SetValue((object)this, Convert.ChangeType(parameterValue, property.PropertyType), (object[])null); } }
/// <inheritdoc cref="IStatisticsManager.SaveVisitorAsync(VisitorInfo)"/> public async Task <bool> SaveVisitorAsync(VisitorInfo visitorInfo) { if (visitorInfo is null) { throw new ArgumentNullException(nameof(visitorInfo)); } bool isLoggedIn = visitorInfo.UserId.HasValue; string ipAddress = visitorInfo.IpAddress; if (_cacheEngine.GetCacheObject(VISITS_KEY, out Dictionary <string, bool> recentVisitors)) { // If visitor exists in cache if (recentVisitors.ContainsKey(ipAddress)) { if (isLoggedIn && !recentVisitors[ipAddress]) { // Update in cache recentVisitors[ipAddress] = isLoggedIn; // Update existing visit DateTime startDate = DateTime.UtcNow.Subtract(_cacheEngine.CacheOptions?.SlidingExpiration ?? new TimeSpan()); Visitor visit = _dbContext.KastraVisitors .Where(v => v.LastVisitAt >= startDate && v.LastVisitAt <= DateTime.UtcNow) .SingleOrDefault(v => v.IpAddress == ipAddress); if (visit is null) { return(false); } visit.UserId = visit.UserId; _dbContext.KastraVisitors.Update(visit); await _dbContext.SaveChangesAsync(); return(true); } return(false); } else { recentVisitors.Add(visitorInfo.IpAddress, isLoggedIn); } } else { recentVisitors = new Dictionary <string, bool>(); recentVisitors.Add(visitorInfo.IpAddress, isLoggedIn); _cacheEngine.SetCacheObject(VISITS_KEY, recentVisitors); } // Save visit in database Visitor visitor = visitorInfo.ToVisitor(); _dbContext.KastraVisitors.Add(visitor); await _dbContext.SaveChangesAsync(); return(true); }