You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.4 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* 版权所有 (c) 2025 WenJY 保留所有权利。
* CLR版本4.0.30319.42000
* 机器名称Mr.Wen's MacBook Pro
* 命名空间Sln.Imm.Daemon.Cache
* 唯一标识C61D97E2-2108-46A3-974C-AFE6C83258BA
*
* 创建者WenJY
* 电子邮箱:
* 创建时间2025-09-05 11:29:42
* 版本V1.0.0
* 描述:
*
*--------------------------------------------------------------------
* 修改人:
* 时间:
* 修改说明:
*
* 版本V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
using Sln.Imm.Daemon.Model.dao;
using Sln.Imm.Daemon.Repository.service;
using Sln.Imm.Daemon.Serilog;
using ZiggyCreatures.Caching.Fusion;
namespace Sln.Imm.Daemon.Cache;
public class BaseDeviceInfoCacheService
{
private readonly SerilogHelper _logger;
private readonly IFusionCache _fusionCache;
private readonly IBaseDeviceInfoService _service;
public BaseDeviceInfoCacheService(SerilogHelper logger, IFusionCache fusionCache, IBaseDeviceInfoService service)
{
_logger = logger;
_fusionCache = fusionCache;
_service = service;
}
/// <summary>
/// 通过缓存获取值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public async Task<List<BaseDeviceInfo>> GetValueAsync(string key)
{
var cachedValue = await _fusionCache.GetOrDefaultAsync<List<BaseDeviceInfo>>(key).ConfigureAwait(false);
if (cachedValue != null)
{
_logger.Info($"通过Cache获取设备数据:{cachedValue.Count};条");
return cachedValue;
}
else
{
var value = _service.GetDeviceInfosByNavigate();
//将值存入缓存,设置过期时间等
await _fusionCache.SetAsync(key, value, TimeSpan.FromMinutes(5)).ConfigureAwait(false);
_logger.Info($"通过ORM获取设备数据:{value.Count};条");
return value;
}
}
public async Task<bool> SetValueAsync(string key, BaseDeviceInfo deviceInfo)
{
var isRes = _service.Update(deviceInfo);
if (isRes)
{
var value = _service.GetDeviceInfosByNavigate();
await _fusionCache.SetAsync(key, value, TimeSpan.FromSeconds(5)).ConfigureAwait(false);
}
return isRes;
}
}