2011-11-14 126 views
0

只有当null作为参数传递时,是否可以缓存函数输出? 事情是这样的:ASP.NET web服务缓存

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

所以当任何== NULL函数返回缓存的输出,而当它不为null,则没有缓存它生成的输出。

回答

1

我不知道是否有更多的声明方法,但你可以很容易地缓存结果在通常的高速缓存,并检查参数为null这样的:

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; 
} 

不过这个版本将需要序列每次甚至通过缓存检索结果。