zoukankan      html  css  js  c++  java
  • 我的一个自己写的更新缓存的aop实例

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.Remoting.Messaging;
    using System.Runtime.Remoting.Contexts;

    namespace AopDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                var tst = new test();
                tst.show();
                tst.show2();
                Console.ReadLine();
            }
        }
        [AutoClearCache("CacheName")]
        public class test : ContextBoundObject
        {
            [AutoClearCacheMethod]
            public void show()
            {
                Console.WriteLine("调用本方法");
            }

            public void show2()
            {
                Console.WriteLine("调用方法2");
            }
        }

        public sealed class AutoClearCacheAop : IMessageSink
        {
            private IMessageSink nextSink; //保存下一个接收器

            public AutoClearCacheAop(IMessageSink nextSink, string CacheName)
            {
                this.CacheName = CacheName;
                this.nextSink = nextSink;
            }
            ///  IMessageSink接口方法,用于异步处理,我们不实现异步处理,所以简单返回null,
            ///  不管是同步还是异步,这个方法都需要定义
            public IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink)
            {
                return null;
            }
            /// 下一个接收器
            public IMessageSink NextSink
            {
                get { return nextSink; }
            }
            public IMessage SyncProcessMessage(IMessage msg)
            {
                IMessage retMsg = null;

                IMethodCallMessage call = msg as IMethodCallMessage;

                if (call == null || (Attribute.GetCustomAttribute(call.MethodBase, typeof(AutoClearCacheMethodAttribute))) == null)
                    retMsg = nextSink.SyncProcessMessage(msg);
                else
                {
                    Console.WriteLine(this.CacheName);
                    //传递消息给下一个接收器 - > 就是指执行你自己的方法
                    retMsg = nextSink.SyncProcessMessage(msg);
                    Console.WriteLine("拦截方法完");
                }

                return retMsg;
            }
            private string CacheName;
        }
        /// <summary>
        /// 标注类某方法内所有数据库操作加入事务控制
        /// </summary>
        [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
        public sealed class AutoClearCacheAttribute : ContextAttribute, IContributeObjectSink
        {
            private string CacheName;
            /// <summary>
            /// 标注类某方法内所有数据库操作加入事务控制,请使用TransactionMethodAttribute同时标注
            /// </summary>
            public AutoClearCacheAttribute(string CacheName)
                : base(CacheName)
            {
                this.CacheName = CacheName;
            }

            public IMessageSink GetObjectSink(MarshalByRefObject obj, IMessageSink next)
            {
                return new AutoClearCacheAop(next, this.CacheName);
            }
        }

        [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
        public sealed class AutoClearCacheMethodAttribute : Attribute
        {
            
        }


    }

  • 相关阅读:
    Python Django 之 Template 模板语言简介
    Prometheus
    MySQL 存储过程中执行DDL
    Mysql 存储过程-转载
    Mysql 时间类型整理
    MySQL 5.7 分区表性能下降的案例分析
    Mysql 分区表-分区操作
    Mysql 中Left/Right join on后面and和where条件查询的差异-Mysql SQL运算符是有优先级
    MySQL出现Waiting for table metadata lock的原因以及解决方法
    Python 字符串转换为字典(String to Dict)
  • 原文地址:https://www.cnblogs.com/qisheng/p/3042618.html
Copyright © 2011-2022 走看看