Changed Shop interface to be more asynchronous.

Implemented changes in Props.

Implemented said changes in AdafruitShop.

Fixed and improved caching in AdafruitShop.
This commit is contained in:
2021-08-11 23:54:52 -05:00
parent ff080390f8
commit 8a1e5aca15
12 changed files with 145 additions and 76 deletions

View File

@@ -7,10 +7,10 @@ using Props.Shop.Framework;
namespace Props.Services.Modules
{
public interface IShopManager : IDisposable
public interface IShopManager : IAsyncDisposable
{
public IEnumerable<string> GetAllShopNames();
public IShop GetShop(string name);
public IEnumerable<IShop> GetAllShops();
public ValueTask<IEnumerable<string>> GetAllShopNames();
public ValueTask<IShop> GetShop(string name);
public ValueTask<IEnumerable<IShop>> GetAllShops();
}
}

View File

@@ -32,13 +32,13 @@ namespace Props.Services.Modules
metricsManager.RegisterSearchQuery(query);
logger.LogDebug("Searching for \"{0}\".", query);
foreach (string shopName in ShopManager.GetAllShopNames())
foreach (string shopName in await ShopManager.GetAllShopNames())
{
if (searchOutline.Enabled[shopName])
{
logger.LogDebug("Checking \"{0}\".", shopName);
int amount = 0;
await foreach (ProductListing product in ShopManager.GetShop(shopName).Search(query, searchOutline.Filters))
await foreach (ProductListing product in (await ShopManager.GetShop(shopName)).Search(query, searchOutline.Filters))
{
if (searchOutline.Filters.Validate(product))
{

View File

@@ -19,12 +19,12 @@ namespace Props.Services.Modules
{
public class ModularShopManager : IShopManager
{
private Task ShopLoadingTask;
private ILoggerFactory loggerFactory;
private ILogger<ModularShopManager> logger;
private Dictionary<string, IShop> shops;
private ModulesOptions options;
private IConfiguration configuration;
private bool disposedValue;
public ModularShopManager(IConfiguration configuration, ILogger<ModularShopManager> logger, ILoggerFactory loggerFactory)
{
@@ -34,26 +34,29 @@ namespace Props.Services.Modules
options = configuration.GetSection(ModulesOptions.Modules).Get<ModulesOptions>();
Directory.CreateDirectory(options.ModuleDataDir);
shops = new Dictionary<string, IShop>();
LoadShops();
ShopLoadingTask = LoadShops();
}
public IEnumerable<string> GetAllShopNames()
public async ValueTask<IEnumerable<string>> GetAllShopNames()
{
await ShopLoadingTask;
return shops.Keys;
}
public IShop GetShop(string name)
public async ValueTask<IShop> GetShop(string name)
{
await ShopLoadingTask;
return shops[name];
}
public IEnumerable<IShop> GetAllShops()
public async ValueTask<IEnumerable<IShop>> GetAllShops()
{
await ShopLoadingTask;
return shops.Values;
}
public void LoadShops()
private async Task LoadShops()
{
string shopsDir = options.ModulesDir;
string shopRegex = options.ShopRegex;
@@ -88,7 +91,7 @@ namespace Props.Services.Modules
{
DirectoryInfo dataDir = Directory.CreateDirectory(Path.Combine(options.ModuleDataDir, file.Substring(file.IndexOf(Path.DirectorySeparatorChar) + 1)));
logger.LogDebug("Checking data directory for \"{0}\" at \"{1}\"", Path.GetFileName(file), dataDir.FullName);
shop.Initialize(dataDir.FullName, loggerFactory);
await shop.Initialize(dataDir.FullName, loggerFactory);
success += 1;
if (!shops.TryAdd(shop.ShopName, shop))
{
@@ -107,27 +110,20 @@ namespace Props.Services.Modules
}
}
protected virtual void Dispose(bool disposing)
public async ValueTask DisposeAsync()
{
if (!disposedValue)
{
if (disposing)
{
foreach (string shopName in shops.Keys)
{
shops[shopName].SaveData().AsTask().Wait();
shops[shopName].Dispose();
}
}
disposedValue = true;
}
logger.LogDebug("Disposing...");
await DisposeAsyncCore();
}
public void Dispose()
protected virtual async ValueTask DisposeAsyncCore()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
await ShopLoadingTask;
foreach (string shopName in shops.Keys)
{
await shops[shopName].DisposeAsync();
logger.LogDebug("Completed dispose task for \"{0}\".", shopName);
}
}
}
}

Binary file not shown.