简体   繁体   English

如何使用 ServiceStack 获取服务方法的可配置缓存持续时间?

[英]How To Get Configurable Cache Duration on Service Methods With ServiceStack?

I was using CacheResponseAttribute on one of the Get methods in the service like [CacheResponse(Duration = 60)] .我在服务中的一种 Get 方法上使用了CacheResponseAttribute ,例如[CacheResponse(Duration = 60)] But I want this cache duration to come from a config file so I can set it to be different depending on the environment the service is currently running on (dev, prod, etc)但我希望此缓存持续时间来自配置文件,因此我可以根据服务当前运行的环境(开发、产品等)将其设置为不同

I know we can't use something that's not constant as a parameter in the attribute constructor.我知道我们不能在属性构造函数中使用非常量作为参数。 So I was planning to do something like所以我打算做类似的事情

    public class MyCacheResponseAttribute : CacheResponseAttribute
    {
        public IConfiguration Configuration { get; set; }
        public CacheWidgetResponseAttribute()
        {
            int.TryParse(Configuration["cache_duration_in_secs"], out var cacheDuration);
            Duration = cacheDuration;
        }
    }

and use this as the decorator on the Get method.并将其用作 Get 方法的装饰器。 However, the dependency injection doesn't seem to work for the attributes since I'm getting the Configuration as null.但是,依赖注入似乎不适用于属性,因为我将配置设置为 null。

My return type is string, I've tried ToOptimizedResultUsingCache but I couldn't get it to return string properly.我的返回类型是字符串,我试过 ToOptimizedResultUsingCache 但我无法让它正确返回字符串。

What options do I have?我有什么选择? Is it possible to make the IoC work on Attributes somehow?是否有可能使 IoC 以某种方式在属性上工作? I guess as a last resort I could have a ICacheClient in the service and use it but that would be my last resort since it's gonna be more custom made.我想作为最后的手段,我可以在服务中使用ICacheClient并使用它,但这将是我最后的手段,因为它会更加定制。

Request Filter Attributes does have their properties autowired from the IOC but that can only happen after an objects constructor is executed, not before.请求过滤器属性确实具有从 IOC 自动装配的属性,但这只能在执行对象构造函数之后发生,而不是之前。

So you could read from your injected IOC properties before the attribute is executed, eg:因此,您可以在执行属性之前从注入的 IOC 属性中读取数据,例如:

public class MyCacheResponseAttribute : CacheResponseAttribute
{
    public IConfiguration Configuration { get; set; }
    public override Task ExecuteAsync(IRequest req, IResponse res, object requestDto)
    {
        if (Duration == default 
            && int.TryParse(Configuration["cache_duration_in_secs"], out var duration))
            Duration = duration;
        return base.ExecuteAsync(req, res, requestDto);
    }
}

Or resolve the IOC dependencies via the singleton, eg:或者通过 singleton 解决 IOC 依赖关系,例如:

public class MyCacheResponseAttribute : CacheResponseAttribute
{
    public MyCacheResponseAttribute()
    {
        var config = HostContext.Resolve<IConfiguration>();
        if (int.TryParse(config["cache_duration_in_secs"], out var duration))
            Duration = duration;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM