Skip to content

Commit

Permalink
reformat code && implement memory cache for ThumbnailQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Oct 24, 2024
1 parent b4fa5c2 commit f657488
Show file tree
Hide file tree
Showing 10 changed files with 423 additions and 403 deletions.
232 changes: 116 additions & 116 deletions starsky/starsky.feature.import/Services/Import.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public async Task StartBackgroundQueue(DateTime endTime)
var currentPage = 0;
const int batchSize = 100;

if ( _thumbnailQuery.IsRunningJobAsync() )
if ( _thumbnailQuery.IsRunningJob() )
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public class ThumbnailQuery : IThumbnailQuery
{
private readonly ApplicationDbContext _context;
private readonly IWebLogger _logger;
private readonly IMemoryCache _memoryCache;
private readonly IMemoryCache? _memoryCache;
private readonly IServiceScopeFactory? _scopeFactory;

public ThumbnailQuery(ApplicationDbContext context, IServiceScopeFactory? scopeFactory,
IWebLogger logger, IMemoryCache memoryCache)
IWebLogger logger, IMemoryCache? memoryCache = null)
{
_context = context;
_scopeFactory = scopeFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,53 +1,58 @@
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using starsky.foundation.database.Helpers;
using starsky.foundation.database.Interfaces;
using starsky.foundation.database.Models;
using starsky.foundation.platform.Interfaces;

namespace starsky.foundation.database.Thumbnails
namespace starsky.foundation.database.Thumbnails;

public sealed class ThumbnailQueryFactory
{
public sealed class ThumbnailQueryFactory
private readonly IWebLogger _logger;
private readonly IMemoryCache? _memoryCache;
private readonly IServiceScopeFactory? _serviceScopeFactory;
private readonly SetupDatabaseTypes? _setupDatabaseTypes;
private readonly IThumbnailQuery? _thumbnailQuery;

public ThumbnailQueryFactory(SetupDatabaseTypes? setupDatabaseTypes,
IServiceScopeFactory? serviceScopeFactory, IThumbnailQuery? thumbnailQuery,
IWebLogger logger, IMemoryCache memoryCache)
{
private readonly SetupDatabaseTypes? _setupDatabaseTypes;
private readonly IServiceScopeFactory? _serviceScopeFactory;
private readonly IThumbnailQuery? _thumbnailQuery;
private readonly IWebLogger _logger;
_setupDatabaseTypes = setupDatabaseTypes;
_serviceScopeFactory = serviceScopeFactory;
_thumbnailQuery = thumbnailQuery;
_logger = logger;
_memoryCache = memoryCache;
}

public ThumbnailQueryFactory(SetupDatabaseTypes? setupDatabaseTypes, IServiceScopeFactory? serviceScopeFactory, IThumbnailQuery? thumbnailQuery, IWebLogger logger)
public IThumbnailQuery? ThumbnailQuery()
{
if ( _thumbnailQuery == null )
{
_setupDatabaseTypes = setupDatabaseTypes;
_serviceScopeFactory = serviceScopeFactory;
_thumbnailQuery = thumbnailQuery;
_logger = logger;
return null;
}

public IThumbnailQuery? ThumbnailQuery()
var context = _setupDatabaseTypes?.BuilderDbFactory();
if ( _thumbnailQuery.GetType() == typeof(ThumbnailQuery) && context != null &&
_memoryCache != null )
{
if ( _thumbnailQuery == null )
{
return null;
}

var context = _setupDatabaseTypes?.BuilderDbFactory();
if ( _thumbnailQuery.GetType() == typeof(ThumbnailQuery) && context != null )
{
return new ThumbnailQuery(context, _serviceScopeFactory, _logger);
}

// FakeIQuery should skip creation
var isAnyContentIncluded = _thumbnailQuery.GetReflectionFieldValue<List<ThumbnailItem>?>("_content")?.Count != 0;

if ( !isAnyContentIncluded )
{
return Activator.CreateInstance(_thumbnailQuery.GetType(),
context, _serviceScopeFactory, _logger) as IThumbnailQuery;
}

_logger.LogInformation("FakeIThumbnailQuery _content detected");
return _thumbnailQuery;
return new ThumbnailQuery(context, _serviceScopeFactory, _logger, _memoryCache);
}

// FakeIQuery should skip creation
var isAnyContentIncluded =
_thumbnailQuery.GetReflectionFieldValue<List<ThumbnailItem>?>("_content")?.Count != 0;

if ( !isAnyContentIncluded )
{
return Activator.CreateInstance(_thumbnailQuery.GetType(),
context, _serviceScopeFactory, _logger) as IThumbnailQuery;
}

_logger.LogInformation("FakeIThumbnailQuery _content detected");
return _thumbnailQuery;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
using starsky.foundation.database.Models;
using starsky.foundation.injection;
using starsky.foundation.platform.Enums;
using starsky.foundation.platform.Helpers;
using starsky.foundation.thumbnailgeneration.Interfaces;
using starsky.foundation.thumbnailgeneration.Models;

namespace starsky.foundation.thumbnailgeneration.Services;

[Service(typeof(IUpdateStatusGeneratedThumbnailService), InjectionLifetime = InjectionLifetime.Scoped)]
[Service(typeof(IUpdateStatusGeneratedThumbnailService),
InjectionLifetime = InjectionLifetime.Scoped)]
public class UpdateStatusGeneratedThumbnailService : IUpdateStatusGeneratedThumbnailService
{
private readonly IThumbnailQuery _thumbnailQuery;
Expand All @@ -22,7 +22,7 @@ public UpdateStatusGeneratedThumbnailService(IThumbnailQuery thumbnailQuery)
}

/// <summary>
/// Ignores the not found items to update
/// Ignores the not found items to update
/// </summary>
/// <param name="generationResults">items</param>
/// <returns>updated data transfer list</returns>
Expand All @@ -44,6 +44,7 @@ public async Task<List<ThumbnailResultDataTransferModel>> AddOrUpdateStatusAsync
{
continue;
}

dtoObjects[index].Change(generationResult.Size, generationResult.Success);
dtoObjects[index].Reasons = generationResult.ErrorMessage;
}
Expand All @@ -53,11 +54,12 @@ public async Task<List<ThumbnailResultDataTransferModel>> AddOrUpdateStatusAsync
}

/// <summary>
/// Remove items with status not found
/// Remove items with status not found
/// </summary>
/// <param name="generationResults">items</param>
/// <returns>remove by fileHash</returns>
public async Task<List<string>> RemoveNotfoundStatusAsync(List<GenerationResultModel> generationResults)
public async Task<List<string>> RemoveNotfoundStatusAsync(
List<GenerationResultModel> generationResults)
{
// in the next step only the fileHash is included
var dtoObjects = generationResults
Expand Down
9 changes: 6 additions & 3 deletions starsky/starskytest/FakeMocks/FakeIThumbnailQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ namespace starskytest.FakeMocks;

public class FakeIThumbnailQuery : IThumbnailQuery
{
private bool _isRunningJob;
private readonly List<ThumbnailItem> _content = new();

public FakeIThumbnailQuery(List<ThumbnailItem>? items = null)
public FakeIThumbnailQuery(List<ThumbnailItem>? items = null, bool isRunningJob = false)
{
_isRunningJob = isRunningJob;
if ( items != null )
{
_content = items;
Expand Down Expand Up @@ -133,11 +135,12 @@ public Task<bool> UpdateAsync(ThumbnailItem item)

public bool IsRunningJob()
{
throw new NotImplementedException();
return _isRunningJob;
}

public bool SetRunningJob(bool value)
{
throw new NotImplementedException();
_isRunningJob = value;
return _isRunningJob;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public async Task Preflight_SingleImage_HappyFlow()
new FakeIImportQuery(),
new FakeExifTool(_iStorageFake, appSettings), null!, _console,
new FakeIMetaExifThumbnailService(), new FakeIWebLogger(),
new FakeIThumbnailQuery());
new FakeIThumbnailQuery(), new FakeMemoryCache());

var result = await importService.Preflight(
new List<string> { "/test.jpg" },
Expand All @@ -94,7 +94,7 @@ public async Task Preflight_SingleImage_Ignore()
new FakeIImportQuery(),
new FakeExifTool(_iStorageFake, appSettings), null!,
_console, new FakeIMetaExifThumbnailService(), new FakeIWebLogger(),
new FakeIThumbnailQuery());
new FakeIThumbnailQuery(), new FakeMemoryCache());

var result = await importService.Preflight(
new List<string> { "/test.jpg" },
Expand All @@ -114,7 +114,7 @@ public async Task Preflight_SingleImage_Ignore_ColorClassOverwrite()
new FakeIImportQuery(),
new FakeExifTool(_iStorageFake, appSettings), null!,
_console, new FakeIMetaExifThumbnailService(), new FakeIWebLogger(),
new FakeIThumbnailQuery());
new FakeIThumbnailQuery(), new FakeMemoryCache());

var result = await importService.Preflight(
new List<string> { "/color_class_winner.jpg" },
Expand All @@ -138,7 +138,7 @@ public async Task Preflight_SingleImage_ForceOverWrite_ColorClassOverwrite()
new FakeIImportQuery(),
new FakeExifTool(_iStorageFake, appSettings),
null!, _console, new FakeIMetaExifThumbnailService(),
new FakeIWebLogger(), new FakeIThumbnailQuery());
new FakeIWebLogger(), new FakeIThumbnailQuery(), new FakeMemoryCache());

var result = await importService.Preflight(
new List<string> { "/color_class_winner.jpg" }, // <- in this test we change it
Expand Down Expand Up @@ -170,7 +170,7 @@ public async Task Preflight_SingleImage_DateGetByFileNameNoExif()
new FakeIImportQuery(),
new FakeExifTool(_iStorageFake, appSettings), null!, _console,
new FakeIMetaExifThumbnailService(), new FakeIWebLogger(),
new FakeIThumbnailQuery());
new FakeIThumbnailQuery(), new FakeMemoryCache());

var result = await importService.Preflight(
new List<string> { "/2020-04-27 11:07:00.jpg" },
Expand All @@ -197,7 +197,7 @@ public async Task Preflight_SingleImage_FileType_NotSupported()
new FakeIImportQuery(),
new FakeExifTool(_iStorageFake, appSettings), null!, _console,
new FakeIMetaExifThumbnailService(), new FakeIWebLogger(),
new FakeIThumbnailQuery());
new FakeIThumbnailQuery(), new FakeMemoryCache());

var result = await importService.Preflight(
new List<string> { "/test.jpg" },
Expand Down Expand Up @@ -686,7 +686,7 @@ public async Task Importer_IOException_FileFailsWritingToSubPath()
), appSettings, fakeImportQuery,
new FakeExifTool(_iStorageFake, appSettings), fakeDbQuery, _console,
new FakeIMetaExifThumbnailService(), new FakeIWebLogger(),
new FakeIThumbnailQuery());
new FakeIThumbnailQuery(), new FakeMemoryCache());

var result = await importService.Importer(
new ImportIndexItem
Expand Down
Loading

0 comments on commit f657488

Please sign in to comment.