此次的目标是实现经过标注Attribute实现缓存的功能,精简代码,减小缓存的代码侵入业务代码。html
缓存内容即为Service查询汇总的内容,不作其余高大上的功能,提高短期屡次查询的响应速度,适当减轻数据库压力。git
在作以前,也去看了EasyCaching的源码,此次的想法也是源于这里,AOP的方式让代码减小耦合,可是缓存策略有限。通过考虑决定,本身实现相似功能,在以后的应用中也方便对缓存策略的扩展。github
本文内容也许有点不严谨的地方,仅供参考。一样欢迎各位路过的大佬提出建议。redis
以前有作AspectCore的总结,相关内容就再也不赘述了。shell
在stackexchange.Redis和CSRedis中纠结了好久,也没有一个特别的有优点,最终选择了stackexchange.Redis,没有理由。至于链接超时的问题,能够用异步解决。数据库
Install-Package StackExchange.Redis -Version 2.0.601
{ "Redis": { "Default": { "Connection": "127.0.0.1:6379", "InstanceName": "RedisCache:", "DefaultDB": 0 } } }
用于链接Redis服务器,包括建立链接,获取数据库等操做json
public class RedisClient : IDisposable { private string _connectionString; private string _instanceName; private int _defaultDB; private ConcurrentDictionary<string, ConnectionMultiplexer> _connections; public RedisClient(string connectionString, string instanceName, int defaultDB = 0) { _connectionString = connectionString; _instanceName = instanceName; _defaultDB = defaultDB; _connections = new ConcurrentDictionary<string, ConnectionMultiplexer>(); } private ConnectionMultiplexer GetConnect() { return _connections.GetOrAdd(_instanceName, p => ConnectionMultiplexer.Connect(_connectionString)); } public IDatabase GetDatabase() { return GetConnect().GetDatabase(_defaultDB); } public IServer GetServer(string configName = null, int endPointsIndex = 0) { var confOption = ConfigurationOptions.Parse(_connectionString); return GetConnect().GetServer(confOption.EndPoints[endPointsIndex]); } public ISubscriber GetSubscriber(string configName = null) { return GetConnect().GetSubscriber(); } public void Dispose() { if (_connections != null && _connections.Count > 0) { foreach (var item in _connections.Values) { item.Close(); } } } }
Redis是单线程的服务,多几个RedisClient的实例也是无济于事,因此依赖注入就采用singleton的方式。c#
public static class RedisExtensions { public static void ConfigRedis(this IServiceCollection services, IConfiguration configuration) { var section = configuration.GetSection("Redis:Default"); string _connectionString = section.GetSection("Connection").Value; string _instanceName = section.GetSection("InstanceName").Value; int _defaultDB = int.Parse(section.GetSection("DefaultDB").Value ?? "0"); services.AddSingleton(new RedisClient(_connectionString, _instanceName, _defaultDB)); } } public class Startup { public void ConfigureServices(IServiceCollection services) { services.ConfigRedis(Configuration); } }
建立一个缓存Key的生成器,以Attribute中的CacheKeyPrefix做为前缀,以后能够扩展批量删除的功能。被拦截方法的方法名和入参也一样做为key的一部分,保证Key值不重复。缓存
public static class KeyGenerator { public static string GetCacheKey(MethodInfo methodInfo, object[] args, string prefix) { StringBuilder cacheKey = new StringBuilder(); cacheKey.Append($"{prefix}_"); cacheKey.Append(methodInfo.DeclaringType.Name).Append($"_{methodInfo.Name}"); foreach (var item in args) { cacheKey.Append($"_{item}"); } return cacheKey.ToString(); } public static string GetCacheKeyPrefix(MethodInfo methodInfo, string prefix) { StringBuilder cacheKey = new StringBuilder(); cacheKey.Append(prefix); cacheKey.Append($"_{methodInfo.DeclaringType.Name}").Append($"_{methodInfo.Name}"); return cacheKey.ToString(); } }
Attribute中保存缓存的策略信息,包括过时时间,Key值前缀等信息,在使用缓存时能够对这些选项值进行配置。服务器
public class CacheAbleAttribute : Attribute { /// <summary> /// 过时时间(秒) /// </summary> public int Expiration { get; set; } = 300; /// <summary> /// Key值前缀 /// </summary> public string CacheKeyPrefix { get; set; } = string.Empty; /// <summary> /// 是否高可用(异常时执行原方法) /// </summary> public bool IsHighAvailability { get; set; } = true; /// <summary> /// 只容许一个线程更新缓存(带锁) /// </summary> public bool OnceUpdate { get; set; } = false; }
接下来就是重头戏,拦截器中的逻辑就相对于缓存的相关策略,不用的策略能够分红不一样的拦截器。
这里的逻辑参考了EasyCaching的源码,并加入了Redis分布式锁的应用。
public class CacheAbleInterceptor : AbstractInterceptor { [FromContainer] private RedisClient RedisClient { get; set; } private IDatabase Database; private static readonly ConcurrentDictionary<Type, MethodInfo> TypeofTaskResultMethod = new ConcurrentDictionary<Type, MethodInfo>(); public async override Task Invoke(AspectContext context, AspectDelegate next) { CacheAbleAttribute attribute = context.GetAttribute<CacheAbleAttribute>(); if (attribute == null) { await context.Invoke(next); return; } try { Database = RedisClient.GetDatabase(); string cacheKey = KeyGenerator.GetCacheKey(context.ServiceMethod, context.Parameters, attribute.CacheKeyPrefix); string cacheValue = await GetCacheAsync(cacheKey); Type returnType = context.GetReturnType(); if (string.IsNullOrWhiteSpace(cacheValue)) { if (attribute.OnceUpdate) { string lockKey = $"Lock_{cacheKey}"; RedisValue token = Environment.MachineName; if (await Database.LockTakeAsync(lockKey, token, TimeSpan.FromSeconds(10))) { try { var result = await RunAndGetReturn(context, next); await SetCache(cacheKey, result, attribute.Expiration); return; } finally { await Database.LockReleaseAsync(lockKey, token); } } else { for (int i = 0; i < 5; i++) { Thread.Sleep(i * 100 + 500); cacheValue = await GetCacheAsync(cacheKey); if (!string.IsNullOrWhiteSpace(cacheValue)) { break; } } if (string.IsNullOrWhiteSpace(cacheValue)) { var defaultValue = CreateDefaultResult(returnType); context.ReturnValue = ResultFactory(defaultValue, returnType, context.IsAsync()); return; } } } else { var result = await RunAndGetReturn(context, next); await SetCache(cacheKey, result, attribute.Expiration); return; } } var objValue = await DeserializeCache(cacheKey, cacheValue, returnType); //缓存值不可用 if (objValue == null) { await context.Invoke(next); return; } context.ReturnValue = ResultFactory(objValue, returnType, context.IsAsync()); } catch (Exception) { if (context.ReturnValue == null) { await context.Invoke(next); } } } private async Task<string> GetCacheAsync(string cacheKey) { string cacheValue = null; try { cacheValue = await Database.StringGetAsync(cacheKey); } catch (Exception) { return null; } return cacheValue; } private async Task<object> RunAndGetReturn(AspectContext context, AspectDelegate next) { await context.Invoke(next); return context.IsAsync() ? await context.UnwrapAsyncReturnValue() : context.ReturnValue; } private async Task SetCache(string cacheKey, object cacheValue, int expiration) { string jsonValue = JsonConvert.SerializeObject(cacheValue); await Database.StringSetAsync(cacheKey, jsonValue, TimeSpan.FromSeconds(expiration)); } private async Task Remove(string cacheKey) { await Database.KeyDeleteAsync(cacheKey); } private async Task<object> DeserializeCache(string cacheKey, string cacheValue, Type returnType) { try { return JsonConvert.DeserializeObject(cacheValue, returnType); } catch (Exception) { await Remove(cacheKey); return null; } } private object CreateDefaultResult(Type returnType) { return Activator.CreateInstance(returnType); } private object ResultFactory(object result, Type returnType, bool isAsync) { if (isAsync) { return TypeofTaskResultMethod .GetOrAdd(returnType, t => typeof(Task) .GetMethods() .First(p => p.Name == "FromResult" && p.ContainsGenericParameters) .MakeGenericMethod(returnType)) .Invoke(null, new object[] { result }); } else { return result; } } }
在AspectCore中注册CacheAbleInterceptor拦截器,这里直接注册了用于测试的DemoService,
在正式项目中,打算用反射注册须要用到缓存的Service或者Method。
public static class AspectCoreExtensions { public static void ConfigAspectCore(this IServiceCollection services) { services.ConfigureDynamicProxy(config => { config.Interceptors.AddTyped<CacheAbleInterceptor>(Predicates.Implement(typeof(DemoService))); }); services.BuildAspectInjectorProvider(); } }
[CacheAble(CacheKeyPrefix = "test", Expiration = 30, OnceUpdate = true)] public virtual DateTimeModel GetTime() { return new DateTimeModel { Id = GetHashCode(), Time = DateTime.Now }; }
请求接口,返回时间,并将返回结果缓存到Redis中,保留300秒后过时。