ASP.. NET web服务缓存

本文关键字:缓存 服务 web NET ASP | 更新日期: 2023-09-27 18:14:22

是否可以缓存函数输出仅当null作为参数传递?像这样:

[WebMethod(CacheDuration = 360, NullOnly = true)]
public SomeClass MyMethod(MyClass whatever)
{
    //do something...
    return something;
}

所以当whatever == null时,函数返回缓存的输出,当它不是null时,它生成不缓存的输出

ASP.. NET web服务缓存

我不知道是否有更多的声明性方法,但你可以很容易地将结果缓存到常规缓存中,并检查参数是否为空,就像这样:

public SomeClass MyMethod(MyClass whatever) 
{
    if(whatever == null)
    {
        SomeClass result = Cache["MyMethodCache"] as SomeClass;
        if(result != null)
        return result;
    }

    //do something...
    if(whatever == null)
    {
         Cache.Add("MyMethodCache",something, ... ); //duration, expiration policy, etc.
    }
    return something; 
} 

然而,这个版本将需要序列化结果,即使它是通过缓存检索的。