Skip to content

Commit 01a14f0

Browse files
committed
Added Caching Structure
1 parent 5a4bd16 commit 01a14f0

File tree

4 files changed

+72
-4
lines changed

4 files changed

+72
-4
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Linq.Expressions;
3+
using System.Threading.Tasks;
4+
using LinkDotNet.Blog.Domain;
5+
using Microsoft.Extensions.Caching.Memory;
6+
using X.PagedList;
7+
8+
namespace LinkDotNet.Blog.Infrastructure.Persistence
9+
{
10+
public class CachedRepository<T> : IRepository<T>
11+
where T : Entity
12+
{
13+
private readonly IRepository<T> repository;
14+
private readonly IMemoryCache memoryCache;
15+
16+
public CachedRepository(IRepository<T> repository, IMemoryCache memoryCache)
17+
{
18+
this.repository = repository;
19+
this.memoryCache = memoryCache;
20+
}
21+
22+
public async Task<T> GetByIdAsync(string id)
23+
{
24+
if (memoryCache.TryGetValue(id, out T item))
25+
{
26+
return item;
27+
}
28+
29+
var fromDb = await repository.GetByIdAsync(id);
30+
memoryCache.Set(id, fromDb);
31+
return fromDb;
32+
}
33+
34+
public async Task<IPagedList<T>> GetAllAsync(
35+
Expression<Func<T, bool>> filter = null,
36+
Expression<Func<T, object>> orderBy = null,
37+
bool descending = true,
38+
int page = 1,
39+
int pageSize = int.MaxValue)
40+
{
41+
var key = $"{page}-{pageSize}";
42+
return await memoryCache.GetOrCreate(key, async e =>
43+
{
44+
e.SetOptions(new MemoryCacheEntryOptions
45+
{
46+
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(1),
47+
});
48+
return await repository.GetAllAsync(filter, orderBy, descending, page, pageSize);
49+
});
50+
}
51+
52+
public async Task StoreAsync(T entity)
53+
{
54+
await repository.StoreAsync(entity);
55+
}
56+
57+
public async Task DeleteAsync(string id)
58+
{
59+
await repository.DeleteAsync(id);
60+
}
61+
}
62+
}

LinkDotNet.Blog.Web/LinkDotNet.Blog.Web.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="6.0.0-rc.1.21452.15" />
1212
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.0-rc.1.21452.10" />
1313
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0-rc.1.21452.10" />
14+
<PackageReference Include="Scrutor.AspNetCore" Version="3.3.0" />
1415
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.29.0.36737">
1516
<PrivateAssets>all</PrivateAssets>
1617
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

LinkDotNet.Blog.Web/RegistrationExtensions/StorageProviderExtensions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using LinkDotNet.Blog.Infrastructure.Persistence;
1+
using LinkDotNet.Blog.Domain;
2+
using LinkDotNet.Blog.Infrastructure.Persistence;
3+
using LinkDotNet.Blog.Infrastructure.Persistence.Sql;
24
using Microsoft.Extensions.Configuration;
35
using Microsoft.Extensions.DependencyInjection;
46

@@ -26,6 +28,9 @@ public static void AddStorageProvider(this IServiceCollection services, IConfigu
2628
{
2729
services.UseSqlAsStorageProvider();
2830
}
31+
32+
services.AddScoped<IRepository<BlogPost>, Repository<BlogPost>>();
33+
services.Decorate(typeof(IRepository<>), typeof(CachedRepository<>));
2934
}
3035
}
3136
}

LinkDotNet.Blog.sln

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LinkDotNet.Blog.Infrastruct
1111
EndProject
1212
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LinkDotNet.Blog.UnitTests", "LinkDotNet.Blog.UnitTests\LinkDotNet.Blog.UnitTests.csproj", "{5B868911-7C93-4190-AEE4-3A6694F2FFCE}"
1313
EndProject
14-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LinkDotNet.Blog.IntegrationTests", "LinkDotNet.Blog.IntegrationTests\LinkDotNet.Blog.IntegrationTests.csproj", "{DEFDA17A-9586-4E50-83FB-8F75AC29D39A}"
14+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LinkDotNet.Blog.IntegrationTests", "LinkDotNet.Blog.IntegrationTests\LinkDotNet.Blog.IntegrationTests.csproj", "{DEFDA17A-9586-4E50-83FB-8F75AC29D39A}"
1515
EndProject
16-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LinkDotNet.Blog.TestUtilities", "LinkDotNet.Blog.TestUtilities\LinkDotNet.Blog.TestUtilities.csproj", "{310ABEE1-C131-43E6-A759-F2DB75A483DD}"
16+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LinkDotNet.Blog.TestUtilities", "LinkDotNet.Blog.TestUtilities\LinkDotNet.Blog.TestUtilities.csproj", "{310ABEE1-C131-43E6-A759-F2DB75A483DD}"
1717
EndProject
1818
Global
1919
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -52,4 +52,4 @@ Global
5252
GlobalSection(ExtensibilityGlobals) = postSolution
5353
SolutionGuid = {FB9B0642-F1F0-4BD8-9EDD-15C95F082180}
5454
EndGlobalSection
55-
EndGlobal
55+
EndGlobal

0 commit comments

Comments
 (0)