|
| 1 | +surging is based on .net core language high-performance distributed microservices framework, |
| 2 | +Clients can enable caching to intercept client calls to the server,The following example enables caching |
| 3 | + |
| 4 | +Code Examples |
| 5 | +============= |
| 6 | + |
| 7 | +Create an Interceptor that inherits CacheInterceptor: |
| 8 | +```c# |
| 9 | + public class CacheProviderInterceptor : CacheInterceptor |
| 10 | + { |
| 11 | + public override async Task Intercept(IInvocation invocation) |
| 12 | + { |
| 13 | + var attribute = |
| 14 | + invocation.Attributes.Where(p => p is InterceptMethodAttribute) |
| 15 | + .Select(p => p as InterceptMethodAttribute).FirstOrDefault(); |
| 16 | + var cacheKey = invocation.CacheKey == null ? attribute.Key : |
| 17 | + string.Format(attribute.Key ?? "", invocation.CacheKey); |
| 18 | + await CacheIntercept(attribute, cacheKey, invocation); |
| 19 | + } |
| 20 | + |
| 21 | + private async Task CacheIntercept(InterceptMethodAttribute attribute, string key, IInvocation invocation) |
| 22 | + { |
| 23 | + ICacheProvider cacheProvider = null; |
| 24 | + switch (attribute.Mode) |
| 25 | + { |
| 26 | + case CacheTargetType.Redis: |
| 27 | + { |
| 28 | + cacheProvider = CacheContainer.GetService<ICacheProvider>(string.Format("{0}.{1}", |
| 29 | + attribute.CacheSectionType.ToString(), CacheTargetType.Redis.ToString())); |
| 30 | + break; |
| 31 | + } |
| 32 | + case CacheTargetType.MemoryCache: |
| 33 | + { |
| 34 | + cacheProvider = CacheContainer.GetService<ICacheProvider>(CacheTargetType.MemoryCache.ToString()); |
| 35 | + break; |
| 36 | + } |
| 37 | + } |
| 38 | + if (cacheProvider != null) await Invoke(cacheProvider, attribute, key, invocation); |
| 39 | + } |
| 40 | + |
| 41 | + private async Task Invoke(ICacheProvider cacheProvider, InterceptMethodAttribute attribute, string key, IInvocation invocation) |
| 42 | + { |
| 43 | + switch (attribute.Method) |
| 44 | + { |
| 45 | + case CachingMethod.Get: |
| 46 | + { |
| 47 | + var retrunValue = await cacheProvider.GetFromCacheFirst(key, async () => |
| 48 | + { |
| 49 | + await invocation.Proceed(); |
| 50 | + return invocation.ReturnValue; |
| 51 | + }, invocation.ReturnType, attribute.Time); |
| 52 | + invocation.ReturnValue = retrunValue; |
| 53 | + break; |
| 54 | + } |
| 55 | + default: |
| 56 | + { |
| 57 | + await invocation.Proceed(); |
| 58 | + var keys = attribute.CorrespondingKeys.Select(correspondingKey => string.Format(correspondingKey, invocation.CacheKey)).ToList(); |
| 59 | + keys.ForEach(cacheProvider.RemoveAsync); |
| 60 | + break; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | +``` |
| 66 | + |
| 67 | +Configure the interception code as follows: |
| 68 | +```c# |
| 69 | +builder.AddMicroService(option => |
| 70 | + { |
| 71 | + option.AddClient(); |
| 72 | + option.AddClientIntercepted(typeof(CacheProviderInterceptor));//Configure Interceptor |
| 73 | + //option.UseZooKeeperManager(new ConfigInfo("127.0.0.1:2181")); |
| 74 | + option.UseConsulManager(new ConfigInfo("127.0.0.1:8500")); |
| 75 | + option.UseDotNettyTransport(); |
| 76 | + option.UseRabbitMQTransport(); |
| 77 | + option.AddCache(); |
| 78 | + //option.UseKafkaMQTransport(kafkaOption => |
| 79 | + //{ |
| 80 | + // kafkaOption.Servers = "127.0.0.1"; |
| 81 | + //}); |
| 82 | + //option.UseProtoBufferCodec(); |
| 83 | + option.UseMessagePackCodec(); |
| 84 | + builder.Register(p => new CPlatformContainer(ServiceLocator.Current)); |
| 85 | + }); |
| 86 | +``` |
| 87 | +## Next steps |
0 commit comments