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

61 lines
1.7 KiB

using Infrastructure.Application.Entites.Settings;
using Microsoft.Extensions.Caching.Distributed;
using System.Linq;
using Infrastructure.Extensions;
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 Get(string name)
{
var key = string.Format(_keyTemplate, name);
var setting = this._cache.Get<Setting>(key);
if (setting == null)
{
setting = this._settingService.Get(name);
_cache.Set(key, setting);
}
return setting;
}
public void Add(Setting setting)
{
this._settingService.Add(setting);
}
public void Delete(Setting setting)
{
this._settingService.Delete(setting);
var key = string.Format(_keyTemplate, setting.Name);
this._cache.Remove(key);
}
public void Update(Setting setting)
{
this._settingService.Update(setting);
var key = string.Format(_keyTemplate, setting.Name);
this._cache.Remove(key);
}
public IQueryable<Setting> Table()
{
return _settingService.Table();
}
public IQueryable<Setting> ReadonlyTable()
{
return _settingService.ReadonlyTable();
}
}
}