public static void UpdateCacheLine(string id, DateTime validUntil, string data) { CacheLine line = null; using (CacheDbContext ctx = new CacheDbContext()) { line = ctx.CacheLines.Where(e => string.Equals(e.Id, id)).FirstOrDefault(); } if (line is null) { line = new CacheLine { Id = id, ValidUntil = validUntil, Data = data }; line.Add(); } else { line.ValidUntil = validUntil; line.Data = data; line.Update(); } }
//--------------------------------------------------------Set-, Get- Methods:---------------------------------------------------------\\ #region --Set-, Get- Methods-- #endregion //--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\ #region --Misc Methods (Public)-- /// <summary> /// Removes all cache lines and cache entries from the DB. /// </summary> public static void ClearCache() { using (CacheDbContext ctx = new CacheDbContext()) { ctx.CacheLines.RemoveRange(ctx.CacheLines); ctx.CacheEntries.RemoveRange(ctx.CacheEntries); } }
/// <summary> /// Checks if there exists a <see cref="CacheLine"/> for the given <paramref name="id"/> which is still valid. /// </summary> /// <param name="id">The ID of the <see cref="CacheLine"/>.</param> /// <returns>The cached data in case there exists a valid <see cref="CacheLine"/>. Else, null.</returns> public static string GetCacheLine(string id) { CacheLine line = null; using (CacheDbContext ctx = new CacheDbContext()) { line = ctx.CacheLines.Where(e => string.Equals(e.Id, id)).FirstOrDefault(); } return(!(line is null) && (line.ValidUntil > DateTime.Now) ? line.Data : null); }
/// <summary> /// Checks if there exists a valid <see cref="CacheEntry"/> for the given <paramref name="id"/>. /// </summary> /// <param name="id">The ID of the <see cref="CacheEntry"/>.</param> /// <returns>True in case there exists a valid <see cref="CacheEntry"/>.</returns> public static bool IsCacheEntryValid(string id) { CacheEntry entry = null; using (CacheDbContext ctx = new CacheDbContext()) { entry = ctx.CacheEntries.Where(e => string.Equals(e.Id, id)).FirstOrDefault(); } return(!(entry is null) && (entry.ValidUntil > DateTime.Now)); }
public static void UpdateCacheEntry(string id, DateTime validUntil) { CacheEntry entry = null; using (CacheDbContext ctx = new CacheDbContext()) { entry = ctx.CacheEntries.Where(e => string.Equals(e.Id, id)).FirstOrDefault(); } if (entry is null) { entry = new CacheEntry { Id = id, ValidUntil = validUntil }; entry.Add(); } else { entry.ValidUntil = validUntil; entry.Update(); } }