0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.