Skip to content
Permalink
Browse files

first try of memory cache

  • Loading branch information
ignatandrei committed Jan 19, 2020
1 parent b8d3865 commit 3aeeab7a5c519493764a365885b812f3da6ccb8e
@@ -30,6 +30,10 @@ public GetECBExchange(HttpMessageHandler handler)

public async Task<IEnumerable<ExchangeRates>> GetActualRates()
{
var b = this as BankGetExchange;
if (b.TodayFromCache != null)
return b.TodayFromCache;

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
var ret = new List<ExchangeRates>();
var xml = await httpClient.GetStringAsync("https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");
@@ -55,6 +59,8 @@ public async Task<IEnumerable<ExchangeRates>> GetActualRates()
};
ret.Add(exch);
}
b.TodayFromCache = ret;

return ret;


@@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="McMaster.NETCore.Plugins" Version="0.3.1" />
<PackageReference Include="McMaster.NETCore.Plugins" Version="1.1.0" />
</ItemGroup>

<ItemGroup>
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
using System.Text;
using System.Threading.Tasks;

@@ -11,5 +13,35 @@ public interface BankGetExchange
{
public string Bank { get; }
Task<IEnumerable<ExchangeRates>> GetActualRates();

IEnumerable<ExchangeRates> TodayFromCache
{
get

{
string key = $"{this.Bank}_{DateTime.UtcNow.ToString("yyyyMMdd")}";
var mc = MemoryCache.Default;
if (mc.Contains(key))
return mc[key] as ExchangeRates[];

return null;

}
set
{
var now = DateTime.UtcNow;
var nextDay = now.Date.AddDays(1);
var offset = new DateTimeOffset(nextDay);
string key = $"{this.Bank}_{DateTime.UtcNow.ToString("yyyyMMdd")}";
var mc = MemoryCache.Default;
value = value
.Where(it => Math.Abs(it.Date.Subtract(now).TotalDays) < 1)
.ToArray();
if (value.Any())
mc.Set(key, value, offset);

}
}

}
}
@@ -9,4 +9,9 @@
<WarningsAsErrors />
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="3.1.1" />
<PackageReference Include="System.Runtime.Caching" Version="4.7.0" />
</ItemGroup>

</Project>
@@ -23,18 +23,18 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="4.0.0" />
<PackageReference Include="microsoft.aspnetcore.spaservices.extensions" Version="3.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.1">
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="4.1.1" />
<PackageReference Include="microsoft.aspnetcore.spaservices.extensions" Version="3.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.0.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.0.0" />
<PackageReference Include="NSwag.AspNetCore" Version="13.1.6" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.6.0" />
<PackageReference Include="System.ServiceModel.Syndication" Version="4.6.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.1.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.0" />
<PackageReference Include="NSwag.AspNetCore" Version="13.2.2" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.7.0" />
<PackageReference Include="System.ServiceModel.Syndication" Version="4.7.0" />
</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
@@ -5,17 +5,17 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.0.1">
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.0.1" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.6.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.1" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.7.0" />
</ItemGroup>

<ItemGroup>
@@ -26,19 +26,19 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="coverlet.msbuild" Version="2.7.0">
<PackageReference Include="coverlet.msbuild" Version="2.8.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0-preview-20200116-01" />
<PackageReference Include="RichardSzalay.MockHttp" Version="6.0.0" />
<PackageReference Include="System.Interactive.Async" Version="4.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.1.0">
<PackageReference Include="coverlet.collector" Version="1.2.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

0 comments on commit 3aeeab7

Please sign in to comment.
You can’t perform that action at this time.