feat(dal): IRedisCacheService
This commit is contained in:
13
PARR.DAL/CacheServices/IRedisCacheService.cs
Normal file
13
PARR.DAL/CacheServices/IRedisCacheService.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
namespace PARR.DAL.CacheServices
|
||||
{
|
||||
public interface IRedisCacheService
|
||||
{
|
||||
T? GetCachedData<T>(string key);
|
||||
|
||||
Task<T?> GetCachedDataAsync<T>(string key);
|
||||
|
||||
void SetCachedData<T>(string key, T data, TimeSpan cacheDuration);
|
||||
|
||||
Task SetCachedDataAsync<T>(string key, T data, TimeSpan cacheDuration);
|
||||
}
|
||||
}
|
||||
63
PARR.DAL/CacheServices/RedisCacheService.cs
Normal file
63
PARR.DAL/CacheServices/RedisCacheService.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace PARR.DAL.CacheServices
|
||||
{
|
||||
internal class RedisCacheService : IRedisCacheService
|
||||
{
|
||||
private readonly IDistributedCache cache;
|
||||
|
||||
public RedisCacheService(IDistributedCache cache)
|
||||
{
|
||||
this.cache = cache;
|
||||
}
|
||||
|
||||
public async Task<T?> GetCachedDataAsync<T>(string key)
|
||||
{
|
||||
var jsonData = await cache.GetStringAsync(key);
|
||||
|
||||
if (jsonData == null)
|
||||
return default(T);
|
||||
|
||||
return JsonSerializer.Deserialize<T>(jsonData);
|
||||
}
|
||||
|
||||
|
||||
public T? GetCachedData<T>(string key)
|
||||
{
|
||||
var jsonData = cache.GetString(key);
|
||||
|
||||
if (jsonData == null)
|
||||
return default(T);
|
||||
|
||||
return JsonSerializer.Deserialize<T>(jsonData);
|
||||
}
|
||||
|
||||
|
||||
public void SetCachedData<T>(string key, T data, TimeSpan cacheDuration)
|
||||
{
|
||||
var options = new DistributedCacheEntryOptions
|
||||
{
|
||||
AbsoluteExpirationRelativeToNow = cacheDuration
|
||||
};
|
||||
|
||||
var jsonData = JsonSerializer.Serialize(data);
|
||||
cache.SetString(key, jsonData, options);
|
||||
}
|
||||
|
||||
|
||||
public async Task SetCachedDataAsync<T>(string key, T data, TimeSpan cacheDuration)
|
||||
{
|
||||
var options = new DistributedCacheEntryOptions
|
||||
{
|
||||
AbsoluteExpirationRelativeToNow = cacheDuration
|
||||
};
|
||||
|
||||
var jsonData = JsonSerializer.Serialize(data);
|
||||
await cache.SetStringAsync(key, jsonData, options);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
2756
PARR.DAL/Migrations/20231130020915_TblHostsAddIndexes.Designer.cs
generated
Normal file
2756
PARR.DAL/Migrations/20231130020915_TblHostsAddIndexes.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
27
PARR.DAL/Migrations/20231130020915_TblHostsAddIndexes.cs
Normal file
27
PARR.DAL/Migrations/20231130020915_TblHostsAddIndexes.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace PARR.DAL.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class TblHostsAddIndexes : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Hosts_Ek_IP",
|
||||
table: "Hosts",
|
||||
columns: new[] { "Ek", "IP" });
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropIndex(
|
||||
name: "IX_Hosts_Ek_IP",
|
||||
table: "Hosts");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1636,6 +1636,8 @@ namespace PARR.DAL.Migrations
|
||||
|
||||
b.HasIndex("ResponseAreaCode");
|
||||
|
||||
b.HasIndex("Ek", "IP");
|
||||
|
||||
b.ToTable("Hosts");
|
||||
});
|
||||
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
using PARR.DAL.Models.Base;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PARR.DAL.Models.Base;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace PARR.DAL.Models
|
||||
{
|
||||
[Table("Hosts")]
|
||||
[Index(nameof(Ek), nameof(IP))]
|
||||
public class Host : IBase
|
||||
{
|
||||
[Key]
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using PARR.DAL.CacheServices;
|
||||
using PARR.DAL.Configurations.DbSettings;
|
||||
using PARR.DAL.Context;
|
||||
using PARR.DAL.Contracts;
|
||||
@@ -30,6 +31,13 @@ namespace PARR.DAL
|
||||
.UseNpgsql(configuration.GetConnectionString("DefaultConnection"))
|
||||
);
|
||||
|
||||
services.AddStackExchangeRedisCache(opt =>
|
||||
{
|
||||
opt.Configuration = configuration.GetConnectionString("RedisConnection");
|
||||
});
|
||||
|
||||
services.AddTransient<IRedisCacheService, RedisCacheService>();
|
||||
|
||||
// Entity services
|
||||
services.AddTransient<IHostService, HostService>();
|
||||
services.AddTransient<IApplicationService, ApplicationService>();
|
||||
|
||||
Reference in New Issue
Block a user