mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-07-14 01:02:57 -07:00
Added caching to the settings
This commit is contained in:
parent
fc31db4c6e
commit
22a23d652b
2 changed files with 22 additions and 29 deletions
|
@ -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…
Add table
Add a link
Reference in a new issue