Skip to content

Commit

Permalink
Merge pull request #83 from Olaren15/skip-caching-option
Browse files Browse the repository at this point in the history
Added option to skip caching when enumerating rows
  • Loading branch information
NotAdam authored May 19, 2024
2 parents 8520307 + 185e989 commit db00faa
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
23 changes: 17 additions & 6 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 Down Expand Up @@ -111,7 +114,11 @@ public IEnumerator< T > GetEnumerator()
}

var obj = ReadSubRowObj( parser, rowPtr.RowId, i );
_rowCache.TryAdd( cacheKey, obj );
if( !NoCache.IsEnabled )
{
_rowCache.TryAdd( cacheKey, obj );
}

yield return obj;
}
}
Expand All @@ -125,12 +132,16 @@ public IEnumerator< T > GetEnumerator()
}

var obj = ReadRowObj( parser, rowPtr.RowId );
_rowCache.TryAdd( cacheKey, obj );
if( !NoCache.IsEnabled )
{
_rowCache.TryAdd( cacheKey, obj );
}

yield return obj;
}
}
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
Expand Down
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 db00faa

Please sign in to comment.