Skip to content

Commit

Permalink
Bugfix #537 issue and Correct namespace and StringEncoding default va…
Browse files Browse the repository at this point in the history
…lue (#538)
  • Loading branch information
mjebrahimi authored Sep 18, 2024
1 parent adfa52c commit 04b17e8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
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

0 comments on commit 04b17e8

Please sign in to comment.