forked from ddrilling/AsbCloudServer
Add shorthand to get cache by using default values of obsolescence and tag
This commit is contained in:
parent
04b085b2ec
commit
9880caaf31
@ -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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Кешировать запрос в Dictionary<<typeparamref name="TKey"/>, <typeparamref name="TEntity"/>>. С тегом typeof(TEntity).Name и ключом int Id
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <param name="query"></param>
|
||||
/// <returns></returns>
|
||||
public static Dictionary<int, TEntity> FromCacheDictionary<TEntity>(
|
||||
this IQueryable<TEntity> query)
|
||||
where TEntity : class, AsbCloudDb.Model.IId
|
||||
{
|
||||
var tag = typeof(TEntity).Name;
|
||||
return FromCacheDictionary(query, tag, defaultObsolescence, e => e.Id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Кешировать запрос в Dictionary<<typeparamref name="TKey"/>, <typeparamref name="TEntity"/>>. С тегом typeof(TEntity).Name
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey"></typeparam>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <param name="query"></param>
|
||||
/// <param name="keySelector">Делегат получения ключа из записи</param>
|
||||
/// <returns></returns>
|
||||
public static Dictionary<TKey, TEntity> FromCacheDictionary<TKey, TEntity>(
|
||||
this IQueryable<TEntity> query,
|
||||
Func<TEntity, TKey> keySelector)
|
||||
where TEntity : class
|
||||
where TKey : notnull
|
||||
{
|
||||
var tag = typeof(TEntity).Name;
|
||||
return FromCacheDictionary(query, tag, defaultObsolescence, keySelector);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Кешировать запрос в Dictionary<<typeparamref name="TKey"/>, <typeparamref name="TEntity"/>>.
|
||||
/// </summary>
|
||||
@ -293,6 +326,43 @@ namespace AsbCloudInfrastructure.EfCache
|
||||
return cache.GetData<TKey, TEntity, TModel>(convert);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Асинхронно кешировать запрос в Dictionary<<typeparamref name="TKey"/>, <typeparamref name="TEntity"/>>. С тегом typeof(TEntity).Name и ключом int Id
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <param name="query"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
public static Task<Dictionary<int, TEntity>> FromCacheDictionaryAsync<TEntity>(
|
||||
this IQueryable<TEntity> query,
|
||||
CancellationToken token = default)
|
||||
where TEntity : class, AsbCloudDb.Model.IId
|
||||
{
|
||||
var tag = typeof(TEntity).Name;
|
||||
return FromCacheDictionaryAsync(query, tag, defaultObsolescence, e => e.Id, token);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Асинхронно кешировать запрос в Dictionary<<typeparamref name="TKey"/>, <typeparamref name="TEntity"/>>. С тегом typeof(TEntity).Name
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey"></typeparam>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <param name="query"></param>
|
||||
/// <param name="keySelector">Делегат получения ключа из записи</param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
public static Task<Dictionary<TKey, TEntity>> FromCacheDictionaryAsync<TKey, TEntity>(
|
||||
this IQueryable<TEntity> query,
|
||||
Func<TEntity, TKey> keySelector,
|
||||
CancellationToken token = default)
|
||||
where TEntity : class
|
||||
where TKey : notnull
|
||||
{
|
||||
var tag = typeof(TEntity).Name;
|
||||
return FromCacheDictionaryAsync(query, tag, defaultObsolescence, keySelector, token);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Асинхронно кешировать запрос в Dictionary<<typeparamref name="TKey"/>, <typeparamref name="TEntity"/>>.
|
||||
/// </summary>
|
||||
@ -343,6 +413,17 @@ namespace AsbCloudInfrastructure.EfCache
|
||||
return cache.GetData<TKey, TEntity, TModel>(convert);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// drops cache with tag = typeof(T).Name
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="query"></param>
|
||||
public static void DropCacheDictionary<T>(this IQueryable<T> query)
|
||||
{
|
||||
var tag = typeof(T).Name;
|
||||
DropCacheDictionary<T>(query, tag);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Очистить кеш
|
||||
/// </summary>
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Кешировать запрос в List<<typeparamref name="TEntity"/>>. Кеш tag = typeof(TEntity).Name
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <param name="query"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<TEntity> FromCache<TEntity>(this IQueryable<TEntity> query)
|
||||
where TEntity : class
|
||||
{
|
||||
var tag = typeof(TEntity).Name;
|
||||
return FromCache(query, tag, defaultObsolescence);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Кешировать запрос в List<<typeparamref name="TEntity"/>>.
|
||||
/// </summary>
|
||||
@ -272,6 +286,13 @@ namespace AsbCloudInfrastructure.EfCache
|
||||
return cache.GetData(convert);
|
||||
}
|
||||
|
||||
public static Task<IEnumerable<TEntity>> FromCacheAsync<TEntity>(this IQueryable<TEntity> query, CancellationToken token = default)
|
||||
where TEntity : class
|
||||
{
|
||||
var tag = typeof(TEntity).Name;
|
||||
return FromCacheAsync(query, tag, defaultObsolescence, token);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Асинхронно кешировать запрос в List<<typeparamref name="TEntity"/>>.<br/>
|
||||
/// </summary>
|
||||
@ -311,6 +332,17 @@ namespace AsbCloudInfrastructure.EfCache
|
||||
return cache.GetData(convert);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// drops cache with tag = typeof(T).Name
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="query"></param>
|
||||
public static void DropCache<T>(this IQueryable<T> query)
|
||||
{
|
||||
var tag = typeof(T).Name;
|
||||
DropCache(query, tag);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Очистить кеш
|
||||
/// </summary>
|
||||
|
Loading…
Reference in New Issue
Block a user