-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5947e47
commit aa044bb
Showing
22 changed files
with
264 additions
and
66 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
src/Subjects/Common.Persistence/Cache/CacheConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace Goodtocode.Common.Persistence.Cache; | ||
|
||
/// <summary> | ||
/// "CacheConfiguration": { | ||
/// "AbsoluteExpirationInHours": 1, | ||
/// "SlidingExpirationInMinutes": 30 | ||
/// } | ||
/// </summary> | ||
public class CacheConfiguration | ||
{ | ||
public int AbsoluteExpirationInHours { get; set; } | ||
public int SlidingExpirationInMinutes { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Goodtocode.Common.Persistence.Cache; | ||
|
||
public enum CacheTypes | ||
{ | ||
Redis, | ||
Memory | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Goodtocode.Common.Persistence.Cache; | ||
|
||
public interface ICacheService | ||
{ | ||
bool TryGet<T>(string cacheKey, out T value); | ||
T Set<T>(string cacheKey, T value); | ||
void Remove(string cacheKey); | ||
} |
39 changes: 39 additions & 0 deletions
39
src/Subjects/Common.Persistence/Cache/MemoryCacheService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Microsoft.Extensions.Caching.Memory; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Goodtocode.Common.Persistence.Cache; | ||
|
||
public class MemoryCacheService : ICacheService | ||
{ | ||
private readonly IMemoryCache _memoryCache; | ||
private readonly CacheConfiguration _cacheConfig; | ||
private readonly MemoryCacheEntryOptions _cacheOptions; | ||
public MemoryCacheService(IMemoryCache memoryCache, IOptions<CacheConfiguration> cacheConfig) | ||
Check warning on line 11 in src/Subjects/Common.Persistence/Cache/MemoryCacheService.cs
|
||
{ | ||
_memoryCache = memoryCache; | ||
_cacheConfig = cacheConfig.Value; | ||
if (_cacheConfig != null) | ||
{ | ||
_cacheOptions = new MemoryCacheEntryOptions | ||
{ | ||
AbsoluteExpiration = DateTime.Now.AddHours(_cacheConfig.AbsoluteExpirationInHours), | ||
Priority = CacheItemPriority.High, | ||
SlidingExpiration = TimeSpan.FromMinutes(_cacheConfig.SlidingExpirationInMinutes) | ||
}; | ||
} | ||
} | ||
public bool TryGet<T>(string cacheKey, out T value) | ||
{ | ||
_memoryCache.TryGetValue(cacheKey, out value); | ||
if (value == null) return false; | ||
else return true; | ||
} | ||
public T Set<T>(string cacheKey, T value) | ||
{ | ||
return _memoryCache.Set(cacheKey, value, _cacheOptions); | ||
} | ||
public void Remove(string cacheKey) | ||
{ | ||
_memoryCache.Remove(cacheKey); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/Subjects/Common.Persistence/Cache/RedisCacheService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace Goodtocode.Common.Persistence.Cache; | ||
|
||
public class RedisCacheService : ICacheService | ||
{ | ||
public void Remove(string cacheKey) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
public T Set<T>(string cacheKey, T value) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
public bool TryGet<T>(string cacheKey, out T value) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<RootNamespace>Goodtocode.Common.Persistence</RootNamespace> | ||
<AssemblyName>Goodtocode.Common.Persistence</AssemblyName> | ||
<Version>1.0.0</Version> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.8" /> | ||
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="7.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.1" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using Goodtocode.Common.Persistence.Cache; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Goodtocode.Common.Persistence; | ||
|
||
public static class ConfigureServices | ||
{ | ||
public static IServiceCollection AddInfrastructureServices(this IServiceCollection services, | ||
IConfiguration configuration) | ||
{ | ||
services.Configure<CacheConfiguration>(options => { configuration.GetSection("CacheConfiguration"); }); | ||
//For In-Memory Caching | ||
services.AddMemoryCache(); | ||
services.AddTransient<MemoryCacheService>(); | ||
services.AddTransient<RedisCacheService>(); | ||
services.AddTransient<Func<CacheTypes, ICacheService>>(serviceProvider => key => | ||
{ | ||
return key switch | ||
{ | ||
CacheTypes.Memory => serviceProvider.GetService<MemoryCacheService>(), | ||
CacheTypes.Redis => serviceProvider.GetService<RedisCacheService>(), | ||
_ => serviceProvider.GetService<MemoryCacheService>(), | ||
}; | ||
}); | ||
|
||
return services; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/Subjects/Common.Persistence/Repository/GenericRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using Goodtocode.Common.Persistence.Cache; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Goodtocode.Common.Persistence.Repository; | ||
|
||
public class GenericRepository<T> : IGenericRepository<T> where T : class | ||
{ | ||
private readonly static CacheTypes CacheTypes = CacheTypes.Memory; | ||
private readonly string cacheKey = $"{typeof(T)}"; | ||
private readonly DbContext _dbContext; | ||
private readonly Func<CacheTypes, ICacheService> _cacheService; | ||
public GenericRepository(DbContext dbContext, Func<CacheTypes, ICacheService> cacheService) | ||
{ | ||
_dbContext = dbContext; | ||
_cacheService = cacheService; | ||
} | ||
public virtual async Task<T> GetByIdAsync(int id) | ||
{ | ||
return await _dbContext.Set<T>().FindAsync(id); | ||
} | ||
public async Task<IReadOnlyList<T>> GetAllAsync() | ||
{ | ||
if (!_cacheService(CacheTypes).TryGet(cacheKey, out IReadOnlyList<T> cachedList)) | ||
{ | ||
cachedList = await _dbContext.Set<T>().ToListAsync(); | ||
_cacheService(CacheTypes).Set(cacheKey, cachedList); | ||
} | ||
return cachedList; | ||
} | ||
public async Task<T> AddAsync(T entity) | ||
{ | ||
await _dbContext.Set<T>().AddAsync(entity); | ||
await _dbContext.SaveChangesAsync(); | ||
await RefreshCache(); | ||
return entity; | ||
} | ||
public async Task UpdateAsync(T entity) | ||
{ | ||
_dbContext.Entry(entity).State = EntityState.Modified; | ||
await _dbContext.SaveChangesAsync(); | ||
await RefreshCache(); | ||
} | ||
public async Task DeleteAsync(T entity) | ||
{ | ||
_dbContext.Set<T>().Remove(entity); | ||
await _dbContext.SaveChangesAsync(); | ||
await RefreshCache(); | ||
} | ||
public async Task RefreshCache() | ||
{ | ||
_cacheService(CacheTypes).Remove(cacheKey); | ||
var cachedList = await _dbContext.Set<T>().ToListAsync(); | ||
_cacheService(CacheTypes).Set(cacheKey, cachedList); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/Subjects/Common.Persistence/Repository/IGenericRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Goodtocode.Common.Persistence.Repository; | ||
|
||
public interface IGenericRepository<T> where T : class | ||
{ | ||
Task<T> GetByIdAsync(int id); | ||
Task<IReadOnlyList<T>> GetAllAsync(); | ||
Task<T> AddAsync(T entity); | ||
Task UpdateAsync(T entity); | ||
Task DeleteAsync(T entity); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.