Skip to content
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

Bugfix #537 issue and Correct namespace and StringEncoding default value #538

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ namespace EasyCaching.Serialization.MemoryPack;
/// </summary>
public record EasyCachingMemPackSerializerOptions
{
public StringEncoding StringEncoding { set; get; }
/// <summary>
/// Gets or sets the string encoding. (Defaults to <see cref="StringEncoding.Utf8"/>)
/// </summary>
/// <value>
/// The string encoding.
/// </value>
public StringEncoding StringEncoding { set; get; } = StringEncoding.Utf8;
}


Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using MemoryPack;
using EasyCaching.Core.Configurations;
using EasyCaching.Serialization.Json;
using EasyCaching.Core.Configurations;
using EasyCaching.Serialization.MemoryPack;

namespace EasyCaching.Serialization.MemoryPack;
namespace Microsoft.Extensions.DependencyInjection;

/// <summary>
/// Easy caching options extensions.
Expand All @@ -13,7 +12,7 @@ public static class EasyCachingOptionsExtensions
/// Withs the memory pack serializer.
/// </summary>
/// <param name="options">Options.</param>
/// <param name="name">The name of this serializer instance.</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));
Expand All @@ -23,10 +22,10 @@ public static EasyCachingOptions WithMemoryPack(this EasyCachingOptions options,

/// <summary>
/// Withs the memory pack serializer.
/// </summary>
/// </summary>
/// <param name="options">Options.</param>
/// <param name="serializerOptions">Configure serializer settings.</param>
/// <param name="name">The name of this serializer instance.</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));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
namespace EasyCaching.Serialization.Json;

using System;
using EasyCaching.Core.Configurations;
using EasyCaching.Core.Serialization;
using EasyCaching.Serialization.MemoryPack;
using global::MemoryPack;
using MemoryPack;
using Microsoft.Extensions.DependencyInjection;

namespace EasyCaching.Serialization.MemoryPack;

/// <summary>
/// MemoryPack options extension.
/// </summary>
Expand All @@ -29,8 +27,8 @@ internal sealed class MemoryPackOptionsExtension : IEasyCachingOptionsExtension
/// <param name="configure">Configure.</param>
public MemoryPackOptionsExtension(string name, Action<EasyCachingMemPackSerializerOptions> configure)
{
this._name = name;
this._configure = configure;
_name = name;
_configure = configure;
}

/// <summary>
Expand All @@ -44,11 +42,16 @@ public void AddServices(IServiceCollection services)
services.AddOptions();
services.Configure(_name, configure);

services.AddSingleton<IEasyCachingSerializer, DefaultMemoryPackSerializer>(x =>
services.AddSingleton<IEasyCachingSerializer, DefaultMemoryPackSerializer>(services =>
{
var optionsMon = x.GetRequiredService<Microsoft.Extensions.Options.IOptionsMonitor<EasyCachingMemPackSerializerOptions>>();
var optionsMon = services.GetRequiredService<Microsoft.Extensions.Options.IOptionsMonitor<EasyCachingMemPackSerializerOptions>>();
var easyCachingOptions = optionsMon.Get(_name);
var options = new MemoryPackSerializerOptions { StringEncoding = easyCachingOptions.StringEncoding };

var options = MemoryPackSerializerOptions.Default;
typeof(MemoryPackSerializerOptions)
.GetProperty(nameof(MemoryPackSerializerOptions.StringEncoding))
.SetValue(options, easyCachingOptions.StringEncoding);

return new DefaultMemoryPackSerializer(_name, options);
});
}
Expand Down
Loading