Skip to content

Commit 9718850

Browse files
committed
add CacheIntercept Metadata to support routepath-based cache degradation
1 parent a943800 commit 9718850

File tree

7 files changed

+265
-16
lines changed

7 files changed

+265
-16
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Surging.Core.ProxyGenerator.Interceptors.Implementation.Metadatas
6+
{
7+
public enum CacheTargetType
8+
{
9+
Redis,
10+
CouchBase,
11+
Memcached,
12+
MemoryCache,
13+
}
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Surging.Core.ProxyGenerator.Interceptors.Implementation.Metadatas
6+
{
7+
public enum CachingMethod
8+
{
9+
Get,
10+
Put,
11+
Remove
12+
}
13+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using Surging.Core.CPlatform;
2+
using Surging.Core.CPlatform.Runtime.Server.Implementation.ServiceDiscovery.Attributes;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
7+
namespace Surging.Core.ProxyGenerator.Interceptors.Implementation.Metadatas
8+
{
9+
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
10+
public class ServiceCacheIntercept : ServiceDescriptorAttribute
11+
{
12+
#region 字段
13+
14+
#endregion
15+
16+
#region 构造函数
17+
/// <summary>
18+
/// 初始化一个新的<c>InterceptMethodAttribute</c>类型。
19+
/// </summary>
20+
/// <param name="method">缓存方式。</param>
21+
public ServiceCacheIntercept(CachingMethod method)
22+
{
23+
this.Method = method;
24+
}
25+
/// <summary>
26+
/// 初始化一个新的<c>InterceptMethodAttribute</c>类型。
27+
/// </summary>
28+
/// <param name="method">缓存方式。</param>
29+
/// <param name="correspondingMethodNames">与当前缓存方式相关的方法名称。注:此参数仅在缓存方式为Remove时起作用。</param>
30+
public ServiceCacheIntercept(CachingMethod method, params string[] correspondingMethodNames)
31+
: this(method)
32+
{
33+
this.CorrespondingKeys = correspondingMethodNames;
34+
}
35+
36+
internal ServiceCacheIntercept(string [] serviceInterceptItem)
37+
{
38+
39+
}
40+
#endregion
41+
42+
#region 公共属性
43+
/// <summary>
44+
/// 有效时间
45+
/// </summary>
46+
public int Time { get; set; } = 60;
47+
/// <summary>
48+
/// 采用什么进行缓存
49+
/// </summary>
50+
public CacheTargetType Mode { get; set; } = CacheTargetType.MemoryCache;
51+
52+
///// <summary>
53+
///// 设置SectionType
54+
///// </summary>
55+
public string CacheSectionType
56+
{
57+
get;
58+
set;
59+
}
60+
61+
public string L2Key
62+
{
63+
get; set;
64+
}
65+
66+
public bool EnableL2Cache
67+
{
68+
get; set;
69+
}
70+
71+
public string Key { get; set; }
72+
/// <summary>
73+
/// 获取或设置缓存方式。
74+
/// </summary>
75+
public CachingMethod Method { get; set; }
76+
77+
/// <summary>
78+
/// 获取或设置一个<see cref="Boolean"/>值,该值表示当缓存方式为Put时,是否强制将值写入缓存中。
79+
/// </summary>
80+
public bool Force { get; set; }
81+
/// <summary>
82+
/// 获取或设置与当前缓存方式相关的方法名称。注:此参数仅在缓存方式为Remove时起作用。
83+
/// </summary>
84+
public string[] CorrespondingKeys { get; set; }
85+
86+
public override void Apply(ServiceDescriptor descriptor)
87+
{
88+
descriptor.Key(Key)
89+
.L2Key(L2Key)
90+
.EnableL2Cache(EnableL2Cache)
91+
.Mode(Mode)
92+
.CacheSectionType(CacheSectionType)
93+
.Method(Method)
94+
.Force(Force)
95+
.CacheTime(Time)
96+
.CorrespondingKeys(CorrespondingKeys);
97+
}
98+
99+
#endregion
100+
}
101+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
using Surging.Core.CPlatform;
2+
using Surging.Core.ProxyGenerator.Interceptors.Implementation.Metadatas;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
using System.Linq;
7+
8+
namespace Surging.Core.ProxyGenerator.Interceptors.Implementation
9+
{
10+
public static class ServiceDescriptorExtensions
11+
{
12+
public static ServiceDescriptor CacheTime(this ServiceDescriptor descriptor,int time)
13+
{
14+
if(string.IsNullOrEmpty(descriptor.GetMetadata<string>("CacheIntercept")))
15+
descriptor.Metadatas["CacheIntercept"] = time;
16+
else
17+
descriptor.Metadatas["CacheIntercept"] += $"|{time}";
18+
19+
return descriptor;
20+
}
21+
22+
public static ServiceDescriptor Mode(this ServiceDescriptor descriptor, CacheTargetType cacheTargetType)
23+
{
24+
var targetType= Convert.ToInt32(cacheTargetType).ToString();
25+
if (string.IsNullOrEmpty(descriptor.GetMetadata<string>("CacheIntercept")))
26+
descriptor.Metadatas["CacheIntercept"] = targetType;
27+
else
28+
descriptor.Metadatas["CacheIntercept"] += $"|{targetType}";
29+
30+
return descriptor;
31+
}
32+
33+
public static ServiceDescriptor Method(this ServiceDescriptor descriptor, CachingMethod cachingMethod)
34+
{
35+
var iCachingMethod = Convert.ToInt32(cachingMethod).ToString();
36+
if (string.IsNullOrEmpty(descriptor.GetMetadata<string>("CacheIntercept")))
37+
descriptor.Metadatas["CacheIntercept"] = iCachingMethod;
38+
else
39+
descriptor.Metadatas["CacheIntercept"] += $"|{iCachingMethod}";
40+
41+
return descriptor;
42+
}
43+
44+
public static ServiceDescriptor CacheSectionType(this ServiceDescriptor descriptor, string cacheSectionType)
45+
{
46+
if (string.IsNullOrEmpty(descriptor.GetMetadata<string>("CacheIntercept")))
47+
descriptor.Metadatas["CacheIntercept"] = cacheSectionType;
48+
else
49+
descriptor.Metadatas["CacheIntercept"] += $"|{cacheSectionType}";
50+
51+
return descriptor;
52+
}
53+
54+
public static ServiceDescriptor Force(this ServiceDescriptor descriptor, bool Force)
55+
{
56+
var iForce = Convert.ToInt32(Force).ToString();
57+
if (string.IsNullOrEmpty(descriptor.GetMetadata<string>("CacheIntercept")))
58+
descriptor.Metadatas["CacheIntercept"] = iForce;
59+
else
60+
descriptor.Metadatas["CacheIntercept"] += $"|{iForce}";
61+
62+
return descriptor;
63+
}
64+
65+
public static ServiceDescriptor Key(this ServiceDescriptor descriptor, string key)
66+
{
67+
if (string.IsNullOrEmpty(descriptor.GetMetadata<string>("CacheIntercept")))
68+
descriptor.Metadatas["CacheIntercept"] = key;
69+
else
70+
descriptor.Metadatas["CacheIntercept"] += $"|{key}";
71+
72+
return descriptor;
73+
}
74+
75+
public static ServiceDescriptor L2Key(this ServiceDescriptor descriptor, string L2Key)
76+
{
77+
if (string.IsNullOrEmpty(descriptor.GetMetadata<string>("CacheIntercept")))
78+
descriptor.Metadatas["CacheIntercept"] = L2Key;
79+
else
80+
descriptor.Metadatas["CacheIntercept"] += $"|{L2Key}";
81+
82+
return descriptor;
83+
}
84+
85+
public static ServiceDescriptor EnableL2Cache(this ServiceDescriptor descriptor, bool enableL2Cache)
86+
{
87+
var iEnableL2Cache = Convert.ToInt32(enableL2Cache).ToString();
88+
if (string.IsNullOrEmpty(descriptor.GetMetadata<string>("CacheIntercept")))
89+
descriptor.Metadatas["CacheIntercept"] = iEnableL2Cache;
90+
else
91+
descriptor.Metadatas["CacheIntercept"] += $"|{iEnableL2Cache}";
92+
93+
return descriptor;
94+
}
95+
96+
public static ServiceDescriptor CorrespondingKeys(this ServiceDescriptor descriptor, string[] CorrespondingKeys)
97+
{
98+
if (CorrespondingKeys != null)
99+
{
100+
var correspondingKey = string.Join(",", CorrespondingKeys);
101+
if (string.IsNullOrEmpty(descriptor.GetMetadata<string>("CacheIntercept")))
102+
descriptor.Metadatas["CacheIntercept"] = correspondingKey;
103+
else
104+
descriptor.Metadatas["CacheIntercept"] += $"|{correspondingKey}";
105+
}
106+
else
107+
{
108+
descriptor.Metadatas["CacheIntercept"] += "|";
109+
}
110+
111+
return descriptor;
112+
}
113+
114+
public static ServiceCacheIntercept GetCacheIntercept(this ServiceDescriptor descriptor)
115+
{
116+
return new ServiceCacheIntercept( descriptor.GetMetadata("CacheIntercept", "").Split("|"));
117+
}
118+
}
119+
}

src/Surging.Core/Surging.Core.ProxyGenerator/ServiceProxyModule.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,20 @@ public override void Initialize(AppModuleContext context)
2424
serviceProvider.GetInstances<IServiceProxyFactory>();
2525
if (AppConfig.ServerOptions.ReloadOnChange)
2626
{
27-
new ServiceRouteWatch(serviceProvider,
28-
() =>
29-
{
30-
var builder = new ContainerBuilder();
31-
var result = serviceProvider.GetInstances<IServiceEngineBuilder>().ReBuild(builder);
32-
if (result != null)
33-
{
34-
builder.Update(serviceProvider.Current.ComponentRegistry);
35-
serviceProvider.GetInstances<IServiceEntryManager>().UpdateEntries(serviceProvider.GetInstances<IEnumerable<IServiceEntryProvider>>());
36-
// serviceProvider.GetInstances<IServiceProxyFactory>().RegisterProxType(result.Value.Item2.ToArray(), result.Value.Item1.ToArray());
37-
serviceProvider.GetInstances<IServiceRouteProvider>().RegisterRoutes(0);
38-
serviceProvider.GetInstances<IServiceProxyFactory>();
39-
}
40-
});
27+
//new ServiceRouteWatch(serviceProvider,
28+
// () =>
29+
// {
30+
// var builder = new ContainerBuilder();
31+
// var result = serviceProvider.GetInstances<IServiceEngineBuilder>().ReBuild(builder);
32+
// if (result != null)
33+
// {
34+
// builder.Update(serviceProvider.Current.ComponentRegistry);
35+
// serviceProvider.GetInstances<IServiceEntryManager>().UpdateEntries(serviceProvider.GetInstances<IEnumerable<IServiceEntryProvider>>());
36+
// // serviceProvider.GetInstances<IServiceProxyFactory>().RegisterProxType(result.Value.Item2.ToArray(), result.Value.Item1.ToArray());
37+
// serviceProvider.GetInstances<IServiceRouteProvider>().RegisterRoutes(0);
38+
// serviceProvider.GetInstances<IServiceProxyFactory>();
39+
// }
40+
// });
4141
}
4242
}
4343

src/Surging.IModuleServices/Surging.IModuleServices.Common/IUserService.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
using System.Collections.Generic;
1616
using System.ComponentModel.DataAnnotations;
1717
using System.Threading.Tasks;
18-
using Surging.Core.CPlatform.Validation;
18+
using Surging.Core.CPlatform.Validation;
19+
using Metadatas=Surging.Core.ProxyGenerator.Interceptors.Implementation.Metadatas;
1920

2021
namespace Surging.IModuleServices.Common
2122
{
@@ -67,6 +68,7 @@ public interface IUserService: IServiceKey
6768
[Authorization(AuthType = AuthorizationType.JWT)]
6869
[Command(Strategy = StrategyType.Injection, ShuntStrategy = AddressSelectorMode.HashAlgorithm, ExecutionTimeoutInMilliseconds = 1500, BreakerRequestVolumeThreshold = 3, Injection = @"return 1;", RequestCacheEnabled = false)]
6970
[InterceptMethod(CachingMethod.Get, Key = "GetUserId_{0}", CacheSectionType = SectionType.ddlCache, L2Key= "GetUserId_{0}", EnableL2Cache = true, Mode = CacheTargetType.Redis, Time = 480)]
71+
[Metadatas.ServiceCacheIntercept(Metadatas.CachingMethod.Get, Key = "GetUserId_{0}", CacheSectionType = "ddlCache", L2Key= "GetUserId_{0}", EnableL2Cache = true, Mode = Metadatas.CacheTargetType.Redis, Time = 480)]
7072
[ServiceRoute("{userName}")]
7173
Task<int> GetUserId(string userName);
7274

src/Surging.Services/Surging.Services.Server/surgingSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"Surging": {
33
"Ip": "${Surging_Server_IP}|127.0.0.1",
44
"WatchInterval": 30,
5-
"Port": "${Surging_Server_Port}|981",
5+
"Port": "${Surging_Server_Port}|98",
66
"MappingIp": "${Mapping_ip}",
77
"MappingPort": "${Mapping_Port}",
88
"Token": "true",

0 commit comments

Comments
 (0)