-
Notifications
You must be signed in to change notification settings - Fork 333
MemoryPack Serializer #438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
13 changes: 13 additions & 0 deletions
13
...asyCaching.Serialization.MemoryPack/Configurations/EasyCachingMemPackSerializerOptions.cs
This file contains hidden or 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,13 @@ | ||
using MemoryPack; | ||
|
||
namespace EasyCaching.Serialization.MemoryPack; | ||
|
||
/// <summary> | ||
/// EasyCaching memory pack serializer options. | ||
/// </summary> | ||
public record EasyCachingMemPackSerializerOptions | ||
{ | ||
public StringEncoding StringEncoding { set; get; } | ||
} | ||
|
||
|
36 changes: 36 additions & 0 deletions
36
...ation/EasyCaching.Serialization.MemoryPack/Configurations/EasyCachingOptionsExtensions.cs
This file contains hidden or 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,36 @@ | ||
using MemoryPack; | ||
using EasyCaching.Core.Configurations; | ||
using EasyCaching.Serialization.Json; | ||
|
||
namespace EasyCaching.Serialization.MemoryPack; | ||
|
||
/// <summary> | ||
/// Easy caching options extensions. | ||
/// </summary> | ||
public static class EasyCachingOptionsExtensions | ||
{ | ||
/// <summary> | ||
/// Withs the memory pack serializer. | ||
/// </summary> | ||
/// <param name="options">Options.</param> | ||
/// <param name="name">The name of this serializer instance.</param> | ||
public static EasyCachingOptions WithMemoryPack(this EasyCachingOptions options, string name = "mempack") | ||
{ | ||
options.RegisterExtension(new MemoryPackOptionsExtension(name, null)); | ||
|
||
return options; | ||
} | ||
|
||
/// <summary> | ||
/// Withs the memory pack serializer. | ||
/// </summary> | ||
/// <param name="options">Options.</param> | ||
/// <param name="configure">Configure serializer settings.</param> | ||
/// <param name="name">The name of this serializer instance.</param> | ||
public static EasyCachingOptions WithMemoryPack(this EasyCachingOptions options, Action<EasyCachingMemPackSerializerOptions> serializerOptions, string name) | ||
{ | ||
options.RegisterExtension(new MemoryPackOptionsExtension(name, serializerOptions)); | ||
|
||
return options; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...ization/EasyCaching.Serialization.MemoryPack/Configurations/MemoryPackOptionsExtension.cs
This file contains hidden or 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,55 @@ | ||
namespace EasyCaching.Serialization.Json; | ||
|
||
using System; | ||
using EasyCaching.Core.Configurations; | ||
using EasyCaching.Core.Serialization; | ||
using EasyCaching.Serialization.MemoryPack; | ||
using global::MemoryPack; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
/// <summary> | ||
/// MemoryPack options extension. | ||
/// </summary> | ||
internal sealed class MemoryPackOptionsExtension : IEasyCachingOptionsExtension | ||
{ | ||
/// <summary> | ||
/// The name. | ||
/// </summary> | ||
private readonly string _name; | ||
|
||
/// <summary> | ||
/// The configure. | ||
/// </summary> | ||
private readonly Action<EasyCachingMemPackSerializerOptions> _configure; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="T:EasyCaching.Serialization.MemoryPack.MemoryPackOptionsExtension"/> class. | ||
/// </summary> | ||
/// <param name="name">Name.</param> | ||
/// <param name="configure">Configure.</param> | ||
public MemoryPackOptionsExtension(string name, Action<EasyCachingMemPackSerializerOptions> configure) | ||
{ | ||
this._name = name; | ||
this._configure = configure; | ||
} | ||
|
||
/// <summary> | ||
/// Adds the services. | ||
/// </summary> | ||
/// <param name="services">Services.</param> | ||
public void AddServices(IServiceCollection services) | ||
{ | ||
Action<EasyCachingMemPackSerializerOptions> configure = _configure ?? (_ => { }); | ||
|
||
services.AddOptions(); | ||
services.Configure(_name, configure); | ||
|
||
services.AddSingleton<IEasyCachingSerializer, DefaultMemoryPackSerializer>(x => | ||
{ | ||
var optionsMon = x.GetRequiredService<Microsoft.Extensions.Options.IOptionsMonitor<EasyCachingMemPackSerializerOptions>>(); | ||
var easyCachingOptions = optionsMon.Get(_name); | ||
var options = new MemoryPackSerializerOptions { StringEncoding = easyCachingOptions.StringEncoding }; | ||
return new DefaultMemoryPackSerializer(_name, options); | ||
}); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
serialization/EasyCaching.Serialization.MemoryPack/DefaultMemoryPackSerializer.cs
This file contains hidden or 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,33 @@ | ||
using EasyCaching.Core.Serialization; | ||
using MemoryPack; | ||
|
||
namespace EasyCaching.Serialization.MemoryPack; | ||
|
||
/// <summary> | ||
/// Default MemoryPack serializer | ||
/// </summary> | ||
public class DefaultMemoryPackSerializer : IEasyCachingSerializer | ||
{ | ||
private readonly string _name; | ||
private readonly MemoryPackSerializerOptions _memoryPackSerializerOptions; | ||
|
||
public string Name => _name; | ||
|
||
public DefaultMemoryPackSerializer(string name, MemoryPackSerializerOptions options = null) | ||
{ | ||
_name = name; | ||
_memoryPackSerializerOptions = options; | ||
} | ||
|
||
public T Deserialize<T>(byte[] bytes) => MemoryPackSerializer.Deserialize<T>(bytes, _memoryPackSerializerOptions); | ||
public object Deserialize(byte[] bytes, Type type) => MemoryPackSerializer.Deserialize(type, bytes, _memoryPackSerializerOptions); | ||
public object DeserializeObject(ArraySegment<byte> value) => throw new NotImplementedException("this is not supported in MemoryPack serializer"); | ||
public byte[] Serialize<T>(T value) => MemoryPackSerializer.Serialize(value, _memoryPackSerializerOptions); | ||
|
||
public ArraySegment<byte> SerializeObject(object obj) | ||
{ | ||
var bytes = MemoryPackSerializer.Serialize(obj.GetType(), obj, _memoryPackSerializerOptions); | ||
return new ArraySegment<byte>(bytes); | ||
} | ||
} | ||
|
13 changes: 13 additions & 0 deletions
13
...lization/EasyCaching.Serialization.MemoryPack/EasyCaching.Serialization.MemoryPack.csproj
This file contains hidden or 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,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\EasyCaching.Core\EasyCaching.Core.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="MemoryPack" Version="1.9.7" /> | ||
</ItemGroup> | ||
</Project> |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MemoryPack don't support this one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No it does not, it needs the type to be deserialized to. It can't handle dynamic or non memoryPackable objects.