-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* MemoryPack serializer * some cleanups Co-authored-by: amirsolhi <[email protected]>
- Loading branch information
Showing
12 changed files
with
338 additions
and
44 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
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
13 changes: 13 additions & 0 deletions
13
...asyCaching.Serialization.MemoryPack/Configurations/EasyCachingMemPackSerializerOptions.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,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 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 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 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 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 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.