public async Task <VBSession> GetAsync(string sessionHash, bool updateLastActivity = false) { if (!updateLastActivity && cache.TryGet(VBCacheKey.UserSession, sessionHash, out VBSession session)) { return(session); } int cookieTimeoutSpan = GetCookieTimeoutSpam(); session = await db.Sessions.Include(s => s.User) .ThenInclude(u => u.UserGroup) .Include(s => s.User) .ThenInclude(u => u.CustomAvatar) .SingleOrDefaultAsync(s => s.SessionHash == sessionHash && s.LastActivityRaw > cookieTimeoutSpan); if (session != null && updateLastActivity) { session.LastActivity = DateTime.UtcNow; session.Location = contextAccessor.HttpContext.Request.Path; await db.SaveChangesAsync(); } cache.Set(VBCacheKey.UserSession, sessionHash, session, settingsManager.GetCommonSettings().CookieTimeout); return(session); }
// Author is currently only present on read operations (ToDo) public async Task <VBThread> GetThreadAsync(int id, bool writeable = false) { // Don't want to use the cache for write operations since this may confuse EF's tracking system. So get a fresh copy for this cases if (!writeable && cache.TryGet(VBCacheKey.Thread, id.ToString(), out VBThread thread)) { return(thread); } thread = await GetSimpleThreadIncludeQuery(writeable : writeable).SingleOrDefaultAsync(t => t.Id == id); if (thread.FirstPost != null) { // To avoid problems by confusing EFs change detection, we only add the author on write operations if (!writeable) { thread.Author = thread.FirstPost.Author; } } cache.Set(VBCacheKey.Thread, id.ToString(), thread); return(thread); }
public T GetSettingsWithCache <T>() where T : new() { string cacheSubKey = typeof(T).FullName; if (cache.TryGet(VBCacheKey.Settings, cacheSubKey, out T settings)) { return(settings); } settings = GetSettings <T>(); cache.Set(VBCacheKey.Settings, cacheSubKey, settings, TimeSpan.FromDays(7)); return(settings); }
public async Task <List <VBForum> > GetForumsWhereUserCanAsync(VBUserGroup userGroup, VBForumFlags?flags = null, bool writeable = false) { // Better not caching permissions here. We ran into strange issues, altough we used the group id as sub key for caching if (writeable || !cache.TryGet(VBCacheKey.Forums, out List <VBForum> forums)) { forums = await GetForumsQuery().ToListAsync(); cache.Set(VBCacheKey.Forums, forums); } var forumsWithFlags = FetchForumsWithFlags(forums, userGroup); var permittedForums = forumsWithFlags.Where(kvp => flags == null || kvp.Value.HasFlag(flags.Value)) .Select(kvp => kvp.Key) .ToList(); return(permittedForums); }