Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

Commit 415ba32

Browse files
committed
Aggiunti metodi per utilizzo cache tramite REDIS
1 parent 07e4c6d commit 415ba32

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

src/NET6CustomLibrary/GlobalUsings.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
global using Microsoft.AspNetCore.Routing;
2020
global using Microsoft.Data.SqlClient;
2121
global using Microsoft.EntityFrameworkCore;
22+
global using Microsoft.Extensions.Caching.Distributed;
2223
global using Microsoft.Extensions.Configuration;
2324
global using Microsoft.Extensions.DependencyInjection;
2425
global using Microsoft.Extensions.Diagnostics.HealthChecks;
@@ -42,8 +43,9 @@
4243
global using NET6CustomLibrary.RabbitMQ.Abstractions;
4344
global using NET6CustomLibrary.Serilog.Models;
4445
global using NET6CustomLibrary.Serilog.Services;
46+
global using Newtonsoft.Json;
4547
global using Npgsql;
4648
global using RabbitMQ.Client;
4749
global using RabbitMQ.Client.Events;
4850
global using Serilog;
49-
global using Swashbuckle.AspNetCore.SwaggerGen;
51+
global using Swashbuckle.AspNetCore.SwaggerGen;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
namespace NET6CustomLibrary.RedisCache;
2+
3+
public class CacheService : ICacheService
4+
{
5+
private readonly IDistributedCache _cache;
6+
7+
public CacheService(IDistributedCache cache)
8+
{
9+
_cache = cache;
10+
}
11+
12+
/// <summary>
13+
/// Get the values from the cache
14+
/// </summary>
15+
/// <typeparam name="T"></typeparam>
16+
/// <param name="key"></param>
17+
/// <returns></returns>
18+
public T Get<T>(string key)
19+
{
20+
var value = _cache.GetString(key);
21+
22+
if (value != null)
23+
{
24+
return JsonConvert.DeserializeObject<T>(value);
25+
}
26+
27+
return default;
28+
}
29+
30+
/// <summary>
31+
/// Set the values in the cache
32+
/// </summary>
33+
/// <typeparam name="T"></typeparam>
34+
/// <param name="key"></param>
35+
/// <param name="value"></param>
36+
/// <param name="absoluteExpireTime"></param>
37+
/// <param name="slidingExpireTime"></param>
38+
/// <returns></returns>
39+
public T Set<T>(string key, T value, TimeSpan? absoluteExpireTime = null, TimeSpan? slidingExpireTime = null)
40+
{
41+
var options = new DistributedCacheEntryOptions
42+
{
43+
// Imposta l'ora in cui la cache scadrà a partire dall'ora di inserimento (che rappresenta l'adesso)
44+
AbsoluteExpirationRelativeToNow = absoluteExpireTime,
45+
46+
// Tempo fino al quale la voce della cache è valida, prima del quale se si verifica un hit il tempo deve essere esteso ulteriormente.
47+
SlidingExpiration = slidingExpireTime
48+
};
49+
50+
_cache.SetString(key, JsonConvert.SerializeObject(value), options);
51+
52+
return value;
53+
}
54+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace NET6CustomLibrary.RedisCache;
2+
3+
public interface ICacheService
4+
{
5+
T Get<T>(string key);
6+
T Set<T>(string key, T value, TimeSpan? absoluteExpireTime = null, TimeSpan? slidingExpireTime = null);
7+
}

0 commit comments

Comments
 (0)