-
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.
Merge pull request #42 from goodtocode/34-subjects-business-list-pagi…
…nation-with-crud 34 subjects business list pagination with crud
- Loading branch information
Showing
98 changed files
with
2,075 additions
and
676 deletions.
There are no files selected for viewing
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
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 _cache; | ||
private readonly CacheConfiguration _config; | ||
private readonly MemoryCacheEntryOptions _options; | ||
public MemoryCacheService(IMemoryCache memoryCache, IOptions<CacheConfiguration> cacheConfig) | ||
{ | ||
_cache = memoryCache; | ||
_config = cacheConfig.Value; | ||
if (_config != null) | ||
{ | ||
_options = new MemoryCacheEntryOptions | ||
{ | ||
AbsoluteExpiration = DateTime.Now.AddHours(_config.AbsoluteExpirationInHours), | ||
Priority = CacheItemPriority.High, | ||
SlidingExpiration = TimeSpan.FromMinutes(_config.SlidingExpirationInMinutes) | ||
}; | ||
} | ||
} | ||
public bool TryGet<T>(string cacheKey, out T value) | ||
{ | ||
_cache.TryGetValue(cacheKey, out value); | ||
if (value == null) return false; | ||
else return true; | ||
} | ||
public T Set<T>(string cacheKey, T value) | ||
{ | ||
return _cache.Set(cacheKey, value, _options); | ||
} | ||
public void Remove(string cacheKey) | ||
{ | ||
_cache.Remove(cacheKey); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
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,43 @@ | ||
using Microsoft.Extensions.Caching.Distributed; | ||
using Microsoft.Extensions.Options; | ||
using System.Text.Json; | ||
|
||
namespace Goodtocode.Common.Persistence.Cache; | ||
|
||
public class RedisCacheService : ICacheService | ||
{ | ||
private readonly IDistributedCache _cache; | ||
private readonly CacheConfiguration _config; | ||
private readonly DistributedCacheEntryOptions _options; | ||
|
||
public RedisCacheService(IDistributedCache cache, IOptions<CacheConfiguration> cacheConfig) | ||
{ | ||
_cache = cache; | ||
_config = cacheConfig.Value; | ||
if (_config != null) | ||
{ | ||
_options = new DistributedCacheEntryOptions | ||
{ | ||
AbsoluteExpiration = DateTime.Now.AddHours(_config.AbsoluteExpirationInHours), | ||
SlidingExpiration = TimeSpan.FromMinutes(_config.SlidingExpirationInMinutes) | ||
}; | ||
} | ||
} | ||
|
||
public bool TryGet<T>(string cacheKey, out T value) | ||
{ | ||
var serialized = _cache.GetString(cacheKey); | ||
value = JsonSerializer.Deserialize<T>(serialized); | ||
if (value == null) return false; | ||
else return true; | ||
} | ||
public T Set<T>(string cacheKey, T value) | ||
{ | ||
_cache.SetString(cacheKey, JsonSerializer.Serialize(value), _options); | ||
return value; | ||
} | ||
public void Remove(string cacheKey) | ||
{ | ||
_cache.Remove(cacheKey); | ||
} | ||
} |
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; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/Subjects/Common.Persistence/Repository/CachedRepository.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,62 @@ | ||
using Goodtocode.Common.Persistence.Cache; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Goodtocode.Common.Persistence.Repository; | ||
|
||
public class CachedRepository<T> : ICachedRepository<T> where T : class | ||
{ | ||
private readonly static CacheTypes CacheType = CacheTypes.Memory; | ||
private readonly string cacheKey = $"{typeof(T)}"; | ||
private readonly DbContext _dbContext; | ||
private readonly Func<CacheTypes, ICacheService> _cacheService; | ||
|
||
public CachedRepository(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(CacheType).TryGet(cacheKey, out IReadOnlyList<T> cachedList)) | ||
{ | ||
cachedList = await _dbContext.Set<T>().ToListAsync(); | ||
_cacheService(CacheType).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(CacheType).Remove(cacheKey); | ||
var cachedList = await _dbContext.Set<T>().ToListAsync(); | ||
_cacheService(CacheType).Set(cacheKey, cachedList); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/Subjects/Common.Persistence/Repository/ICachedRepository.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 ICachedRepository<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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace Goodtocode.Common.Infrastructure.ApiClient; | ||
|
||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "RFC6749")] | ||
public class BearerToken | ||
{ | ||
public string token_type { get; set; } = string.Empty; | ||
|
||
public int expires_in { get; set; } | ||
|
||
public int ext_expires_in { get; set; } | ||
|
||
public string access_token { get; set; } = string.Empty; | ||
} |
File renamed without changes.
File renamed without changes.
7 changes: 3 additions & 4 deletions
7
...nt/Common.Infrastructure.ApiClient.csproj → .../Common.ApiClient/Common.ApiClient.csproj
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
File renamed without changes.
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.