You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
114 lines
3.7 KiB
114 lines
3.7 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.Caching.Distributed;
|
|
using Volo.Abp.Caching;
|
|
|
|
namespace Win_in.Sfs.Shared.Application;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public static class CachingExtensions
|
|
{
|
|
/// <summary>
|
|
/// 获取或添加缓存
|
|
/// </summary>
|
|
/// <typeparam name="TCacheItem"></typeparam>
|
|
/// <param name="cache"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="factory"></param>
|
|
/// <param name="minutes"></param>
|
|
/// <returns></returns>
|
|
public static async Task<TCacheItem> GetOrAddItemAsync<TCacheItem>(
|
|
this IDistributedCache<TCacheItem> cache, string key, Func<Task<TCacheItem>> factory, int minutes)
|
|
where TCacheItem : class
|
|
{
|
|
TCacheItem cacheItem;
|
|
|
|
var result = await cache.GetAsync(key).ConfigureAwait(false);
|
|
if (result == null)
|
|
{
|
|
cacheItem = await factory.Invoke().ConfigureAwait(false);
|
|
await cache.SetItemAsync(key, cacheItem, minutes).ConfigureAwait(false);
|
|
}
|
|
else
|
|
{
|
|
cacheItem = result;
|
|
}
|
|
|
|
return cacheItem;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="cache"></param>
|
|
/// <param name="keys"></param>
|
|
/// <typeparam name="TCacheItem"></typeparam>
|
|
/// <returns></returns>
|
|
public static async Task<IEnumerable<TCacheItem>> GetItemsAsync<TCacheItem>(
|
|
this IDistributedCache<TCacheItem> cache, IEnumerable<string> keys) where TCacheItem : class
|
|
{
|
|
var cacheItems = await cache.GetManyAsync(keys, null, true).ConfigureAwait(false);
|
|
return cacheItems.Select(p => p.Value);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="cache"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="cacheItem"></param>
|
|
/// <param name="minutes"></param>
|
|
/// <typeparam name="TCacheItem"></typeparam>
|
|
public static async Task SetItemAsync<TCacheItem>(
|
|
this IDistributedCache<TCacheItem> cache, string key, TCacheItem cacheItem, int minutes) where TCacheItem : class
|
|
{
|
|
var options = CreateDistributedCacheEntryOptions<TCacheItem>(minutes);
|
|
await cache.SetAsync(key, cacheItem, options, null, true).ConfigureAwait(false);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="cache"></param>
|
|
/// <param name="cacheItems"></param>
|
|
/// <param name="minutes"></param>
|
|
/// <typeparam name="TCacheItem"></typeparam>
|
|
public static async Task SetItemsAsync<TCacheItem>(
|
|
this IDistributedCache<TCacheItem> cache, IEnumerable<KeyValuePair<string, TCacheItem>> cacheItems, int minutes) where TCacheItem : class
|
|
{
|
|
var options = CreateDistributedCacheEntryOptions<TCacheItem>(minutes);
|
|
await cache.SetManyAsync(cacheItems, options).ConfigureAwait(false);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="cache"></param>
|
|
/// <param name="key"></param>
|
|
/// <typeparam name="TCacheItem"></typeparam>
|
|
public static async Task DeleteItemAsync<TCacheItem>(
|
|
this IDistributedCache<TCacheItem> cache, string key)
|
|
where TCacheItem : class
|
|
{
|
|
var result = await cache.GetAsync(key).ConfigureAwait(false);
|
|
if (result != null)
|
|
{
|
|
await cache.RemoveAsync(key).ConfigureAwait(false);
|
|
}
|
|
}
|
|
|
|
private static DistributedCacheEntryOptions CreateDistributedCacheEntryOptions<TCacheItem>(int minutes)
|
|
{
|
|
var options = new DistributedCacheEntryOptions();
|
|
if (minutes != SfsCacheConst.Never)
|
|
{
|
|
options.AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(minutes);
|
|
}
|
|
|
|
return options;
|
|
}
|
|
}
|
|
|