Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions AskFm/AskFm.API/AskFm.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.0" />
<PackageReference Include="StackExchange.Redis" Version="2.9.11" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
</ItemGroup>

Expand Down
10 changes: 10 additions & 0 deletions AskFm/AskFm.API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,16 @@ public static void Main(string[] args)
})
.AddEntityFrameworkStores<AppDbContext>()
.AddDefaultTokenProviders();


builder.Services.AddStackExchangeRedisCache(options =>
{
options.Configuration = builder.Configuration.GetConnectionString("Redis");
options.InstanceName = "AskFmCache";
});

builder.Services.AddSingleton<RedisCacheService>();


var app = builder.Build();

Expand Down
3 changes: 2 additions & 1 deletion AskFm/AskFm.API/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"ConnectionStrings": {
"DefaultConnection": "CONNECTIONSTRING"
"DefaultConnection": "CONNECTIONSTRING",
"Redis": "localhost:6379"
},
"Logging": {
"LogLevel": {
Expand Down
14 changes: 14 additions & 0 deletions AskFm/AskFm.BLL/Services/IRedisService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.Extensions.Caching.Distributed;
using System.Text.Json;

namespace AskFm.BLL.Services;

public interface IRedisService
{
public Task SetCacheAsync<T>(string key, T value, TimeSpan expirationTime);

public Task<T?> GetCacheAsync<T>(string key);

public Task RemoveCacheAsync(string key);
}

37 changes: 37 additions & 0 deletions AskFm/AskFm.BLL/Services/RedisCacheService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Microsoft.Extensions.Caching.Distributed;
using System.Text.Json;

namespace AskFm.BLL.Services;

public class RedisCacheService : IRedisService
{
private readonly IDistributedCache _cache;

public RedisCacheService(IDistributedCache cache)
{
_cache = cache;
}

public async Task SetCacheAsync<T>(string key, T value, TimeSpan expirationTime)
{
var options = new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = expirationTime
};

var jsonData = JsonSerializer.Serialize(value);
await _cache.SetStringAsync(key, jsonData, options);
}

public async Task<T?> GetCacheAsync<T>(string key)
{
var jsonData = await _cache.GetStringAsync(key);
return jsonData is null ? default : JsonSerializer.Deserialize<T?>(jsonData);
}

public async Task RemoveCacheAsync(string key)
{
await _cache.RemoveAsync(key);
}
}

Loading