コード例 #1
0
        public override void OnInvoke(MethodInterceptionArgs args)
        {
            var keyGenerator     = new StringBuilder();
            var referenceTypeKey = new StringBuilder();
            var valueTypeKey     = new StringBuilder();

            var methodName = CacheExtensions.IsCustomKey(_customKey)
                ? args.MethodNameWithClassName()
                : _customKey.WhiteSpaceRemove();

            keyGenerator.Append(methodName);

            var arguments = args.Arguments.ToList();

            foreach (var dataArgument in arguments)
            {
                if (dataArgument != null)
                {
                    var getTypeInfo = dataArgument.GetType().GetTypeInfo().ToString();
                    var getBaseType = dataArgument.GetType().BaseType?.ToString();

                    if (getTypeInfo != "System.String" && getBaseType != "System.ValueType")
                    {
                        var getObjectName = dataArgument.GetType().Name;

                        referenceTypeKey.AppendFormat($"{getObjectName}RT/");

                        var getPropertiesValue =
                            dataArgument.GetType().GetProperties().Select(x => x.GetValue(dataArgument));

                        foreach (var value in getPropertiesValue)
                        {
                            referenceTypeKey.AppendFormat(value == null ? "<Null>/" : $"{value}/");
                        }
                    }

                    if (getTypeInfo == "System.String" || getBaseType == "System.ValueType")
                    {
                        valueTypeKey.AppendFormat($"{dataArgument}-");
                    }
                }

                else
                {
                    valueTypeKey.AppendFormat("<Null>-");
                }
            }

            if (!IsNullOrEmpty(referenceTypeKey.ToString()))
            {
                referenceTypeKey.Remove(referenceTypeKey.Length - 1, 1);
                keyGenerator.Append("&" + referenceTypeKey);
            }

            if (!IsNullOrEmpty(valueTypeKey.ToString()))
            {
                valueTypeKey = valueTypeKey.Remove(valueTypeKey.Length - 1, 1);
                keyGenerator.Append("&VT" + valueTypeKey);
            }

            var clearKeyData = keyGenerator.ToString().WhiteSpaceRemove();

            if (_cacheManager.IsAdd(clearKeyData))
            {
                var cacheValue = _cacheManager.Get <object>(clearKeyData);

                switch (_cacheType.Name)
                {
                case "MicrosoftMemoryCacheManager":

                    args.ReturnValue = cacheValue;
                    return;

                case "RedisCacheManager":

                    var methodInfo = args.Method as MethodInfo;
                    if (methodInfo == null)
                    {
                        return;
                    }
                    var methodReturnType = methodInfo.ReturnType;

                    args.ReturnValue = JsonConvert.DeserializeObject(cacheValue.ToString(), methodReturnType);
                    return;

                default:
                    return;
                }
            }

            base.OnInvoke(args);

            _cacheManager.Add(clearKeyData, args.ReturnValue, _cacheByMinute);
        }