Skip to content

Commit

Permalink
添加EasyCaching 2级缓存配置
Browse files Browse the repository at this point in the history
  • Loading branch information
UtilCore committed Nov 19, 2023
1 parent 0a2dbff commit 91b79ec
Show file tree
Hide file tree
Showing 17 changed files with 398 additions and 131 deletions.
2 changes: 1 addition & 1 deletion build/version.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<VersionMajor>7</VersionMajor>
<VersionMinor>1</VersionMinor>
<VersionPatch>78</VersionPatch>
<VersionPatch>80</VersionPatch>
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
Expand Down
56 changes: 42 additions & 14 deletions src/Util.AspNetCore/Http/HttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Util.Helpers;
using Util.SystemTextJson;

namespace Util.Http;
namespace Util.Http;

/// <summary>
/// Http请求
Expand Down Expand Up @@ -38,6 +38,10 @@ public class HttpRequest<TResult> : IHttpRequest<TResult> where TResult : class
/// Json序列化配置
/// </summary>
private JsonSerializerOptions _jsonSerializerOptions;
/// <summary>
/// 是否忽略SSL证书
/// </summary>
private bool _ignoreSsl;

#endregion

Expand Down Expand Up @@ -235,6 +239,16 @@ public IHttpRequest<TResult> Certificate( string path, string password ) {

#endregion

#region IgnoreSsl(是否忽略SSL证书)

/// <inheritdoc />
public IHttpRequest<TResult> IgnoreSsl() {
_ignoreSsl = true;
return this;
}

#endregion

#region JsonSerializerOptions(设置Json序列化配置)

/// <inheritdoc />
Expand All @@ -251,7 +265,7 @@ public IHttpRequest<TResult> JsonSerializerOptions( JsonSerializerOptions option
/// 获取Json序列化配置
/// </summary>
protected virtual JsonSerializerOptions GetJsonSerializerOptions() {
if ( _jsonSerializerOptions != null )
if( _jsonSerializerOptions != null )
return _jsonSerializerOptions;
return new JsonSerializerOptions {
PropertyNameCaseInsensitive = true,
Expand Down Expand Up @@ -338,19 +352,19 @@ public IHttpRequest<TResult> UseCookies( bool isUseCookies ) {

/// <inheritdoc />
public IHttpRequest<TResult> Cookie( string key, string value ) {
if ( key.IsEmpty() )
if( key.IsEmpty() )
return this;
if ( Cookies.ContainsKey( key ) )
if( Cookies.ContainsKey( key ) )
Cookies.Remove( key );
Cookies.Add( key, value );
return this;
}

/// <inheritdoc />
public IHttpRequest<TResult> Cookie( IDictionary<string, string> cookies ) {
if ( cookies == null )
if( cookies == null )
return this;
foreach ( var cookie in cookies )
foreach( var cookie in cookies )
Cookie( cookie.Key, cookie.Value );
return this;
}
Expand Down Expand Up @@ -515,10 +529,10 @@ protected virtual string GetUrl( string url ) {
/// 添加Cookie
/// </summary>
protected virtual void AddCookies() {
if ( Cookies.Count == 0 )
if( Cookies.Count == 0 )
return;
var cookieValues = new List<CookieHeaderValue>();
foreach ( var cookie in Cookies )
foreach( var cookie in Cookies )
cookieValues.Add( new CookieHeaderValue( cookie.Key, cookie.Value ) );
Header( "Cookie", cookieValues.Select( t => t.ToString() ).Join() );
}
Expand Down Expand Up @@ -662,7 +676,7 @@ protected HttpClient GetClient() {
protected HttpClientHandler CreateHttpClientHandler() {
var handlerFactory = _httpClientFactory as IHttpMessageHandlerFactory;
var handler = handlerFactory?.CreateHandler();
while ( handler is DelegatingHandler delegatingHandler ) {
while( handler is DelegatingHandler delegatingHandler ) {
handler = delegatingHandler.InnerHandler;
}
return handler as HttpClientHandler;
Expand All @@ -673,10 +687,11 @@ protected HttpClientHandler CreateHttpClientHandler() {
/// </summary>
/// <param name="handler">Http客户端处理器</param>
protected virtual void InitHttpClientHandler( HttpClientHandler handler ) {
if ( handler == null )
if( handler == null )
return;
InitCertificate( handler );
InitUseCookies( handler );
IgnoreSsl( handler );
}

#endregion
Expand All @@ -702,12 +717,25 @@ protected virtual void InitCertificate( HttpClientHandler handler ) {
/// 初始化是否携带Cookie
/// </summary>
protected virtual void InitUseCookies( HttpClientHandler handler ) {
if ( handler.UseCookies != IsUseCookies )
if( handler.UseCookies != IsUseCookies )
handler.UseCookies = IsUseCookies;
}

#endregion

#region IgnoreSsl(忽略SSL证书错误)

/// <summary>
/// 忽略SSL证书错误
/// </summary>
protected virtual void IgnoreSsl( HttpClientHandler handler ) {
if( _ignoreSsl == false )
return;
handler.ServerCertificateCustomValidationCallback ??= ( _, _, _, _ ) => true;
}

#endregion

#region InitHttpClient(初始化Http客户端)

/// <summary>
Expand All @@ -726,7 +754,7 @@ protected virtual void InitHttpClient( HttpClient client ) {
/// 初始化基地址
/// </summary>
protected virtual void InitBaseAddress( HttpClient client ) {
if ( BaseAddressUri.IsEmpty() )
if( BaseAddressUri.IsEmpty() )
return;
client.BaseAddress = new Uri( BaseAddressUri );
}
Expand All @@ -739,7 +767,7 @@ protected virtual void InitBaseAddress( HttpClient client ) {
/// 初始化超时间隔
/// </summary>
protected virtual void InitTimeout( HttpClient client ) {
if ( HttpTimeout == null )
if( HttpTimeout == null )
return;
client.Timeout = HttpTimeout.SafeValue();
}
Expand Down Expand Up @@ -778,7 +806,7 @@ protected virtual async Task<TResult> SendAfterAsync( HttpResponseMessage respon
protected virtual async Task<TResult> SuccessHandlerAsync( HttpResponseMessage response, string content ) {
var result = ConvertTo( content, response.GetContentType() );
SuccessAction?.Invoke( result );
if ( SuccessFunc != null )
if( SuccessFunc != null )
await SuccessFunc( result );
return result;
}
Expand Down
4 changes: 4 additions & 0 deletions src/Util.AspNetCore/Http/IHttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public interface IHttpRequest<TResult> : IHttpRequest where TResult : class {
/// <param name="password">证书密码</param>
IHttpRequest<TResult> Certificate( string path, string password );
/// <summary>
/// 忽略SSL证书
/// </summary>
IHttpRequest<TResult> IgnoreSsl();
/// <summary>
/// 设置Json序列化配置
/// </summary>
/// <param name="options">Json序列化配置</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="EasyCaching.Bus.Redis" Version="1.9.2" />
<PackageReference Include="EasyCaching.HybridCache" Version="1.9.2" />
<PackageReference Include="EasyCaching.InMemory" Version="1.9.2" />
<PackageReference Include="EasyCaching.Redis" Version="1.9.2" />
<PackageReference Include="EasyCaching.Serialization.SystemTextJson" Version="1.9.2" />
Expand Down
Loading

0 comments on commit 91b79ec

Please sign in to comment.