zoukankan      html  css  js  c++  java
  • asp.net mvc,基于aop实现的接口访问统计、接口缓存等

    其实asp.net 上aop现有的框架应该蛮多的,比如静态注入式的PostSharp(新版本好像已经商业化了,旧版本又不支持.net4.0+),或者通过反射的(性能会降低)。

    本文则是通过mvc其中一种方法拦截器ActionFilter(参考网上已经有很多类似例子)。

    首先新建一个日志控制类,命名为ApiLogAttribute,继承于ActionFilterAttribute

    /// <summary>
        /// 记录访问日志以及站点安全检查
        /// </summary>
        public class ApiLogAttribute : ActionFilterAttribute
        {
            #region 属性
    
             private string logType;
             public ApiLogAttribute(string type)
            {
                logType = type;
            }
    
            #endregion
    
            public override void OnActionExecuting(HttpActionContext actionContext)
            {
                if (string.IsNullOrEmpty(logType))
                    return;
                AccessSummaryHelper.AddOneVisit(logType);//自己实现接口访问量统计的代码
    
               
                //这里可以约定 增加一些接口校验,避免无效的请求。return;
                    
                
                //校验不通过的,则直接返回,不会进入方法体内
                actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.OK, new ApiResult{ code=1,Msg="无效的请求。"});
     } 
    }

    接着,再建一个接口方法缓存类,命名为ApiCacheAttribute,继承自ActionFilterAttribute

    public class ApiCacheAttribute : ActionFilterAttribute//,System.Web.Mvc.ActionFilterAttribute
        {
        //设置缓存的时间,默认为1分钟
    private CacheTimeOption cacheTime ; public ApiCacheAttribute(CacheTimeOption cachetime = CacheTimeOption.OneMinute) { cacheTime = cachetime; } public override void OnActionExecuting(HttpActionContext actionContext) { base.OnActionExecuting(actionContext); string cacheKey = HttpContext.Current.Request.RawUrl; ApiResult ret = CacheHelper.Get<ApiResult>(cacheKey); //在action执行前终止请求时,应该使用填充方法Response,将不返回action方法体。 if (ret != null) { actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.OK, ret); } } public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) { string cacheKey = HttpContext.Current.Request.RawUrl; var obj = ((ObjectContent)(actionExecutedContext.Response.Content)).Value; if (obj != null) { ApiResult ret = obj as ApiResult; if (ret != null) { CacheHelper.Set(cacheKey, ret, cacheTime); } } } }

    注意上面 ApiResult为自定义的接口返回类。

    public class ApiResult
    {
            public int Code{ get; set; }
    
            public string Msg { get; set; } 

    public object Data { get; set; }
    }

    最后在具体的类接口上调用如下:

     public class XXController : ApiController
        {
            
            [HttpGet]
            [ApiCache]
            [ApiLog("1.1")]
            public ApiResult GetXX()
            {
                    //业务代码略
            }
    }
  • 相关阅读:
    【MySQL】学生成绩
    【MySQL】统计累计求和
    【MySQL】查询不在表中的数据
    【MySQL】排名函数
    【Python】数据处理分析,一些问题记录
    【Github】如何下载csv文件/win10如何修改txt文件为csv文件
    【Windows】github无法访问/hosts文件只能另存为txt
    【C/C++】拔河比赛/分组/招商银行
    【C/C++】金币
    【C/C++】旋转数组的最小数字/ 剑指offer
  • 原文地址:https://www.cnblogs.com/contraII/p/3822467.html
Copyright © 2011-2022 走看看