#34 add size based cache
This commit is contained in:
@@ -36,7 +36,8 @@ namespace OpenCand.API
|
||||
{
|
||||
FileProvider = new PhysicalFileProvider(Path.Combine(workingDir, "fotos_cand")),
|
||||
RequestPath = "/assets/fotos"
|
||||
}); app.UseHttpsRedirection();
|
||||
});
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
// Use rate limiting middleware
|
||||
app.UseRateLimiter();
|
||||
@@ -53,11 +54,18 @@ namespace OpenCand.API
|
||||
|
||||
app.Run();
|
||||
}
|
||||
private static void SetupServices(WebApplicationBuilder builder)
|
||||
|
||||
private static void SetupServices(WebApplicationBuilder builder)
|
||||
{
|
||||
builder.Services.Configure<FotosSettings>(builder.Configuration.GetSection("FotosSettings"));
|
||||
builder.Services.AddMemoryCache();
|
||||
|
||||
|
||||
// Configure memory cache with size limit from appsettings
|
||||
var sizeLimitMB = builder.Configuration.GetValue<int>("CacheSettings:SizeLimitMB", 15);
|
||||
builder.Services.AddMemoryCache(options =>
|
||||
{
|
||||
options.SizeLimit = sizeLimitMB * 1024L * 1024L; // Convert MB to bytes
|
||||
});
|
||||
|
||||
builder.Services.AddScoped(provider => new OpenCandRepository(provider.GetRequiredService<IConfiguration>(), provider.GetService<IMemoryCache>()));
|
||||
builder.Services.AddScoped(provider => new CandidatoRepository(provider.GetRequiredService<IConfiguration>(), provider.GetService<IMemoryCache>()));
|
||||
builder.Services.AddScoped(provider => new BemCandidatoRepository(provider.GetRequiredService<IConfiguration>(), provider.GetService<IMemoryCache>()));
|
||||
@@ -66,7 +74,7 @@ namespace OpenCand.API
|
||||
|
||||
builder.Services.AddScoped<OpenCandService>();
|
||||
builder.Services.AddScoped<EstatisticaService>();
|
||||
|
||||
|
||||
// Add cache preload background service
|
||||
builder.Services.AddHostedService<CachePreloadService>();
|
||||
}
|
||||
|
@@ -1,5 +1,7 @@
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Npgsql;
|
||||
using System.Text.Json;
|
||||
using System.Text;
|
||||
|
||||
namespace OpenCand.Repository
|
||||
{
|
||||
@@ -56,7 +58,8 @@ namespace OpenCand.Repository
|
||||
_cache.Set(cacheKey, result, new MemoryCacheEntryOptions
|
||||
{
|
||||
SlidingExpiration = expiration ?? DefaultCacheExpiration,
|
||||
Priority = priority ?? DefaultCachePriority
|
||||
Priority = priority ?? DefaultCachePriority,
|
||||
Size = EstimateSize(result)
|
||||
});
|
||||
}
|
||||
|
||||
@@ -99,7 +102,8 @@ namespace OpenCand.Repository
|
||||
_cache.Set(cacheKey, result, new MemoryCacheEntryOptions
|
||||
{
|
||||
SlidingExpiration = expiration ?? DefaultCacheExpiration,
|
||||
Priority = priority ?? DefaultCachePriority
|
||||
Priority = priority ?? DefaultCachePriority,
|
||||
Size = EstimateSize(result)
|
||||
});
|
||||
}
|
||||
|
||||
@@ -133,5 +137,25 @@ namespace OpenCand.Repository
|
||||
{
|
||||
return identifier != null ? $"{entityName}_{identifier}" : entityName;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Estimates the memory size of an object by serializing it to JSON
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of the object</typeparam>
|
||||
/// <param name="obj">The object to estimate size for</param>
|
||||
/// <returns>Estimated size in bytes</returns>
|
||||
private static long EstimateSize<T>(T obj)
|
||||
{
|
||||
if (obj == null) return 0;
|
||||
try
|
||||
{
|
||||
var json = JsonSerializer.Serialize(obj);
|
||||
return Encoding.UTF8.GetByteCount(json);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 1024; // Default estimate if serialization fails
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -12,5 +12,8 @@
|
||||
"Path": "./fotos_cand",
|
||||
"ApiBasePath": "http://localhost:5299/assets/fotos"
|
||||
},
|
||||
"CacheSettings": {
|
||||
"SizeLimitMB": 15
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
|
Reference in New Issue
Block a user