[AZ-310] [AZ-311] Route tile endpoints through ITileService

Move cache+DB+download logic for /tiles/{z}/{x}/{y} and
/api/satellite/tiles/latlon out of Program.cs into TileService.
Endpoints now inject only ITileService + ILogger. Service owns
IMemoryCache (1h absolute / 30min sliding preserved). Added
TileBytes DTO; ITileService gains GetOrDownloadTileAsync and
DownloadAndStoreSingleTileAsync. 5 new unit tests cover cache
hit, repo hit, downloader fallback, and AZ-311 happy + error.

Build clean (0/0), unit suite 40/40. Resolves architecture
baseline F3 in code (docs handled by AZ-315).

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Oleksandr Bezdieniezhnykh
2026-05-10 06:06:11 +03:00
parent 220277b9c7
commit 12b582deac
12 changed files with 387 additions and 121 deletions
@@ -7,6 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.10" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.10" />
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.10" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.10" />
+80 -22
View File
@@ -1,6 +1,8 @@
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using SatelliteProvider.Common.DTO;
using SatelliteProvider.Common.Interfaces;
using SatelliteProvider.Common.Utils;
using SatelliteProvider.DataAccess.Models;
using SatelliteProvider.DataAccess.Repositories;
@@ -8,17 +10,25 @@ namespace SatelliteProvider.Services;
public class TileService : ITileService
{
private static readonly TimeSpan TileCacheAbsolute = TimeSpan.FromHours(1);
private static readonly TimeSpan TileCacheSliding = TimeSpan.FromMinutes(30);
private static readonly TimeSpan TileResponseMaxAge = TimeSpan.FromDays(1);
private const string TileImageContentType = "image/jpeg";
private readonly ISatelliteDownloader _downloader;
private readonly ITileRepository _tileRepository;
private readonly IMemoryCache _cache;
private readonly ILogger<TileService> _logger;
public TileService(
ISatelliteDownloader downloader,
ITileRepository tileRepository,
IMemoryCache cache,
ILogger<TileService> logger)
{
_downloader = downloader;
_tileRepository = tileRepository;
_cache = cache;
_logger = logger;
}
@@ -48,8 +58,6 @@ public class TileService : ITileService
cancellationToken);
var result = new List<TileMetadata>();
int reusedCount = existingTilesList.Count;
int downloadedCount = downloadedTiles.Count;
foreach (var existingTile in existingTilesList)
{
@@ -58,26 +66,7 @@ public class TileService : ITileService
foreach (var downloadedTile in downloadedTiles)
{
var now = DateTime.UtcNow;
var tileEntity = new TileEntity
{
Id = Guid.NewGuid(),
TileZoom = downloadedTile.ZoomLevel,
TileX = downloadedTile.X,
TileY = downloadedTile.Y,
Latitude = downloadedTile.CenterLatitude,
Longitude = downloadedTile.CenterLongitude,
TileSizeMeters = downloadedTile.TileSizeMeters,
TileSizePixels = 256,
ImageType = "jpg",
MapsVersion = $"downloaded_{now:yyyy-MM-dd}",
Version = currentVersion,
FilePath = downloadedTile.FilePath,
CreatedAt = now,
UpdatedAt = now
};
var tileEntity = BuildTileEntity(downloadedTile, currentVersion);
await _tileRepository.InsertAsync(tileEntity);
result.Add(MapToMetadata(tileEntity));
}
@@ -101,6 +90,75 @@ public class TileService : ITileService
return tiles.Select(MapToMetadata);
}
public async Task<TileBytes> GetOrDownloadTileAsync(int z, int x, int y, CancellationToken cancellationToken = default)
{
var cacheKey = $"tile_{z}_{x}_{y}";
var etag = $"\"{z}_{x}_{y}\"";
if (_cache.TryGetValue(cacheKey, out byte[]? cachedBytes) && cachedBytes != null)
{
return new TileBytes(cachedBytes, TileImageContentType, etag, TileResponseMaxAge);
}
string filePath;
var existing = await _tileRepository.GetByTileCoordinatesAsync(z, x, y);
if (existing != null && File.Exists(existing.FilePath))
{
filePath = existing.FilePath;
}
else
{
var tileCenter = GeoUtils.TileToWorldPos(x, y, z);
var downloaded = await _downloader.DownloadSingleTileAsync(tileCenter.Lat, tileCenter.Lon, z, cancellationToken);
var entity = BuildTileEntity(downloaded, DateTime.UtcNow.Year);
await _tileRepository.InsertAsync(entity);
filePath = entity.FilePath;
}
var bytes = await File.ReadAllBytesAsync(filePath, cancellationToken);
_cache.Set(cacheKey, bytes, new MemoryCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TileCacheAbsolute,
SlidingExpiration = TileCacheSliding
});
return new TileBytes(bytes, TileImageContentType, etag, TileResponseMaxAge);
}
public async Task<TileMetadata> DownloadAndStoreSingleTileAsync(
double latitude,
double longitude,
int zoomLevel,
CancellationToken cancellationToken = default)
{
var downloaded = await _downloader.DownloadSingleTileAsync(latitude, longitude, zoomLevel, cancellationToken);
var entity = BuildTileEntity(downloaded, DateTime.UtcNow.Year);
await _tileRepository.InsertAsync(entity);
return MapToMetadata(entity);
}
private static TileEntity BuildTileEntity(DownloadedTileInfoV2 downloaded, int currentVersion)
{
var now = DateTime.UtcNow;
return new TileEntity
{
Id = Guid.NewGuid(),
TileZoom = downloaded.ZoomLevel,
TileX = downloaded.X,
TileY = downloaded.Y,
Latitude = downloaded.CenterLatitude,
Longitude = downloaded.CenterLongitude,
TileSizeMeters = downloaded.TileSizeMeters,
TileSizePixels = 256,
ImageType = "jpg",
MapsVersion = $"downloaded_{now:yyyy-MM-dd}",
Version = currentVersion,
FilePath = downloaded.FilePath,
CreatedAt = now,
UpdatedAt = now
};
}
private static TileMetadata MapToMetadata(TileEntity entity)
{
return new TileMetadata