SystemEditStampCache.cs
1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using Microsoft.Extensions.Caching.Distributed;
using Volo.Abp.Caching;
namespace FoodLabeling.Application.Helpers;
/// <summary>
/// 平台「系统编辑」时间戳缓存项(任意业务写接口成功后更新,供 <c>my-menus</c> 的 <c>lastUpdated</c> 使用)。
/// </summary>
public class SystemEditStampCacheItem
{
public DateTime At { get; set; }
}
/// <summary>
/// 全局系统编辑时间戳缓存键(单实例,不按用户区分)。
/// </summary>
public class SystemEditStampCacheKey
{
public override string ToString() => "FoodLabeling:SystemEditStamp";
}
/// <summary>
/// 读写平台系统编辑时间戳。
/// </summary>
public static class SystemEditStampCacheHelper
{
private static readonly DistributedCacheEntryOptions DefaultOptions = new()
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromDays(3650)
};
private static readonly SystemEditStampCacheKey GlobalKey = new();
public static async Task<DateTime?> GetAsync(
IDistributedCache<SystemEditStampCacheItem, SystemEditStampCacheKey> cache)
{
var item = await cache.GetAsync(GlobalKey);
return item?.At;
}
public static async Task TouchAsync(
IDistributedCache<SystemEditStampCacheItem, SystemEditStampCacheKey> cache)
{
await cache.SetAsync(
GlobalKey,
new SystemEditStampCacheItem { At = DateTime.Now },
DefaultOptions);
}
}