redman
redman

Reputation: 2165

ASP.NET web service Cache

Is it possible to cache function output only when null is passed as a parameter? Something like this:

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

So when whatever == null the function return cached output, and when it's not null it generates output without caching it.

Upvotes: 0

Views: 1608

Answers (1)

Stilgar
Stilgar

Reputation: 23551

I don't know if there is more declarative method but you can easily cache the result in the regular cache and check if the argument is null like this:

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

However this version will need to serialize the result each time even it is retrieved through cache.

Upvotes: 1

Related Questions