Added caching to the settings

pull/1676/head
tidusjar 7 years ago
parent fc31db4c6e
commit 22a23d652b

@ -13,13 +13,6 @@ namespace Ombi.Store.Repository
GlobalSettings Insert(GlobalSettings entity);
Task<GlobalSettings> InsertAsync(GlobalSettings entity);
/// <summary>
/// Gets all.
/// </summary>
/// <returns></returns>
IEnumerable<GlobalSettings> GetAll();
Task<IEnumerable<GlobalSettings>> GetAllAsync();
/// <summary>
/// Gets the specified identifier.
/// </summary>

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Ombi.Store.Context;
using Ombi.Store.Entities;
@ -10,77 +11,76 @@ namespace Ombi.Store.Repository
{
public class SettingsJsonRepository : ISettingsRepository
{
public SettingsJsonRepository(IOmbiContext ctx)
public SettingsJsonRepository(IOmbiContext ctx, IMemoryCache mem)
{
Db = ctx;
_cache = mem;
}
private IOmbiContext Db { get; }
private readonly IMemoryCache _cache;
public GlobalSettings Insert(GlobalSettings entity)
{
_cache.Remove(entity.SettingsName);
var settings = Db.Settings.Add(entity);
Db.SaveChanges();
return settings.Entity;
}
public async Task<GlobalSettings> InsertAsync(GlobalSettings entity)
{
_cache.Remove(entity.SettingsName);
var settings = await Db.Settings.AddAsync(entity).ConfigureAwait(false);
await Db.SaveChangesAsync().ConfigureAwait(false);
return settings.Entity;
}
public IEnumerable<GlobalSettings> GetAll()
{
var page = Db.Settings.AsNoTracking().ToList();
return page;
}
public async Task<IEnumerable<GlobalSettings>> GetAllAsync()
{
var page = await Db.Settings.AsNoTracking().ToListAsync();
return page;
}
public GlobalSettings Get(string pageName)
{
return _cache.GetOrCreate(pageName, entry =>
{
entry.AbsoluteExpiration = DateTimeOffset.Now.AddHours(1);
var entity = Db.Settings.AsNoTracking().FirstOrDefault(x => x.SettingsName == pageName);
return entity;
});
}
public async Task<GlobalSettings> GetAsync(string settingsName)
{
return await _cache.GetOrCreateAsync(settingsName, async entry =>
{
entry.AbsoluteExpiration = DateTimeOffset.Now.AddHours(1);
var obj = await Db.Settings.AsNoTracking().FirstOrDefaultAsync(x => x.SettingsName == settingsName);
return obj;
});
}
public async Task DeleteAsync(GlobalSettings entity)
{
_cache.Remove(entity.SettingsName);
Db.Settings.Remove(entity);
await Db.SaveChangesAsync();
}
public async Task UpdateAsync(GlobalSettings entity)
{
_cache.Remove(entity.SettingsName);
Db.Update(entity);
await Db.SaveChangesAsync();
}
public void Delete(GlobalSettings entity)
{
_cache.Remove(entity.SettingsName);
Db.Settings.Remove(entity);
Db.SaveChanges();
}
public void Update(GlobalSettings entity)
{
_cache.Remove(entity.SettingsName);
Db.SaveChanges();
}
}

Loading…
Cancel
Save