SystemEditStampCache.cs 1.42 KB
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);
    }
}