PostSharp Caching MethodInterceptionAspect使用ASP.NET Core内存缓存

问题描述 投票:2回答:1
public class CacheAttribute : MethodInterceptionAspect
{
    public override void OnInvoke(MethodInterceptionArgs methodInterceptionArgs)
    {
        if ((methodInterceptionArgs.Method.Name == CacheAspectAction.Get.ToString()) 
            //&& (Memory.Cache[cacheKey] != null)
            )
        {
        //    methodInterceptionArgs.ReturnValue = HttpRuntime.Cache[cacheKey];
            return;
        }

        object returnVal = methodInterceptionArgs.Invoke(methodInterceptionArgs.Arguments);

        ClanCache(cacheKeyBase, cacheKey);

        if (returnVal != null)
            //Memory.Cache.Insert(cacheKey, returnVal, null, expirationInformation.AbsoluteExpiration, expirationInformation.SlidingExpiration);

        methodInterceptionArgs.ReturnValue = returnVal;
    }
}

如何从任何类访问ASP.NET Core中的内存缓存,包括PostSharp方面?例如,我需要访问IMemoryCacheMethodInterceptionAspect中的OnMethodBoundaryAspect

c# .net-core postsharp
1个回答
1
投票

我将在这里假设您正在使用内置的ASP.NET Core依赖注入和IMemoryCache实现。但是,该示例可以很容易地适应其他实现。我将选择Global Service Locator方法来解决方面的依赖关系。以下是文档页面中的修改示例。

// A helper class that resolves services using built-in ASP.NET Core service provider.
public static class AspectServiceLocator
{
    private static IServiceProvider serviceProvider;

    public static void Initialize(IWebHost host)
    {
        serviceProvider = host.Services;
    }

    public static Lazy<T> GetService<T>() where T : class
    {
        return new Lazy<T>(GetServiceImpl<T>);
    }

    private static T GetServiceImpl<T>()
    {
        if (serviceProvider == null)
            throw new InvalidOperationException();

        return (T) serviceProvider.GetService(typeof(T));
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        IWebHost host = CreateWebHostBuilder(args).Build();

        // Initialize the AspectServiceLocator during ASP.NET Core program start-up
        AspectServiceLocator.Initialize(host);

        host.Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
}

[PSerializable]
public class CacheAttribute : MethodInterceptionAspect
{
    private static Lazy<IMemoryCache> cache;

    static CacheAttribute()
    {
        // Use AspectServiceLocator to initialize the cache service field at application run-time.
        if (!PostSharpEnvironment.IsPostSharpRunning)
        {
            cache = AspectServiceLocator.GetService<IMemoryCache>();
        }
    }

    public override void OnInvoke(MethodInterceptionArgs args)
    {
        object cacheKey = args.Method.Name;
        object cachedResult;

        if (cache.Value.TryGetValue(cacheKey, out cachedResult))
        {
            args.ReturnValue = cachedResult;
            return;
        }

        args.Proceed();

        cache.Value.Set(cacheKey, args.ReturnValue);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.