当前位置: 首页 > news >正文

前端做数据表格的网站如何提升wordpress的打开速度

前端做数据表格的网站,如何提升wordpress的打开速度,搭建影视网站违法,家具网站建设目的及功能定位目录 什么是Filter#xff1f; Exception Filter 实现 注意 ActionFilter 注意 案例#xff1a;自动启用事务的筛选器 事务的使用 TransactionScopeFilter的使用 什么是Filter#xff1f; 切面编程机制#xff0c;在ASP.NET Core特定的位置执行我们自定义的代码。…目录 什么是Filter Exception Filter 实现 注意 ActionFilter 注意 案例自动启用事务的筛选器 事务的使用 TransactionScopeFilter的使用 什么是Filter 切面编程机制在ASP.NET Core特定的位置执行我们自定义的代码。ASP.NET Core中的Filter的五种类型Authorization filter、Resource filter、Action filter、Exception filter、Result filter。本书中重点讲解Exception filter和Action filter。所有筛选器一般有同步和异步两个版本比如IActionFilter、IAsyncActionFilter接口。 Exception Filter 当系统中出现未经处理的异常的时候异常筛选器就会执行。 实现 当系统中出现未处理异常的时候我们需要统一给客户端返回如下格式的响应报文{“code”:”500”,”message”:”异常信息”}。 对于开发环境中message是异常堆栈对于其他环境message用一个general的报错信息。实现IAsyncExceptionFilter接口。注入IHostEnvironment得知运行环境。 public class LogExceptionFilter : IAsyncExceptionFilter {public Task OnExceptionAsync(ExceptionContext context){return File.AppendAllTextAsync(F:/error.log, context.Exception.ToString());} }public class MyExceptionFilter : IAsyncExceptionFilter {private readonly IWebHostEnvironment hostEnv;public MyExceptionFilter(IWebHostEnvironment hostEnv){this.hostEnv hostEnv;}public Task OnExceptionAsync(ExceptionContext context){//context.Exception代表异常信息对象//context.ExceptionHandled为true时表示异常已经被处理其他ExceptionFilter将不会再处理//context.Result的值会返回给客户端string msg;if (hostEnv.IsDevelopment()){msg context.Exception.Message;}else{msg 服务器发生了未处理异常;}ObjectResult objresult new ObjectResult(new { code 500, message msg });context.Result objresult;context.ExceptionHandled true;return Task.CompletedTask;} } 注意 ExceptionFilter执行顺序与注册顺序有关后注册的先执行 builder.Services.ConfigureMvcOptions(opt {opt.Filters.AddMyExceptionFilter();opt.Filters.AddLogExceptionFilter(); }); ActionFilter IAsyncActionFilter接口 多个Action Filter的链式执行。 注意 ActionFilter执行顺序与注册顺序有关先注册的先执行 public class MyActionFilter1 : IAsyncActionFilter {public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next){Console.WriteLine(MyActionFilter1前代码);ActionExecutedContext result await next();if (result.Exception ! null){Console.WriteLine(MyActionFilter1捕获到异常);}else{Console.WriteLine(MyActionFilter1后代码);}} } builder.Services.ConfigureMvcOptions(opt {opt.Filters.AddMyActionFilter1();opt.Filters.AddMyActionFilter2(); }); 案例自动启用事务的筛选器 数据库事务要么全部成功、要么全部失败。自动化启动、提交以及回滚事务。当一段使用EF Core进行数据库操作的代码放到TransactionScope声明的范围中的时候这段代码就会自动被标记为“支持事务”。TransactionScope实现了IDisposable接口如果一个TransactionScope的对象没有调用Complete()就执行了Dispose()方法则事务会被回滚否则事务就会被提交。TransactionScope还支持嵌套式事务。.NET Core中的TransactionScope不像.NET FX一样有MSDTC分布式事务提升的问题。请使用最终一致性事务。 事务的使用 [HttpPost] public async Taskstring Test2() {using (TransactionScope tx new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)){dbctx.Books.Add(new Book { Title 计算机网络, Price 12.6 });await dbctx.SaveChangesAsync();dbctx.People.Add(new Person { Name 张三, Age 20 });await dbctx.SaveChangesAsync();tx.Complete();return OK;} }[HttpPost] public string Test1() {using (TransactionScope tx new TransactionScope()){dbctx.Books.Add(new Book { Title 计算机网络, Price 12.6 });dbctx.SaveChangesAsync();dbctx.People.Add(new Person { Name 张三, Age 20 });dbctx.SaveChangesAsync();tx.Complete();return OK;} } TransactionScopeFilter的使用 对于强制不进行事务控制的Action方法请标注NotTransactionalAttribute。 开发筛选器TransactionScopeFilter把TransactionScopeFilter注册到Program.cs中。 NotTransationAttribute.cs [AttributeUsage(AttributeTargets.Method)] public class NotTransationAttribute:Attribute { }TransactionScopeFilter.cs public class TransactionScopeFilter : IAsyncActionFilter {public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next){//context.ActionDescriptor中是当前被执行的Action的描述信息//context.ActionArguments中是当前被执行的Action的参数信息ControllerActionDescriptor controllerActionDescriptor context.ActionDescriptor as ControllerActionDescriptor;//if (controllerActionDescriptor null)//不是一个MVC的Actionbool isTX false;//是否进行事务控制if (controllerActionDescriptor ! null){//获取当前Action是否有NotTransationAttribute特性bool hasNotTransationAttribute controllerActionDescriptor.MethodInfo.GetCustomAttributes(typeof(NotTransationAttribute), false).Any();//如果没有NotTransationAttribute特性则进行事务控制isTX !hasNotTransationAttribute;}if (isTX){//创建一个异步的事务范围using (TransactionScope txnew TransactionScope(TransactionScopeAsyncFlowOption.Enabled)){var r await next();if (r.Exceptionnull){tx.Complete();}}}else{await next();}} }Program.cs builder.Services.ConfigureMvcOptions(opt {opt.Filters.AddTransactionScopeFilter(); }); 案例开发请求限流器 需求 Action Filter可以在满足条件的时候终止操作方法的执行。在Action Filter中如果我们不调用await next()就可以终止Action方法的执行了。为了避免恶意客户端频繁发送大量请求消耗服务器资源我们要实现“一秒钟内只允许最多有一个来自同一个IP地址的请求”。 public class ratelimitActionFilter : IAsyncActionFilter {private readonly IMemoryCache memcache;public ratelimitActionFilter(IMemoryCache memcache){this.memcache memcache;}public Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next){//从HTTP请求的上下文中获取客户端的远程IP地址string removeIP context.HttpContext.Connection.RemoteIpAddress.ToString();//context.ActionDescriptor中是当前被执行的Action的描述信息转换为ControllerActionDescriptor类型ControllerActionDescriptor controllerActionDescriptor context.ActionDescriptor as ControllerActionDescriptor;//构建缓存的Keystring cacheKey $LastVisitTick_{controllerActionDescriptor.ControllerName}_{removeIP};//从缓存中获取上次访问的时间戳long? lastTick memcache.Getlong?(cacheKey);//如果上次访问的时间戳不存在或者距离当前时间已经超过1秒if (lastTick null || Environment.TickCount64 - lastTick 1000){//设置当前的时间戳到缓存中memcache.Set(cacheKey, Environment.TickCount64, TimeSpan.FromSeconds(10));//避免长期不访问的用户一直占用缓存return next();}else{context.Result new ContentResult{StatusCode 429,Content 访问太频繁请稍后再试};return Task.CompletedTask;}} }
http://www.w-s-a.com/news/286970/

相关文章:

  • 小型网站网站建设需要网络公司是什么行业
  • 滑动 手机网站 代码网页制作与设计讨论
  • 自己做网站处理图片用什么软件wordpress html5支持
  • 校园网站怎么建软文文案范文
  • 中国建设官方网站如何创建自己的软件
  • 来宾住房与城乡建设网站天津西青区怎么样
  • 西安网站建设培训班鄂州网页定制
  • 西部数码网站备份自己怎么做网站啊
  • h5网站开发用什么软件制作公司网站建设代理怎么做
  • 网站建设资料准备网上购物app有哪些
  • 沧州做网站优化哪家公司便宜国内百度云网站建设
  • 网站的最近浏览 怎么做龙岩市人才网最新招聘信息
  • 网站建设需要找工信部吗网站开发账务处理
  • 做那种的视频网站个体工商网站备案
  • 推广网官方推广网站中国建设招聘信息网站
  • 医院网站建设需要多少钱网络营销渠道可分为哪几种
  • 怎么取网页视频网站元素计算机专业论文网站开发
  • 上海网站建设备案号怎么恢复wordpress打开页面空白
  • 30个做设计的网站企业设计网站
  • 招生网站开发的背景创意 wordpress
  • 网站备案资料查询小型企业管理系统软件
  • 温州网站建设维护怎么做好网站开发、设计
  • 佛山 做网站公司有哪些网站排名忽然不见了
  • 广告网站建设最专业东莞大朗网站设计
  • 网站做流量的论坛贴吧分销商城系统源码
  • 新手建立网站的步骤网站建设费怎么入分录
  • 哪里建网站性价比高做网站赚取广告费
  • 邢台集团网站建设价格微信怎么做捐钱的网站
  • 做网站费用需要分摊吗装修公司一般多少钱一平方
  • 公司主页的网站格式wordpress自动推送给百度