Skip to content

Commit

Permalink
PANIC
Browse files Browse the repository at this point in the history
  • Loading branch information
VibeNL committed Dec 20, 2023
1 parent 83ad1bb commit b51ee76
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
32 changes: 32 additions & 0 deletions GhostfolioSidekick/Ghostfolio/API/CacheKey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using GhostfolioSidekick.Model;

namespace GhostfolioSidekick.Ghostfolio.API
{
internal class CacheKey : IEquatable<CacheKey>
{
public string[] Identifiers { get; }
private AssetClass?[] expectedAssetClass;
private AssetSubClass?[] expectedAssetSubClass;

public CacheKey(string[] identifiers, AssetClass?[] expectedAssetClass, AssetSubClass?[] expectedAssetSubClass)
{
Identifiers = identifiers;
this.expectedAssetClass = expectedAssetClass;
this.expectedAssetSubClass = expectedAssetSubClass;
}

public bool Equals(CacheKey? other)

Check failure on line 18 in GhostfolioSidekick/Ghostfolio/API/CacheKey.cs

View workflow job for this annotation

GitHub Actions / build

'CacheKey.Equals(CacheKey?)': not all code paths return a value

Check failure on line 18 in GhostfolioSidekick/Ghostfolio/API/CacheKey.cs

View workflow job for this annotation

GitHub Actions / build

'CacheKey.Equals(CacheKey?)': not all code paths return a value
{
// TODO
}
public override bool Equals(object obj)

Check warning on line 22 in GhostfolioSidekick/Ghostfolio/API/CacheKey.cs

View workflow job for this annotation

GitHub Actions / build

Nullability of type of parameter 'obj' doesn't match overridden member (possibly because of nullability attributes).
{
return Equals(obj as CacheKey);
}

public override int GetHashCode()
{
return base.GetHashCode();
}
}
}
24 changes: 15 additions & 9 deletions GhostfolioSidekick/Ghostfolio/API/GhostfolioAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,31 +150,37 @@ public async Task UpdateAccount(Model.Account account)
return null;
}

foreach (var identifier in identifiers)
var key = new CacheKey(identifiers, expectedAssetClass, expectedAssetSubClass);

if (memoryCache.TryGetValue(key, out Model.SymbolProfile? asset))
{
if (memoryCache.TryGetValue(identifier, out Model.SymbolProfile? asset))
{
return asset;
}
return asset;
}

foreach (var identifier in identifiers)
{
var mappedIdentifier = mapper.MapSymbol(identifier);
var foundAsset = await FindByMarketData(identifier);
foundAsset ??= await FindByDataProvider(identifier, expectedCurrency, expectedAssetClass, expectedAssetSubClass, mappedIdentifier);

if (foundAsset != null)
{
AddToCache(identifier, foundAsset, memoryCache);
await UpdateKnownIdentifiers(foundAsset, mappedIdentifier, identifier);
AddToCache(key, foundAsset, memoryCache);
await UpdateKnownIdentifiers(foundAsset, identifiers);
return foundAsset;
}
}

AddToCache(key, null, memoryCache);
logger.LogError($"Could not find any identifier [{string.Join(",", identifiers)}] as a symbol");
return null;

static void AddToCache(string identifier, Model.SymbolProfile? asset, IMemoryCache cache)
static void AddToCache(CacheKey key, Model.SymbolProfile? asset, IMemoryCache cache)
{
cache.Set(identifier, asset, CacheDuration.Long());
if (asset != null)
{
cache.Set(key, asset, CacheDuration.Long());
}
}

async Task<Model.SymbolProfile?> FindByMarketData(string? identifier)
Expand Down
6 changes: 3 additions & 3 deletions GhostfolioSidekick/Ghostfolio/API/Mapper/SymbolMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ public SymbolMapper(IEnumerable<Mapping> mappings)
this.mappings = mappings.ToList();
}

internal string? MapCurrency(string? sourceCurrency)
internal string MapCurrency(string sourceCurrency)
{
return Map(MappingType.Currency, sourceCurrency);
}

internal string? MapSymbol(string? identifier)
internal string MapSymbol(string identifier)
{
return Map(MappingType.Symbol, identifier);
}

private string? Map(MappingType type, string? sourceCurrency)
private string Map(MappingType type, string sourceCurrency)
{
return mappings.SingleOrDefault(x => x.MappingType == type && x.Source == sourceCurrency)?.Target ?? sourceCurrency;
}
Expand Down

0 comments on commit b51ee76

Please sign in to comment.