From 9880caaf315effe46f34c7d298a1554970fe6e3d Mon Sep 17 00:00:00 2001 From: ngfrolov Date: Fri, 10 Jun 2022 17:32:05 +0500 Subject: [PATCH] Add shorthand to get cache by using default values of obsolescence and tag --- .../EfCache/EfCacheDictionaryExtensions.cs | 81 +++++++++++++++++++ .../EfCache/EfCacheExtensions.cs | 32 ++++++++ 2 files changed, 113 insertions(+) diff --git a/AsbCloudInfrastructure/EfCache/EfCacheDictionaryExtensions.cs b/AsbCloudInfrastructure/EfCache/EfCacheDictionaryExtensions.cs index 21a672f7..b4f45b3e 100644 --- a/AsbCloudInfrastructure/EfCache/EfCacheDictionaryExtensions.cs +++ b/AsbCloudInfrastructure/EfCache/EfCacheDictionaryExtensions.cs @@ -19,6 +19,7 @@ namespace AsbCloudInfrastructure.EfCache private static readonly TimeSpan semaphoreTimeout = TimeSpan.FromSeconds(25); private static readonly SemaphoreSlim semaphore = new(1); private static readonly TimeSpan minCacheTime = TimeSpan.FromSeconds(2); + private static readonly TimeSpan defaultObsolescence = TimeSpan.FromMinutes(4); private class CacheItem { @@ -241,6 +242,38 @@ namespace AsbCloudInfrastructure.EfCache return cache; } + /// + /// Кешировать запрос в Dictionary<, >. С тегом typeof(TEntity).Name и ключом int Id + /// + /// + /// + /// + public static Dictionary FromCacheDictionary( + this IQueryable query) + where TEntity : class, AsbCloudDb.Model.IId + { + var tag = typeof(TEntity).Name; + return FromCacheDictionary(query, tag, defaultObsolescence, e => e.Id); + } + + /// + /// Кешировать запрос в Dictionary<, >. С тегом typeof(TEntity).Name + /// + /// + /// + /// + /// Делегат получения ключа из записи + /// + public static Dictionary FromCacheDictionary( + this IQueryable query, + Func keySelector) + where TEntity : class + where TKey : notnull + { + var tag = typeof(TEntity).Name; + return FromCacheDictionary(query, tag, defaultObsolescence, keySelector); + } + /// /// Кешировать запрос в Dictionary<, >. /// @@ -293,6 +326,43 @@ namespace AsbCloudInfrastructure.EfCache return cache.GetData(convert); } + /// + /// Асинхронно кешировать запрос в Dictionary<, >. С тегом typeof(TEntity).Name и ключом int Id + /// + /// + /// + /// + /// + public static Task> FromCacheDictionaryAsync( + this IQueryable query, + CancellationToken token = default) + where TEntity : class, AsbCloudDb.Model.IId + { + var tag = typeof(TEntity).Name; + return FromCacheDictionaryAsync(query, tag, defaultObsolescence, e => e.Id, token); + } + + /// + /// Асинхронно кешировать запрос в Dictionary<, >. С тегом typeof(TEntity).Name + /// + /// + /// + /// + /// Делегат получения ключа из записи + /// + /// + public static Task> FromCacheDictionaryAsync( + this IQueryable query, + Func keySelector, + CancellationToken token = default) + where TEntity : class + where TKey : notnull + { + var tag = typeof(TEntity).Name; + return FromCacheDictionaryAsync(query, tag, defaultObsolescence, keySelector, token); + } + + /// /// Асинхронно кешировать запрос в Dictionary<, >. /// @@ -343,6 +413,17 @@ namespace AsbCloudInfrastructure.EfCache return cache.GetData(convert); } + /// + /// drops cache with tag = typeof(T).Name + /// + /// + /// + public static void DropCacheDictionary(this IQueryable query) + { + var tag = typeof(T).Name; + DropCacheDictionary(query, tag); + } + /// /// Очистить кеш /// diff --git a/AsbCloudInfrastructure/EfCache/EfCacheExtensions.cs b/AsbCloudInfrastructure/EfCache/EfCacheExtensions.cs index b1a9e382..2c9c953f 100644 --- a/AsbCloudInfrastructure/EfCache/EfCacheExtensions.cs +++ b/AsbCloudInfrastructure/EfCache/EfCacheExtensions.cs @@ -19,6 +19,7 @@ namespace AsbCloudInfrastructure.EfCache private static readonly TimeSpan semaphoreTimeout = TimeSpan.FromSeconds(25); private static readonly SemaphoreSlim semaphore = new(1); private static readonly TimeSpan minCacheTime = TimeSpan.FromSeconds(2); + private static readonly TimeSpan defaultObsolescence = TimeSpan.FromMinutes(4); private class CacheItem { @@ -237,6 +238,19 @@ namespace AsbCloudInfrastructure.EfCache return cache; } + /// + /// Кешировать запрос в List<>. Кеш tag = typeof(TEntity).Name + /// + /// + /// + /// + public static IEnumerable FromCache(this IQueryable query) + where TEntity : class + { + var tag = typeof(TEntity).Name; + return FromCache(query, tag, defaultObsolescence); + } + /// /// Кешировать запрос в List<>. /// @@ -272,6 +286,13 @@ namespace AsbCloudInfrastructure.EfCache return cache.GetData(convert); } + public static Task> FromCacheAsync(this IQueryable query, CancellationToken token = default) + where TEntity : class + { + var tag = typeof(TEntity).Name; + return FromCacheAsync(query, tag, defaultObsolescence, token); + } + /// /// Асинхронно кешировать запрос в List<>.
///
@@ -311,6 +332,17 @@ namespace AsbCloudInfrastructure.EfCache return cache.GetData(convert); } + /// + /// drops cache with tag = typeof(T).Name + /// + /// + /// + public static void DropCache(this IQueryable query) + { + var tag = typeof(T).Name; + DropCache(query, tag); + } + /// /// Очистить кеш ///