示例#1
0
 private string getKey(IInvocation invocation, CacheAttribute attribute)
 {
     if (attribute.IsKey)
     {
         if (attribute.IsResolve)
         {
             throw new NotSupportedException();
         }
         else
         {
             return attribute.Key;
         }
     }
     else//if (attribute.IsArg)
     {
         if (attribute.IsResolve)
         {
             throw new NotSupportedException();
         }
         else
         {
             object[] args = InterceptorHelper.GetInvocationMethodArgs(invocation);
             if (attribute.Arg <= args.Length)
             {
                 return args[attribute.Arg].ToString();
             }
             else
             {
                 throw new IndexOutOfRangeException();
             }
         }
     }
 }
示例#2
0
 private string getKey(IInvocation invocation, CacheAttribute attribute)
 {
     if (attribute.IsKey)
     {
         if (attribute.IsResolve)
         {
             throw new NotSupportedException();
         }
         else
         {
             return(attribute.Key);
         }
     }
     else//if (attribute.IsArg)
     {
         if (attribute.IsResolve)
         {
             throw new NotSupportedException();
         }
         else
         {
             object[] args = InterceptorHelper.GetInvocationMethodArgs(invocation);
             if (attribute.Arg <= args.Length)
             {
                 return(args[attribute.Arg].ToString());
             }
             else
             {
                 throw new IndexOutOfRangeException();
             }
         }
     }
 }
示例#3
0
 private IDependencyWrapper getDependency(IInvocation invocation, CacheAttribute attribute)
 {
     Type targetType = invocation.TargetType;
         MethodInfo dependencyCallback = targetType.GetMethod(attribute.DependencyCallback);
         IDependencyWrapper dependency = (IDependencyWrapper)dependencyCallback.Invoke(
             null,
             new object[]{
                 InterceptorHelper.GetInvocationTarget(invocation),
                 InterceptorHelper.GetInvocationMethodArgs(invocation)
             }
         );
         return dependency;
 }
示例#4
0
        public override void Intercept(IInvocation invocation)
        {
            CacheAttribute attribute = AttributeHelper.GetAttribute <CacheAttribute>(invocation) as CacheAttribute;

            attribute.Key = getKey(invocation, attribute);
            ICache cache     = IocCoreFactory.Get <ICache>();
            object cacheData = cache.Get(attribute.Key);

            if (cacheData != null)
            {
                invocation.ReturnValue = cacheData;
                if (LogLevel <= attribute.LogLevel)
                {
                    StringBuilder logstr = new StringBuilder();
                    logstr.AppendFormat("Cache Hit! Key:\"{0}\" Caller:{1}", attribute.Key, InterceptorHelper.GetMethodInfo(invocation));
                    Log.Log(logstr.ToString(), attribute.LogLevel);
                }
                return;
            }
            IDependencyWrapper dependency;

            if (!String.IsNullOrWhiteSpace(attribute.DependencyCallback))
            {
                dependency = getDependency(invocation, attribute);
            }
            else
            {
                dependency = null;
            }
            invocation.Proceed();
            if (dependency != null)
            {
                object     returnValue       = invocation.ReturnValue;
                MethodInfo onRemovedCallback = invocation.TargetType.GetMethod(attribute.OnRemovedCallback);
                cache.Insert(
                    attribute.Key,
                    returnValue,
                    dependency.Instance,
                    attribute.Absolute,
                    attribute.Sliding,
                    (int)attribute.Priority,
                    onRemovedCallback);
                if (LogLevel <= attribute.LogLevel)
                {
                    StringBuilder logstr = new StringBuilder();
                    logstr.AppendFormat("Cache Insert! Key:\"{0}\" Caller:{1}", attribute.Key, InterceptorHelper.GetMethodInfo(invocation));
                    Log.Log(logstr.ToString(), attribute.LogLevel);
                }
            }
        }
示例#5
0
        private IDependencyWrapper getDependency(IInvocation invocation, CacheAttribute attribute)
        {
            Type               targetType         = invocation.TargetType;
            MethodInfo         dependencyCallback = targetType.GetMethod(attribute.DependencyCallback);
            IDependencyWrapper dependency         = (IDependencyWrapper)dependencyCallback.Invoke(
                null,
                new object[] {
                InterceptorHelper.GetInvocationTarget(invocation),
                InterceptorHelper.GetInvocationMethodArgs(invocation)
            }
                );

            return(dependency);
        }