added proper redis support

This commit is contained in:
Egor 2022-08-28 04:15:05 +03:00
parent b9814fd408
commit 834eea7e1c
9 changed files with 95 additions and 12 deletions

25
EFTask/.dockerignore Normal file
View file

@ -0,0 +1,25 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md

View file

@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.2.32630.192
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EFTask", "EFTask\EFTask.csproj", "{38961979-A830-4012-9CD2-A7AFA0201540}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFTask", "EFTask\EFTask.csproj", "{38961979-A830-4012-9CD2-A7AFA0201540}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution

View file

@ -5,12 +5,14 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.0-preview.7.22376.2" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="7.0.0-preview.7.22376.6" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0-preview.7.22375.6" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.15.1" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.0-preview.7" />
<PackageReference Include="Sharprompt" Version="2.4.3" />
<PackageReference Include="StackExchange.Redis" Version="2.6.48" />

View file

@ -84,6 +84,7 @@ namespace EFTask
var player = new FootballPlayer() { Name = name, Age = age };
fc.Players.Add(player);
fc.SaveChanges();
fc.RedisCache.StringSet("total_players", fc.Players.Count());
Console.WriteLine($"Successfully inserted a new player with an id {player.Id}");
}
@ -109,6 +110,7 @@ namespace EFTask
foreach (var player in playersToDelete)
fc.Players.Remove(player);
fc.SaveChanges();
fc.RedisCache.StringSet("total_players", fc.Players.Count());
Console.WriteLine($"Successfully deleted {playersToDelete.Count()} players");
}
@ -138,6 +140,7 @@ namespace EFTask
var team = new FootballTeam() { Name = name };
fc.Teams.Add(team);
fc.SaveChanges();
fc.RedisCache.StringSet("overall_expenses", fc.Contracts.Sum(c => c.Salary).ToString());
Console.WriteLine($"Successfully inserted a new team with an id {team.Id}");
}
@ -162,6 +165,7 @@ namespace EFTask
foreach (var team in teamsToDelete)
fc.Teams.Remove(team);
fc.SaveChanges();
fc.RedisCache.StringSet("overall_expenses", fc.Contracts.Sum(c => c.Salary).ToString());
Console.WriteLine($"Successfully deleted {teamsToDelete.Count()} teams");
}
@ -214,6 +218,7 @@ namespace EFTask
var contract = new FootbalContract() { Player = player, Team = team, Salary = salary };
fc.Contracts.Add(contract);
fc.SaveChanges();
fc.RedisCache.StringSet("overall_expenses", fc.Contracts.Sum(c => c.Salary).ToString());
Console.WriteLine($"Successfully inserted a new contract with an id {contract.Id}");
}
@ -243,6 +248,7 @@ namespace EFTask
var salary = Prompt.Input<decimal>("Input the player's salary", contract.Salary, contract.Salary.ToString());
contract.Salary = salary;
fc.SaveChanges();
fc.RedisCache.StringSet("overall_expenses", fc.Contracts.Sum(c => c.Salary).ToString());
Console.WriteLine($"Successfully updated a contract with an id {contract.Id}");
}
@ -254,6 +260,7 @@ namespace EFTask
foreach (var contract in contractsToDelete)
fc.Contracts.Remove(contract);
fc.SaveChanges();
fc.RedisCache.StringSet("overall_expenses", fc.Contracts.Sum(c => c.Salary).ToString());
Console.WriteLine($"Successfully deleted {contractsToDelete.Count()} contracts");
}
@ -262,27 +269,43 @@ namespace EFTask
Console.WriteLine("Calculating amount of players in a team...");
var team = Prompt.Select("Select a team", fc.Teams, 10,
textSelector: t => $"{t.Id}. {t.Name}");
var cachedValue = fc.RedisCache.StringGet(team.Id.ToString());
if (cachedValue != StackExchange.Redis.RedisValue.Null)
{
var result = cachedValue.ToString();
Console.WriteLine($"There are {result} players in team {team.Name}");
}
var playersAmount = team.Contracts.Count();
Console.WriteLine($"There are {playersAmount} players in team {team.Name}");
}
protected void PlayersTotalAmount(FootballContext fc)
{
throw new NotImplementedException();
var cachedValue = fc.RedisCache.StringGet("total_players");
string playersAmount;
if (cachedValue.IsNull)
{
playersAmount = fc.Players.Count().ToString();
fc.RedisCache.StringSet("total_players", playersAmount);
}
else playersAmount = cachedValue.ToString();
Console.WriteLine($"There are {playersAmount} players in total");
}
protected void TeamExpenses(FootballContext fc)
{
throw new NotImplementedException();
Console.WriteLine("Calculating expenses of a team...");
var team = Prompt.Select("Select a team", fc.Teams, 10,
textSelector: t => $"{t.Id}. {t.Name}");
var expenses = team.Contracts.Sum(c => c.Salary);
Console.WriteLine($"Expenses of team {team.Name} are {expenses} $/year");
}
protected void OverallExpenses(FootballContext fc)
{
throw new NotImplementedException();
var cachedValue = fc.RedisCache.StringGet("overall_expenses");
string expenses;
if (cachedValue.IsNull)
{
expenses = fc.Contracts.Sum(c => c.Salary).ToString();
fc.RedisCache.StringSet("overall_expenses", expenses);
}
else expenses = cachedValue.ToString();
Console.WriteLine($"Overall expenses are {expenses} $/year");
}
protected void Quit(FootballContext _)

View file

@ -0,0 +1,10 @@
{
"profiles": {
"EFTask": {
"commandName": "Project"
},
"Docker": {
"commandName": "Docker"
}
}
}

View file

@ -1,6 +1,6 @@
{
"ConnectionStrings": {
"Postgres": "host=localhost;port=5432;database=football;username=cbgr;password=cbgr",
"Redis": "localhost:6379"
"Postgres": "host=localhost;port=6543;database=football;username=cbgr;password=cbgr",
"Redis": "localhost:7623"
}
}

View file

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" Sdk="Microsoft.Docker.Sdk">
<PropertyGroup Label="Globals">
<ProjectVersion>2.1</ProjectVersion>
<DockerTargetOS>Linux</DockerTargetOS>
<ProjectGuid>e5b07c27-2842-4f0d-ade4-ae8afc101721</ProjectGuid>
</PropertyGroup>
<ItemGroup>
<None Include="docker-compose.override.yml">
<DependentUpon>docker-compose.yml</DependentUpon>
</None>
<None Include="docker-compose.yml" />
<None Include=".dockerignore" />
</ItemGroup>
</Project>

View file

@ -0,0 +1 @@
version: '3.4'

View file

@ -0,0 +1,7 @@
version: '3.4'
name: EFTask
services:
psql:
build: .
image: postgres:latest