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. 创建节点createElement

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  2. HDU 2108 Shape of HDU (判断是不是凸多边形 叉乘)

    Shape of HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  3. 【dotnet跨平台】&quot;dotnet restore&quot;和&quot;dotnet run&quot;都做了些什么?

    [dotnet跨平台]"dotnet restore"和"dotnet run"都做了些什么? 前言: 关于dotnet跨平台的相关内容.能够參考:跨平台.NE ...

  4. Codeforces Round #262 (Div. 2)460A. Vasya and Socks(简单数学题)

    题目链接:http://codeforces.com/contest/460/problem/A A. Vasya and Socks time limit per test 1 second mem ...

  5. OpenStack源码系列---nova-conductor

    nova-conductor启动的也是一个rpc server,代码框架和nova-compute类似,所以我也懒得再详细分析一遍服务启动的过程.nova-api那篇文章的最后我说"cctx ...

  6. libusb 源码阅读

    libusb_init(NULL), 如果传入一个NULL, 则libusb 内部会有一个 usbi_default_context 变量在内部保存上下文. 这样以后调用 libusb 函数时可以不指 ...

  7. Android如果动态改变CursorAdapter Item个数

    //adapter内部类 private class SearchAdapter extends CursorAdapter { @Override public View newView(Conte ...

  8. UVa 12587 Reduce the Maintenance Cost(Tarjan + 二分 + DFS)

    题意:n个城市(n <= 10000), 有m条边(m <= 40000),每一个城市有一个维护费用Cost(i),除此之外,每条边的维修费用为去掉该边后不能通信的城市对数与边权的积.这个 ...

  9. python -- day 11 考试题

    1. 文件t1.txt里面的内容为:(6分) 1,alex,22,13651054608,IT 2,wusir,23,13304320533,Tearcher 3,taibai,18,13332353 ...

  10. C++11 std::function、std::bind和lambda表达式

    参考博客: C++可调用对象详解-https://www.cnblogs.com/Philip-Tell-Truth/p/5814213.html 一.关于std::function与std::bin ...