Skip to content

Commit

Permalink
Control caching via the NoCache class
Browse files Browse the repository at this point in the history
  • Loading branch information
Olaren15 committed May 6, 2024
1 parent ecc1b7f commit 185e989
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
22 changes: 10 additions & 12 deletions src/Lumina/Excel/ExcelSheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@ public ExcelSheet( ExcelHeaderFile headerFile, string name, Language requestedLa
{
rowObj.PopulateData( parser, GameData, RequestedLanguage );
}

_rowCache[ cacheKey ] = rowObj;


if( !NoCache.IsEnabled )
{
_rowCache[ cacheKey ] = rowObj;
}

return rowObj;
}

Expand All @@ -82,7 +85,7 @@ private T ReadSubRowObj( RowParser parser, uint rowId, uint subRowId )
return obj;
}

public IEnumerator< T > GetEnumerator( bool skipCaching )
public IEnumerator< T > GetEnumerator()
{
ExcelDataFile file = null!;
RowParser parser = null!;
Expand Down Expand Up @@ -111,7 +114,7 @@ public IEnumerator< T > GetEnumerator( bool skipCaching )
}

var obj = ReadSubRowObj( parser, rowPtr.RowId, i );
if( !skipCaching )
if( !NoCache.IsEnabled )
{
_rowCache.TryAdd( cacheKey, obj );
}
Expand All @@ -129,7 +132,7 @@ public IEnumerator< T > GetEnumerator( bool skipCaching )
}

var obj = ReadRowObj( parser, rowPtr.RowId );
if( !skipCaching )
if( !NoCache.IsEnabled )
{
_rowCache.TryAdd( cacheKey, obj );
}
Expand All @@ -139,14 +142,9 @@ public IEnumerator< T > GetEnumerator( bool skipCaching )
}
}

public IEnumerator< T > GetEnumerator()
{
return GetEnumerator( false );
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator( false );
return GetEnumerator();
}
}
}
29 changes: 29 additions & 0 deletions src/Lumina/Excel/NoCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;

namespace Lumina.Excel
{
/// <summary>
/// Class that allows to skip the caching of Excel rows when they are read.
/// </summary>
public sealed class NoCache : IDisposable
{
[field: ThreadStatic]
internal static bool IsEnabled { get; private set; }

/// <summary>
/// Disables the caching of Excel rows when they are read.
/// </summary>
public NoCache()
{
IsEnabled = true;
}

/// <summary>
/// Re-enables the caching of Excel rows when they are read.
/// </summary>
public void Dispose()
{
IsEnabled = false;
}
}
}

0 comments on commit 185e989

Please sign in to comment.