-
Notifications
You must be signed in to change notification settings - Fork 8
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 #73 from atidev/extend_serializers_support
Поддержать расширение common сторонними сериализаторами
- Loading branch information
Showing
28 changed files
with
1,416 additions
and
1,171 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
1 change: 0 additions & 1 deletion
1
ATI.Services.Common/Behaviors/OperationBuilder/BaseFunctionBuilder.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
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 |
---|---|---|
|
@@ -6,7 +6,6 @@ | |
using System.Net; | ||
using JetBrains.Annotations; | ||
|
||
|
||
namespace ATI.Services.Common.Behaviors | ||
{ | ||
[PublicAPI] | ||
|
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
ATI.Services.Common/Caching/Redis/Abstractions/IRedisSerializer.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 System; | ||
using StackExchange.Redis; | ||
|
||
namespace ATI.Services.Common.Caching.Redis.Abstractions; | ||
|
||
public interface IRedisSerializer | ||
{ | ||
public RedisValue Serialize<TIn>(TIn value); | ||
public RedisValue Serialize(object value, Type type); | ||
|
||
public T Deserialize<T>(RedisValue value); | ||
object Deserialize(RedisValue value, Type type); | ||
} |
22 changes: 22 additions & 0 deletions
22
ATI.Services.Common/Caching/Redis/Abstractions/RedisBinarySerializer.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,22 @@ | ||
using System; | ||
using ATI.Services.Common.Serializers; | ||
using StackExchange.Redis; | ||
|
||
namespace ATI.Services.Common.Caching.Redis.Abstractions; | ||
|
||
public sealed class RedisBinarySerializer : IRedisSerializer | ||
{ | ||
private readonly IBinarySerializer _serializer; | ||
|
||
public RedisBinarySerializer(IBinarySerializer serializer) | ||
{ | ||
_serializer = serializer; | ||
} | ||
|
||
public RedisValue Serialize<TIn>(TIn value) => _serializer.Serialize(value); | ||
|
||
public RedisValue Serialize(object value, Type type) => _serializer.Serialize(value, type); | ||
|
||
public T Deserialize<T>(RedisValue value) => _serializer.Deserialize<T>(value); | ||
public object Deserialize(RedisValue value, Type type) => _serializer.Deserialize(value, type); | ||
} |
22 changes: 22 additions & 0 deletions
22
ATI.Services.Common/Caching/Redis/Abstractions/RedisStringSerializer.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,22 @@ | ||
using System; | ||
using ATI.Services.Common.Serializers; | ||
using StackExchange.Redis; | ||
|
||
namespace ATI.Services.Common.Caching.Redis.Abstractions; | ||
|
||
public sealed class RedisStringSerializer : IRedisSerializer | ||
{ | ||
private readonly ISerializer<string> _serializer; | ||
|
||
public RedisStringSerializer(ISerializer serializer) | ||
{ | ||
_serializer = serializer; | ||
} | ||
|
||
public RedisValue Serialize<TIn>(TIn value) => _serializer.Serialize(value); | ||
|
||
public RedisValue Serialize(object value, Type type) => _serializer.Serialize(value, type); | ||
|
||
public T Deserialize<T>(RedisValue value) => _serializer.Deserialize<T>(value); | ||
public object Deserialize(RedisValue value, Type type) => _serializer.Deserialize(value, type); | ||
} |
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 |
---|---|---|
@@ -1,86 +1,83 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using ATI.Services.Common.Behaviors; | ||
using ATI.Services.Common.Caching.Redis.Abstractions; | ||
using ATI.Services.Common.Logging; | ||
using ATI.Services.Common.Serializers; | ||
using NLog; | ||
using Polly.CircuitBreaker; | ||
using Polly.Timeout; | ||
using Polly.Wrap; | ||
using StackExchange.Redis; | ||
|
||
namespace ATI.Services.Common.Caching.Redis | ||
namespace ATI.Services.Common.Caching.Redis; | ||
|
||
public abstract class BaseRedisCache | ||
{ | ||
public abstract class BaseRedisCache | ||
private static readonly ILogger Logger = LogManager.GetCurrentClassLogger(); | ||
protected IRedisSerializer Serializer; | ||
protected RedisOptions Options; | ||
|
||
protected async Task<OperationResult> ExecuteAsync( | ||
Func<Task> func, | ||
object context, | ||
AsyncCircuitBreakerPolicy circuitBreakerPolicy, | ||
AsyncPolicyWrap policy) | ||
{ | ||
private static readonly ILogger Logger = LogManager.GetCurrentClassLogger(); | ||
protected readonly ISerializer Serializer; | ||
try | ||
{ | ||
if (circuitBreakerPolicy.CircuitState == CircuitState.Open) | ||
{ | ||
return new OperationResult(ActionStatus.InternalOptionalServerUnavailable); | ||
} | ||
|
||
protected RedisOptions Options; | ||
await policy.ExecuteAsync(func); | ||
|
||
protected BaseRedisCache(ISerializer serializer) | ||
return OperationResult.Ok; | ||
} | ||
catch (TimeoutRejectedException ex) | ||
{ | ||
Serializer = serializer; | ||
Logger.ErrorWithObject(ex, | ||
new { DelegateName = func?.Method.Name, ReturnType = func?.Method.ReturnType.Name, Context = context }); | ||
return new OperationResult(ActionStatus.Timeout); | ||
} | ||
|
||
protected async Task<OperationResult> ExecuteAsync( | ||
Func<Task> func, | ||
object context, | ||
AsyncCircuitBreakerPolicy circuitBreakerPolicy, | ||
AsyncPolicyWrap policy) | ||
catch (Exception ex) | ||
{ | ||
try | ||
{ | ||
if (circuitBreakerPolicy.CircuitState == CircuitState.Open) | ||
{ | ||
return new OperationResult(ActionStatus.InternalOptionalServerUnavailable); | ||
} | ||
|
||
await policy.ExecuteAsync(func); | ||
|
||
return OperationResult.Ok; | ||
} | ||
catch (TimeoutRejectedException ex) | ||
{ | ||
Logger.ErrorWithObject(ex, new { DelegateName = func?.Method.Name, ReturnType = func?.Method.ReturnType.Name, Context = context }); | ||
return new OperationResult(ActionStatus.Timeout); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Logger.ErrorWithObject(ex, new { DelegateName = func?.Method.Name, ReturnType = func?.Method.ReturnType.Name, Context = context }); | ||
return new OperationResult(ActionStatus.InternalOptionalServerUnavailable); | ||
} | ||
Logger.ErrorWithObject(ex, | ||
new { DelegateName = func?.Method.Name, ReturnType = func?.Method.ReturnType.Name, Context = context }); | ||
return new OperationResult(ActionStatus.InternalOptionalServerUnavailable); | ||
} | ||
} | ||
|
||
protected async Task<OperationResult<T>> ExecuteAsync<T>( | ||
Func<Task<T>> func, | ||
object context, | ||
AsyncCircuitBreakerPolicy circuitBreakerPolicy, | ||
AsyncPolicyWrap policy) | ||
protected async Task<OperationResult<T>> ExecuteAsync<T>( | ||
Func<Task<T>> func, | ||
object context, | ||
AsyncCircuitBreakerPolicy circuitBreakerPolicy, | ||
AsyncPolicyWrap policy) | ||
{ | ||
try | ||
{ | ||
try | ||
{ | ||
if (circuitBreakerPolicy.CircuitState == CircuitState.Open) | ||
{ | ||
return new OperationResult<T>(ActionStatus.InternalOptionalServerUnavailable); | ||
} | ||
|
||
var result = await policy.ExecuteAsync(func); | ||
|
||
return result == null || result is RedisValue resultAsRedisValue && resultAsRedisValue.IsNull | ||
? new OperationResult<T>(ActionStatus.NotFound) | ||
: new OperationResult<T>(result); | ||
} | ||
catch (TimeoutRejectedException ex) | ||
if (circuitBreakerPolicy.CircuitState == CircuitState.Open) | ||
{ | ||
Logger.ErrorWithObject(ex, new { DelegateName = func?.Method.Name, ReturnType = func?.Method.ReturnType.Name, Context = context }); | ||
return new OperationResult<T>(ActionStatus.Timeout); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Logger.ErrorWithObject(ex, new { DelegateName = func?.Method.Name, ReturnType = func?.Method.ReturnType.Name, Context = context }); | ||
return new OperationResult<T>(ActionStatus.InternalOptionalServerUnavailable); | ||
} | ||
|
||
var result = await policy.ExecuteAsync(func); | ||
|
||
return result == null || result is RedisValue resultAsRedisValue && resultAsRedisValue.IsNull | ||
? new OperationResult<T>(ActionStatus.NotFound) | ||
: new OperationResult<T>(result); | ||
} | ||
catch (TimeoutRejectedException ex) | ||
{ | ||
Logger.ErrorWithObject(ex, | ||
new { DelegateName = func?.Method.Name, ReturnType = func?.Method.ReturnType.Name, Context = context }); | ||
return new OperationResult<T>(ActionStatus.Timeout); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Logger.ErrorWithObject(ex, | ||
new { DelegateName = func?.Method.Name, ReturnType = func?.Method.ReturnType.Name, Context = context }); | ||
return new OperationResult<T>(ActionStatus.InternalOptionalServerUnavailable); | ||
} | ||
} | ||
} |
Oops, something went wrong.