AOP实现缓存的一个例子

using AspectCore.DynamicProxy;
using Microsoft.Extensions.Caching.Memory; [AttributeUsage(AttributeTargets.Method)]
public class MemoryCacheAttribute : AbstractInterceptorAttribute
{
public int Expiration { get; set; } = 2;
public string CacheKey { get; set; } = null; private static readonly MethodInfo _taskResultMethod; private readonly IMemoryCache _cache = MemoryCacheManager.GetInstance(); static MemoryCacheAttribute()
{
_taskResultMethod = typeof(Task).GetMethods().FirstOrDefault(p => p.Name == "FromResult" && p.ContainsGenericParameters);
} public override async Task Invoke(AspectContext context, AspectDelegate next)
{
var parameters = context.ServiceMethod.GetParameters();
if (parameters.Any(it => it.IsIn || it.IsOut))
{
await next(context);
}
else
{
var key = string.IsNullOrEmpty(CacheKey)
? new CacheKey(context.ServiceMethod, parameters, context.Parameters).GetMemoryCacheKey()
: CacheKey;
var returnType = context.IsAsync()
? context.ServiceMethod.ReturnType.GetGenericArguments().First()
: context.ServiceMethod.ReturnType;
if (_cache.TryGetValue(key, out object value))
{
context.ReturnValue = context.IsAsync()
? _taskResultMethod.MakeGenericMethod(returnType).Invoke(null, new object[] { value })
: value;
return;
}
else
{
await context.Invoke(next);
object returnValue = context.ReturnValue;
if (context.ServiceMethod.IsReturnTask())
{
returnValue = returnValue.GetPropertyValue("Result");
} _cache.Set(key, returnValue, new MemoryCacheEntryOptions()
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(Expiration)
});
} }
}
}

AspectCore的AOP操作的更多相关文章

  1. .NetCore中使用AspectCore、ExceptionLess 实现AOP操作日志记录

    结合前面封装的ExceptionLess,接下来使用 AspectCore 实现AOP日志处理 nuget导入AspectCore.Core .AspectCore.Extensions.Depend ...

  2. Java框架spring 学习笔记(十四):注解aop操作

    回见Java框架spring Boot学习笔记(十三):aop实例操作,这里介绍注解aop操作 首先编写一个切入点HelloWorld.java package com.example.spring; ...

  3. Spring的aop操作

    1 在spring里面进行aop操作,使用aspectj实现(1)aspectj不是spring一部分,和spring一起使用进行aop操作(2)Spring2.0以后新增了对AspectJ支持2 使 ...

  4. Spring_day02--Spring的aop操作

    Spring的aop操作 1 在spring里面进行aop操作,使用aspectj实现 (1)aspectj不是spring一部分,和spring一起使用进行aop操作 (2)Spring2.0以后新 ...

  5. 160309、Spring AOP操作action时无法注入,报空指针错误

    今天帮同事看个问题,action注入失败,代码没问题,主要是stuts2权限移交的问题,特此记录一下 Spring AOP操作action时无法注入,报NullPointer异常 当使用Spring ...

  6. spring AOP操作

    在spring进行AOP操作,使用aspectj实现 一.aspectj准备 aspectj不是spring的一部分,和spring一起使用进行AOP的操作 1.除了spring基本的jar包还需要导 ...

  7. Spring框架 aop操作的注解方法 基于aspectj的自动注解aop方法 抽取相同的value="execution(public void cn.itcast.f_aspect.CRUD.*())"

    首先是在xml配置文件中配置好对象,然后开启aop的注解方法——即<aop:aspectj-autoproxy></aop:aspectj-autoproxy> xml代码如下 ...

  8. Spring AOP操作action时无法注入,报NullPointer异常

    Spring AOP操作action时无法注入,报NullPointer异常当使用Spring AOP对action层进行操作时,会出现注入失败的问题,出现空指针异常.原因是一般struts2+spr ...

  9. AOP操作-AspectJ配置文件

    AOP操作(AspectJ配置文件)(了解) (实际中大部分用注解方式) 1,创建两个类,增强类和被增强类,创建方法 2,在spring配置文件中创建两个类对象 3,在spring配置文件中配置切入点

随机推荐

  1. [POI 2001+2014acm上海邀请赛]Gold Mine/Beam Cannon 线段树+扫描线

    Description  Byteman, one of the most deserving employee of The Goldmine of Byteland, is about to re ...

  2. hibernate载入持久化对象的两种方式——get、load

    一.get与load对照 在hibernate中get和load方法是依据id取得持久化对象的两种方法.但在实际使用的过程中总会把两者混淆,不知道什么情况下使用get好,什么时候使用load方法效率更 ...

  3. C++再论单例模式

    #include <iostream> #include <windows.h> #include <mutex> std::mutex gmutex; using ...

  4. v-for指令

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Hero In Maze

    Hero In Maze 时间限制(普通/Java):1000MS/10000MS          执行内存限制:65536KByte 描写叙述 500年前,Jesse是我国最卓越的剑客. 他英俊潇 ...

  6. C# Excel批注“哪种开发语言最好”

    Excel批注经常使用于为个别的单元格加入凝视.读者可以从凝视中获取额外的信息. 批注可隐藏,仅仅会在单元格右上方显示红色三角.加入后不会对单元格的内容喧宾夺主.在日常编程处理Excel中,为个别单元 ...

  7. $.ajax 使用详解

    Jquery在异步提交方面封装的很好,直接用AJAX非常麻烦,Jquery大大简化了我们的操作. $.post.$.get是一些简单的方法,如果要处理复杂的逻辑,还是需要用到jQuery.ajax() ...

  8. URAL 1731. Dill(数学啊 )

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1731 1731. Dill Time limit: 0.5 second Memory ...

  9. 常见的页面效果,相关的js代码

    1.焦点图 $(document).ready(function(){ var i=0; var autoChange= setInterval(function(){ if(i<$(" ...

  10. DEDE自定义表单显示提交时间|添加提交时间,获取ip的方法

    前提是后台自定义表单字段一定要有  “时间”,这里的acca_time <div class="tit">*咨询内容:</div> <div clas ...