private IDictionary <string, int> GetPageList() { lock (_lock) { if (!EZmsInMemoryCache.TryGetValue(CacheKey, out IDictionary <string, int> pages)) { var dataProvider = ServiceLocator.Current.GetInstance <ICachedRouteDataProvider>(); // Only allow one thread to poplate the data lock (_lock) { if (EZmsInMemoryCache.TryGetValue(CacheKey, out pages)) { return(pages); } pages = dataProvider.GetContentToIdMap(); EZmsInMemoryCache.Set(CacheKey, pages, new MemoryCacheEntryOptions() { Priority = CacheItemPriority.NeverRemove, AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(900) }); } } return(pages); } }
public void Update(IDictionary <string, int> pages) { EZmsInMemoryCache.Set(_cacheKey, pages, new MemoryCacheEntryOptions { Priority = CacheItemPriority.NeverRemove, AbsoluteExpirationRelativeToNow = _cacheTimeout }); }
public static IEnumerable <T> FilterForVisitor <T>(this IEnumerable <T> contents, ClaimsPrincipal principal = null, bool includeUnPublished = false) where T : IContent { if (contents == null) { return(Enumerable.Empty <T>()); } if (!includeUnPublished) { var publishedStateAccessor = ServiceLocator.Current.GetInstance <IPublishedStateAccessor>(); contents = contents.Where(publishedStateAccessor.IsPublished); } if (principal == null) { var httpContext = ServiceLocator.Current.GetInstance <IHttpContextAccessor>()?.HttpContext; if (httpContext != null) { principal = httpContext.User; } } var isAuthenticated = principal?.Identity?.IsAuthenticated ?? false; var userRoleIds = new List <string>(); if (isAuthenticated) { var userId = principal.FindFirstValue(ClaimTypes.NameIdentifier); var cacheKey = $"EZms:principal:userroles:{userId}"; if (!EZmsInMemoryCache.TryGetValue(cacheKey, out userRoleIds)) { var userManager = ServiceLocator.Current.GetInstance <UserManager <IdentityUser> >(); var user = userManager.FindByIdAsync(userId).GetAwaiter().GetResult(); if (user != null) { var userRoles = userManager.GetRolesAsync(user).GetAwaiter().GetResult().ToList(); if (!userRoles.IsNullOrEmpty()) { var roleManager = ServiceLocator.Current.GetInstance <RoleManager <IdentityRole> >(); userRoleIds = roleManager.Roles .Where(w => userRoles.Contains(w.Name, StringComparer.OrdinalIgnoreCase)) .Select(w => w.Id) .ToListAsync().GetAwaiter().GetResult(); } } EZmsInMemoryCache.Set( cacheKey, userRoleIds, new MemoryCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30) }); } } var filtered = contents.Where(w => w.AllowedGroups.IsNullOrEmpty() || (!w.AllowedGroups.IsNullOrEmpty() && w.AllowedGroups.Intersect(userRoleIds).Any())); return(filtered); }