Skip to content

Commit d850e83

Browse files
authored
Merge pull request #118 from MechanicalPriest/76-add-method-to-update-entire-cache
Add cache clearing functionality to services - Implemented associated tests
2 parents 98d9f7b + 0588e98 commit d850e83

File tree

7 files changed

+117
-3
lines changed

7 files changed

+117
-3
lines changed

SimcProfileParser.Tests/DataSync/CacheServiceTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,5 +244,39 @@ public void CS_Ptr_Defaults_Off()
244244
// Assert
245245
Assert.That(url, Is.EqualTo("https://raw.githubusercontent.com/simulationcraft/simc/test_branch/engine/dbc/generated/test.inc"));
246246
}
247+
248+
[Test]
249+
public async Task CS_ClearCache_Deletes_Files_And_Recovers()
250+
{
251+
// Arrange
252+
CacheService cache = new CacheService(null, _loggerFactory.CreateLogger<CacheService>());
253+
254+
var configuration = new CacheFileConfiguration()
255+
{
256+
LocalParsedFile = "CombatRatingMultipliers.json",
257+
ParsedFileType = SimcParsedFileType.CombatRatingMultipliers,
258+
RawFiles = new Dictionary<string, string>()
259+
{
260+
{ "ScaleData.raw", "sc_scale_data" }
261+
}
262+
};
263+
var filePath = Path.Combine(cache.BaseFileDirectory, "ScaleData.raw");
264+
265+
// Ensure a file exists by downloading it
266+
_ = await cache.GetRawFileContentsAsync(configuration, "ScaleData.raw");
267+
FileAssert.Exists(filePath);
268+
269+
// Act: clear the cache
270+
await cache.ClearCacheAsync();
271+
272+
// Assert: file was deleted
273+
Assert.That(File.Exists(filePath), Is.False, "Cache file should be removed after clear.");
274+
275+
// Act again: accessing should re-download
276+
_ = await cache.GetRawFileContentsAsync(configuration, "ScaleData.raw");
277+
278+
// Assert: file is back
279+
FileAssert.Exists(filePath);
280+
}
247281
}
248282
}

SimcProfileParser.Tests/SimcGenerationServiceIntegrationTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,27 @@ public async Task SGS_Gets_Game_Version()
135135
ClassicAssert.IsNotNull(version);
136136
ClassicAssert.AreEqual("12.", version.Substring(0, 3));
137137
}
138+
139+
[Test]
140+
public async Task SGS_ClearsCache()
141+
{
142+
// Arrange: Warm up by generating something that touches the cache
143+
var spellOptions = new SimcSpellOptions()
144+
{
145+
ItemLevel =226,
146+
SpellId =343538,
147+
ItemQuality = ItemQuality.ITEM_QUALITY_EPIC,
148+
ItemInventoryType = InventoryType.INVTYPE_TRINKET
149+
};
150+
var spell = await _sgs.GenerateSpellAsync(spellOptions);
151+
ClassicAssert.IsNotNull(spell);
152+
153+
// Act: clear the cache
154+
await _sgs.ClearCacheAsync();
155+
156+
// Assert: call again should still work (re-hydrates cache)
157+
var spell2 = await _sgs.GenerateSpellAsync(spellOptions);
158+
ClassicAssert.IsNotNull(spell2);
159+
}
138160
}
139161
}

SimcProfileParser/DataSync/CacheService.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,5 +501,34 @@ public void SetUseBranchName(string branchName)
501501
{
502502
_useBranchName = branchName;
503503
}
504+
505+
public async Task ClearCacheAsync()
506+
{
507+
// Clear in-memory caches
508+
_cachedFileData.Clear();
509+
_eTagCacheData.Clear();
510+
511+
// Clear on-disk cache
512+
try
513+
{
514+
if (Directory.Exists(BaseFileDirectory))
515+
{
516+
foreach (var file in Directory.GetFiles(BaseFileDirectory))
517+
{
518+
try { File.Delete(file); }
519+
catch (Exception ex)
520+
{
521+
_logger?.LogWarning(ex, "Failed to delete cache file {file}", file);
522+
}
523+
}
524+
}
525+
}
526+
catch (Exception ex)
527+
{
528+
_logger?.LogError(ex, "Error clearing cache directory {BaseFileDirectory}", BaseFileDirectory);
529+
}
530+
531+
await Task.CompletedTask;
532+
}
504533
}
505534
}

SimcProfileParser/Interfaces/DataSync/ICacheService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,10 @@ public interface ICacheService
5656
/// </summary>
5757
/// <param name="branchName">e.g. thewarwithin</param>
5858
void SetUseBranchName(string branchName);
59+
60+
/// <summary>
61+
/// Clears all cached data from memory and disk.
62+
/// </summary>
63+
Task ClearCacheAsync();
5964
}
6065
}

SimcProfileParser/Interfaces/ISimcGenerationService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ public interface ISimcGenerationService
2727
Task<SimcSpell> GenerateSpellAsync(SimcSpellOptions options);
2828

2929
Task<string> GetGameDataVersionAsync();
30+
31+
/// <summary>
32+
/// Clears all cached data used by the generator (both memory and disk cache).
33+
/// </summary>
34+
Task ClearCacheAsync();
3035
// TODO: Make this public once implemented.
3136
//Task<List<SimcTalent>> GetAvailableTalentsAsync(int classId, int specId);
3237
}

SimcProfileParser/SimcGenerationService.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,5 +477,24 @@ public string UseBranchName
477477
get => _cacheService.UseBranchName;
478478
set => _cacheService.SetUseBranchName(value);
479479
}
480+
481+
/// <summary>
482+
/// Clears the cached data for profiles, items, spells, and talents.
483+
/// </summary>
484+
/// <remarks>
485+
/// <para>
486+
/// This method will remove all cached entries associated with the current character or profile.
487+
/// It is useful for refreshing data after game updates, profile changes, or debugging.
488+
/// </para>
489+
/// <para>
490+
/// After clearing the cache, the next profile generation will
491+
/// re-download and re-process all required data from SimulationCraft's data files.
492+
/// </para>
493+
/// </remarks>
494+
/// <seealso cref="SimcProfileParser.Interfaces.ICacheService.ClearCacheAsync"/>
495+
public async Task ClearCacheAsync()
496+
{
497+
await _cacheService.ClearCacheAsync();
498+
}
480499
}
481500
}

SimcProfileParser/SimcProfileParser.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
<PropertyGroup>
44
<TargetFrameworks>net9.0</TargetFrameworks>
55
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
6-
<Version>3.1.0</Version>
7-
<AssemblyVersion>3.1.0</AssemblyVersion>
8-
<FileVersion>3.1.0</FileVersion>
6+
<Version>3.1.1</Version>
7+
<AssemblyVersion>3.1.1</AssemblyVersion>
8+
<FileVersion>3.1.1</FileVersion>
99
<Authors>Mechanical Priest</Authors>
1010
<Company>Mechanical Priest</Company>
1111
<PackageLicenseExpression>GPL-3.0-only</PackageLicenseExpression>

0 commit comments

Comments
 (0)