src/ShenNius.Share.Infrastructure/Attributes/CacheInterceptorAttribute.cs

(开头部分) 5KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using AspectCore.DynamicProxy;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using ShenNius.Share.Infrastructure.Caches;
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
/*************************************
* 类名:CacheInterceptor
* 作者:realyrare
* 邮箱:mhg215@yeah.net
* 时间:2021/3/25 19:18:42
*┌───────────────────────────────────┐     
*│      版权所有:神牛软件    	 │
*└───────────────────────────────────┘
**************************************/

namespace ShenNius.Share.Infrastructure.Attributes
{
    public class CacheInterceptorAttribute : AbstractInterceptorAttribute
    {
        private static readonly ConcurrentDictionary<Type, MethodInfo> TypeofTaskResultMethod = new ConcurrentDictionary<Type, MethodInfo>();
        readonly int _expireSecond;
        readonly string _cacheKey;

        #region 拦截处理
        /// <summary>
        /// 过期时间,单位:分
        /// </summary>
        /// <param name="expireMin"></param>
        public CacheInterceptorAttribute(string cacheKey = null, int expireMin = -1)
        {
            _expireSecond = expireMin * 60;
            _cacheKey = cacheKey;
        }

        public async override Task Invoke(AspectContext context, AspectDelegate next)
        {
            try
            {
                string key = string.Empty;
                //自定义的缓存key不存在,再获取类名+方法名或类名+方法名+参数名的组合式key
                if (!string.IsNullOrEmpty(_cacheKey))
                {
                    key = _cacheKey;
                }
                else
                {
                    key = GetKey(context.ServiceMethod, context.Parameters);
                }

                var returnType = GetReturnType(context);
                var cache = context.ServiceProvider.GetService<ICacheHelper>();
                object result = null;
                if (cache.Exists(key))
                {
                    var strResult = cache.Get(key);
                    result = strResult;
后面还有 85 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。