dev
This commit is contained in:
parent
c451a67b8c
commit
24e7c49030
|
|
@ -0,0 +1,43 @@
|
|||
using GameData.Repository;
|
||||
using GameModel;
|
||||
using GameModel.Contract;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.Caching;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace GameData.CacheProxy
|
||||
{
|
||||
/// <summary>
|
||||
/// cache for repository data, implemented as proxy not as decorator https://www.youtube.com/watch?v=nmAE-JzNSEw
|
||||
/// </summary>
|
||||
public class CachedExpenseRepository : IExpenseRepository
|
||||
{
|
||||
IMemoryCache _cache;
|
||||
ExpenseRepository _repository;
|
||||
|
||||
public CachedExpenseRepository(ExpenseRepository repository, IMemoryCache cache)
|
||||
{
|
||||
_repository = repository;
|
||||
_cache = cache;
|
||||
}
|
||||
|
||||
public Task Delete(IEnumerable<PlayerExpense> enumerable)
|
||||
{
|
||||
return _repository.Delete(enumerable);
|
||||
}
|
||||
|
||||
public Task<IEnumerable<Expense>> GetAll()
|
||||
{
|
||||
return _cache.GetOrCreateAsync("allexpenses", factory => _repository.GetAll());
|
||||
}
|
||||
|
||||
public Task<PlayerExpense> Save(PlayerExpense data)
|
||||
{
|
||||
return _repository.Save(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -8,8 +8,10 @@
|
|||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Autofac" Version="7.1.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="System.Runtime.Caching" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -18,9 +18,7 @@ namespace GameData.Repository
|
|||
|
||||
string UrlExpense => "items/expense";
|
||||
string UrlPlayerExpense => "items/playerexpense";
|
||||
List<PlayerExpense> _memberExpenses = new List<PlayerExpense>();
|
||||
|
||||
private List<Expense> _expenses = null;
|
||||
List<PlayerExpense> _memberExpenses = new List<PlayerExpense>();
|
||||
|
||||
public ExpenseRepository(ILogger<ExpenseRepository> log, ApiClient apiClient)
|
||||
{
|
||||
|
|
@ -32,18 +30,9 @@ namespace GameData.Repository
|
|||
|
||||
public async Task<IEnumerable<Expense>> GetAll()
|
||||
{
|
||||
if (_expenses == null)
|
||||
{
|
||||
_log?.LogDebug("loading expenses from api");
|
||||
var res = await _apiClient.Get<Expense>(UrlExpense);
|
||||
_expenses = new List<Expense> { };
|
||||
_expenses.AddRange(res);
|
||||
}
|
||||
else
|
||||
{
|
||||
_log?.LogDebug("returning expenses from cache");
|
||||
}
|
||||
return _expenses;
|
||||
_log?.LogDebug("loading expenses from api");
|
||||
var res = await _apiClient.Get<Expense>(UrlExpense);
|
||||
return res;
|
||||
}
|
||||
|
||||
public async Task<PlayerExpense> Save(PlayerExpense memberExpense)
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@
|
|||
<PackageReference Include="AsyncAwaitBestPractices" Version="7.0.0" />
|
||||
<PackageReference Include="AutofacSerilogIntegration" Version="5.0.0" />
|
||||
<PackageReference Include="CommandLineParser" Version="2.9.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ using Autofac.Core;
|
|||
using AutofacSerilogIntegration;
|
||||
using CommandLine;
|
||||
using GameData;
|
||||
using GameData.CacheProxy;
|
||||
using GameData.Dummy;
|
||||
using GameData.Repository;
|
||||
using GameHandler;
|
||||
|
|
@ -15,9 +16,11 @@ using GameModel.Contracts;
|
|||
using GameModel.DeathGame;
|
||||
using GameModel.Settings;
|
||||
using KoogleCli.Model;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Serilog;
|
||||
using Spectre.Console;
|
||||
using Spectre.Console.Json;
|
||||
|
|
@ -53,9 +56,13 @@ Autofac.IContainer Register()
|
|||
RegisterAppsettings(builder);
|
||||
|
||||
RegisterLogging(builder);
|
||||
|
||||
var test = new MemoryCache(new MemoryCacheOptions());
|
||||
builder.RegisterType<ApiClient>();
|
||||
builder.RegisterType<ExpenseRepository>().As<IExpenseRepository>().InstancePerLifetimeScope();
|
||||
builder.RegisterType<CachedExpenseRepository>().As<IExpenseRepository>().InstancePerLifetimeScope();
|
||||
builder.RegisterType<ExpenseRepository>().InstancePerLifetimeScope();
|
||||
builder.RegisterInstance(new MemoryCacheOptions()).As<IOptions<MemoryCacheOptions>>();
|
||||
builder.RegisterType<MemoryCache>().As<IMemoryCache>().InstancePerLifetimeScope();
|
||||
|
||||
builder.RegisterType<GameRepository>().As<IGameRepository>().InstancePerLifetimeScope();
|
||||
return builder.Build();
|
||||
}
|
||||
|
|
@ -136,9 +143,18 @@ void StartGameAction(string gameName, Guid gameId)
|
|||
|
||||
if (gameId.Equals(Guid.Empty))
|
||||
{
|
||||
var starttask = _gs.Start(new[] { 1, 2, 3, 4 }, GetGameSettings(gameName), gameName);
|
||||
gameState = starttask.Result;
|
||||
} else
|
||||
try
|
||||
{
|
||||
var starttask = _gs.Start(new[] { 1, 2, 3, 4 }, GetGameSettings(gameName), gameName);
|
||||
gameState = starttask.Result;
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.Message);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var starttask = _gs.Load(gameId);
|
||||
gameState = starttask.Result;
|
||||
|
|
|
|||
Loading…
Reference in New Issue