-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2335 from planetarium/bugfix/table-cache
fix table caching
- Loading branch information
Showing
6 changed files
with
125 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
NineChronicles.Headless.Tests/MemoryCacheExtensionsTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Bencodex; | ||
using Bencodex.Types; | ||
using MessagePack; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using Microsoft.Extensions.Options; | ||
using Nekoyume; | ||
using Nekoyume.TableData; | ||
using Xunit; | ||
|
||
namespace NineChronicles.Headless.Tests; | ||
|
||
public class MemoryCacheExtensionsTest | ||
{ | ||
[Fact] | ||
public async Task Sheet() | ||
{ | ||
var codec = new Codec(); | ||
var lz4Options = MessagePackSerializerOptions.Standard.WithCompression(MessagePackCompression.Lz4BlockArray); | ||
var cache = new MemoryCache(new OptionsWrapper<MemoryCacheOptions>(new MemoryCacheOptions | ||
{ | ||
SizeLimit = null | ||
})); | ||
|
||
var sheets = TableSheetsImporter.ImportSheets(); | ||
var tableName = nameof(ItemRequirementSheet); | ||
var csv = sheets[tableName]; | ||
var cacheKey = Addresses.GetSheetAddress(tableName).ToString(); | ||
var value = (Text)csv; | ||
var compressed = MessagePackSerializer.Serialize(codec.Encode(value), lz4Options); | ||
cache.SetSheet(cacheKey, value, TimeSpan.FromMilliseconds(100)); | ||
Assert.True(cache.TryGetValue(cacheKey, out byte[] cached)); | ||
Assert.Equal(compressed, cached); | ||
Assert.Equal(csv, cache.GetSheet(cacheKey)); | ||
await Task.Delay(100); | ||
Assert.False(cache.TryGetValue(cacheKey, out byte[] _)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using Bencodex; | ||
using Bencodex.Types; | ||
using MessagePack; | ||
using Microsoft.Extensions.Caching.Memory; | ||
|
||
namespace NineChronicles.Headless; | ||
|
||
public static class MemoryCacheExtensions | ||
{ | ||
private static readonly Codec Codec = new Codec(); | ||
private static readonly MessagePackSerializerOptions Lz4Options = MessagePackSerializerOptions.Standard.WithCompression(MessagePackCompression.Lz4BlockArray); | ||
|
||
public static byte[] SetSheet(this MemoryCache cache, string cacheKey, IValue value, TimeSpan ex) | ||
{ | ||
var compressed = MessagePackSerializer.Serialize(Codec.Encode(value), Lz4Options); | ||
cache.Set(cacheKey, compressed, ex); | ||
return compressed; | ||
} | ||
|
||
public static string? GetSheet(this MemoryCache cache, string cacheKey) | ||
{ | ||
if (cache.TryGetValue(cacheKey, out byte[] cached)) | ||
{ | ||
return (Text)Codec.Decode(MessagePackSerializer.Deserialize<byte[]>(cached, Lz4Options)); | ||
} | ||
|
||
return null; | ||
} | ||
} |