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.
iot/projects/Infrastructure/Application/Services/Settings/CachedSettingService.cs

71 lines
2.2 KiB

using Infrastructure.Application.Entites.Settings;
using Microsoft.Extensions.Caching.Distributed;
using System.Linq;
using Infrastructure.Extensions;
using System.Globalization;
using System;
namespace Infrastructure.Application.Services.Settings
{
public class CachedSettingService : ISettingService
{
private readonly IDistributedCache _cache;
private readonly SettingService _settingService;
private readonly string _keyTemplate = "iot-setting-{0}";
public CachedSettingService(IDistributedCache cache, SettingService settingService)
{
this._cache = cache;
this._settingService = settingService;
}
public Setting GetSetting(string name)
{
var key = string.Format(CultureInfo.InvariantCulture, _keyTemplate, name);
var setting = this._cache.Get<Setting>(key);
if (setting == null)
{
setting = this._settingService.GetSetting(name);
_cache.Set(key, setting);
}
return setting;
}
public void AddSetting(Setting setting)
{
this._settingService.AddSetting(setting);
}
public void DeleteSetting(Setting setting)
{
if (setting is null)
{
throw new ArgumentNullException(nameof(setting));
}
this._settingService.DeleteSetting(setting);
var key = string.Format(CultureInfo.InvariantCulture, _keyTemplate, setting.Name);
this._cache.Remove(key);
}
public void UpdateSetting(Setting setting)
{
if (setting is null)
{
throw new ArgumentNullException(nameof(setting));
}
this._settingService.UpdateSetting(setting);
var key = string.Format(CultureInfo.InvariantCulture, _keyTemplate, setting.Name);
this._cache.Remove(key);
}
public IQueryable<Setting> Table()
{
return _settingService.Table();
}
public IQueryable<Setting> ReadonlyTable()
{
return _settingService.ReadonlyTable();
}
}
}