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); GlobalSettings Insert(GlobalSettings entity);
Task<GlobalSettings> InsertAsync(GlobalSettings entity); Task<GlobalSettings> InsertAsync(GlobalSettings entity);
/// <summary>
/// Gets all.
/// </summary>
/// <returns></returns>
IEnumerable<GlobalSettings> GetAll();
Task<IEnumerable<GlobalSettings>> GetAllAsync();
/// <summary> /// <summary>
/// Gets the specified identifier. /// Gets the specified identifier.
/// </summary> /// </summary>

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

Loading…
Cancel
Save