Skip to content

Commit

Permalink
ServiceCacheIntercept Metadata adds support for the "Enable StageCach…
Browse files Browse the repository at this point in the history
…e" option to support stageGateway, which supports single parameter and no parameter cache interception and degradation, as well as multi language microservices cache interception and degradation
  • Loading branch information
fanliang11 committed Apr 27, 2024
1 parent 3be8fc0 commit 23dabab
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@
@Html.TextBoxFor(p => p.ExecutionTimeoutInMilliseconds, new { autocomplete = "off", placeholder = "超时时间", @class = "form-control" })
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label no-padding-right"><span class="red">*</span>开启缓存</label>
<div class="col-sm-9 control-sm">
@Html.RadioButtonFor(p => p.RequestCacheEnabled, true, new { name = "RequestCacheEnabled" }) <label>是</label>
@Html.RadioButtonFor(p => p.RequestCacheEnabled, false, new { name = "RequestCacheEnabled" }) <label>否</label>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label no-padding-right"><span class="red">*</span>错误率</label>
<div class="col-sm-9">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
<th class="center" width="6%">强制熔断</th>
<th class="center" width="6%">容错策略</th>
<th class="center" width="6%">超时时间</th>
<th class="center" width="6%">开启缓存</th>
<th class="center" width="9%">回退实例名</th>
<th class="center" width="9%">注入脚本</th>
<th class="center" width="9%">命名空间</th>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,7 @@
<td class="center">
${Strategy}
</td>
<td class="center">${ExecutionTimeoutInMilliseconds}</td>
<td class="center">
{{if RequestCacheEnabled==true }}
<span class="label label-success arrowed-in arrowed-in-right">是</span>
{{else}}
<span class="label label-danger arrowed">否</span>
{{/if}}
</td>
<td class="center">${ExecutionTimeoutInMilliseconds}</td>
<td class="center">${FallBackName}</td>
<td class="center">${Injection}</td>
<td class="center">${InjectionNamespaces}</td>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Surging.Apm.Skywalking.Abstractions;
using Microsoft.AspNetCore.Hosting;
using Surging.Core.CPlatform.Utilities;

namespace Surging.Apm.Skywalking.Core.Common
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ public static IServiceBuilder RegisterRepositories(this IServiceBuilder builder,
return builder;
}

/// <summary>
/// <summary>
/// 依赖注入组件模块程序集
/// </summary>
/// <param name="builder"></param>
Expand All @@ -654,19 +654,19 @@ public static IServiceBuilder RegisterModules(this IServiceBuilder builder, para
{
GetAbstractModules(moduleAssembly).ForEach(p =>
{
if (!_modules.Any(m => p.ModuleName == m.ModuleName))
{
services.RegisterModule(p);
if (packages.ContainsKey(p.TypeName))
{
var useModules = packages[p.TypeName];
if (useModules.AsSpan().IndexOf(p.ModuleName) >= 0)
p.Enable = true;
else
p.Enable = false;
}
_modules.Add(p);
}
if (!_modules.Any(m => p.ModuleName == m.ModuleName))
{
services.RegisterModule(p);
if (packages.ContainsKey(p.TypeName))
{
var useModules = packages[p.TypeName];
if (useModules.AsSpan().IndexOf(p.ModuleName) >= 0)
p.Enable = true;
else
p.Enable = false;
}
_modules.Add(p);
}
});
}
builder.Services.Register(provider => new ModuleProvider(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public ServiceCommand()
/// <summary>
/// 是否开启缓存
/// </summary>
[JsonIgnore]
public bool RequestCacheEnabled { get; set; }
/// <summary>
/// 注入
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ protected async Task<T> Invoke<T>(IDictionary<string, object> parameters, string
var decodeJOject = typeof(T) == UtilityType.ObjectType;
IInvocation invocation = null;
var serviceRoute =await _serviceRouteProvider.Locate(serviceId);
if ((serviceRoute ==null || !serviceRoute.ServiceDescriptor.ExistIntercept()) ||decodeJOject)
if ((serviceRoute == null || !serviceRoute.ServiceDescriptor.ExistIntercept()) ||
(!serviceRoute.ServiceDescriptor.GetCacheIntercept("Cache").EnableStageCache && decodeJOject))
{
message = await _breakeRemoteInvokeService.InvokeAsync(parameters, serviceId, _serviceKey, decodeJOject);
if (message == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,24 @@ public ServiceCacheIntercept(CachingMethod method, params string[] corresponding
this.CorrespondingKeys = correspondingMethodNames;
}

internal ServiceCacheIntercept(string [] serviceInterceptItem)
internal ServiceCacheIntercept(string[] serviceInterceptItem)
{
Key = serviceInterceptItem[0];
L2Key= serviceInterceptItem[1];
EnableL2Cache = serviceInterceptItem[2] == "1" ? true : false ;
Enum.TryParse<CacheTargetType>(serviceInterceptItem[3],out CacheTargetType mode);
L2Key = serviceInterceptItem[1];
EnableL2Cache = serviceInterceptItem[2] == "1" ? true : false;
Enum.TryParse<CacheTargetType>(serviceInterceptItem[3], out CacheTargetType mode);
Mode = mode;
CacheSectionType = serviceInterceptItem[4];
Enum.TryParse<CachingMethod>(serviceInterceptItem[5], out CachingMethod method);
Enum.TryParse<CachingMethod>(serviceInterceptItem[5], out CachingMethod method);
Method = method;
Force= serviceInterceptItem[6]== "1" ? true : false; ;
Force = serviceInterceptItem[6] == "1" ? true : false; ;
Time = Convert.ToInt32(serviceInterceptItem[7]);
if(!string.IsNullOrEmpty(serviceInterceptItem[8]))
if (!string.IsNullOrEmpty(serviceInterceptItem[8]))
{
CorrespondingKeys = serviceInterceptItem[8].Split(",");
}
if (serviceInterceptItem.Length > 9)
EnableStageCache = serviceInterceptItem[9] == "1" ? true : false;
}
#endregion

Expand All @@ -71,10 +73,10 @@ public string CacheSectionType
set;
} = "";

public string L2Key
public string L2Key
{
get; set;
}= "";
} = "";

public bool EnableL2Cache
{
Expand All @@ -85,12 +87,14 @@ public bool EnableL2Cache
/// <summary>
/// 获取或设置缓存方式。
/// </summary>
public CachingMethod Method { get; set; }
public CachingMethod Method { get; set; }

/// <summary>
/// 获取或设置一个<see cref="Boolean"/>值,该值表示当缓存方式为Put时,是否强制将值写入缓存中。
/// </summary>
public bool Force { get; set; }

public bool EnableStageCache { get; set; } = AppConfig.ServerOptions.RequestCacheEnabled;
/// <summary>
/// 获取或设置与当前缓存方式相关的方法名称。注:此参数仅在缓存方式为Remove时起作用。
/// </summary>
Expand All @@ -107,7 +111,8 @@ public override void Apply(ServiceDescriptor descriptor)
.Method(Method, MetadataId)
.Force(Force, MetadataId)
.CacheTime(Time, MetadataId)
.CorrespondingKeys(CorrespondingKeys, MetadataId);
.CorrespondingKeys(CorrespondingKeys, MetadataId)
.EnableStageCache(EnableStageCache, MetadataId);
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,20 @@ public static ServiceDescriptor CorrespondingKeys(this ServiceDescriptor descrip
descriptor.Metadatas["Intercept"] = metadata.Item2;
return descriptor;
}


public static ServiceDescriptor EnableStageCache(this ServiceDescriptor descriptor, bool enableStageCache, string metadataId)
{
var metadata = GetInterceptMetadata(descriptor, metadataId);
var iEnableL2Cache = Convert.ToInt32(enableStageCache).ToString();
if (string.IsNullOrEmpty(metadata.Item1))
metadata.Item1 = iEnableL2Cache;
else
metadata.Item1 += $"|{iEnableL2Cache}";
metadata.Item2[metadataId] = metadata.Item1;
descriptor.Metadatas["Intercept"] = metadata.Item2;
return descriptor;
}


public static ServiceCacheIntercept GetCacheIntercept(this ServiceDescriptor descriptor,string metadataId)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public interface IUserService: IServiceKey
[Authorization(AuthType = AuthorizationType.JWT)]
[Command(Strategy = StrategyType.Injection, ShuntStrategy = AddressSelectorMode.HashAlgorithm, ExecutionTimeoutInMilliseconds = 1500, BreakerRequestVolumeThreshold = 3, Injection = @"return 1;", RequestCacheEnabled = false)]
[InterceptMethod(CachingMethod.Get, Key = "GetUserId_{0}", CacheSectionType = SectionType.ddlCache, L2Key= "GetUserId_{0}", EnableL2Cache = true, Mode = CacheTargetType.Redis, Time = 480)]
[Metadatas.ServiceCacheIntercept(Metadatas.CachingMethod.Get, Key = "GetUserId_{0}", CacheSectionType = "ddlCache", L2Key= "GetUserId_{0}", EnableL2Cache = true, Mode = Metadatas.CacheTargetType.Redis, Time = 480)]
[Metadatas.ServiceCacheIntercept(Metadatas.CachingMethod.Get, Key = "GetUserId_{0}", CacheSectionType = "ddlCache", L2Key= "GetUserId_{0}", EnableL2Cache = true, Mode = Metadatas.CacheTargetType.Redis, Time = 480,EnableStageCache =true)]
[Metadatas.ServiceLogIntercept()]
[ServiceRoute("{userName}")]
Task<int> GetUserId(string userName);
Expand Down Expand Up @@ -121,7 +121,7 @@ public interface IUserService: IServiceKey
/// 测试无参数调用
/// </summary>
/// <returns>返回是否成功</returns>

[Metadatas.ServiceCacheIntercept(Metadatas.CachingMethod.Get, Key = "GetDictionary", L2Key = "GetDictionary", EnableL2Cache = true, CacheSectionType = "ddlCache", Mode = Metadatas.CacheTargetType.Redis, Time = 480, EnableStageCache = true)]
Task<bool> GetDictionary();

/// <summary>
Expand Down

0 comments on commit 23dabab

Please sign in to comment.