コード例 #1
0
ファイル: CacheBehavior.cs プロジェクト: knyazkov-ma/HelpDesk
        public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
        {
            MethodInfo methodInfo = input.Target.GetType().GetMethods().ToList()
                                    .FirstOrDefault(m => m.Name == input.MethodBase.Name);
            CacheAttribute cacheAttribute =
                methodInfo != null?methodInfo.GetCustomAttribute <CacheAttribute>() : null;


            return(run(input, getNext, cacheAttribute));
        }
コード例 #2
0
ファイル: CacheBehavior.cs プロジェクト: knyazkov-ma/HelpDesk
        private IMethodReturn run(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext, CacheAttribute cacheAttribute)
        {
            var result = getNext()(input, getNext);

            if (result.Exception == null)
            {
                if (cacheAttribute != null)
                {
                    var cacheImplementation = CacheInstaller.GetCache(cacheAttribute.Location);

                    if (cacheImplementation != null)
                    {
                        IList <object> methodPapameters = new List <object>();
                        IEnumerator    enumerator       = input.Arguments.GetEnumerator();
                        int            ind = 0;
                        while (enumerator.MoveNext())
                        {
                            if (cacheAttribute.SkippedParameterIndexes == null ||
                                !cacheAttribute.SkippedParameterIndexes.Contains(ind))
                            {
                                IForCacheKeyValue forCacheKeyValue = enumerator.Current as IForCacheKeyValue;
                                if (forCacheKeyValue != null)
                                {
                                    methodPapameters.Add(forCacheKeyValue.GetForCacheKeyValue());
                                }
                                else
                                {
                                    methodPapameters.Add(enumerator.Current);
                                }
                            }

                            ind++;
                        }


                        if (cacheAttribute.Invalidate)
                        {
                            if (!String.IsNullOrWhiteSpace(cacheAttribute.InvalidateCacheKeyTemplates))
                            {
                                string[] invalidateCacheKeyTemplates = cacheAttribute.InvalidateCacheKeyTemplates.Split(',');
                                foreach (var t in invalidateCacheKeyTemplates)
                                {
                                    cacheImplementation.Remove(String.Format(t.Trim(), methodPapameters.ToArray()));
                                }
                            }
                        }
                        else
                        {
                            string cacheKey = String.Format(cacheAttribute.CacheKeyTemplate, methodPapameters.ToArray());

                            result.ReturnValue = cacheImplementation
                                                 .AddOrGetExisting(cacheKey,
                                                                   () =>
                            {
                                return(result.ReturnValue);
                            },
                                                                   cacheAttribute.ExpirationSeconds);
                        }

                        return(result);
                    }

                    return(result);
                }
                else
                {
                    return(result);
                }
            }
            else
            {
                return(input.CreateExceptionMethodReturn(result.Exception));
            }
        }