WebMethod CacheDuration - avoid caching bad results

How do I avoid caching the error result using ASP / ASMX WebMethod with the CacheDuration property?

Let's say you have a WebMethod that adds two numbers (so it has two number parameters) and also has an account ID parameter. If you claim that the account ID is a legacy account, the answer to these 3 parameters is "Error: Account Expired". If you have a cache duration of 10 minutes, if the account commits in 1 minute and this method is called again within a 10 minute expiration (with the same 3 parameters), doesn't it return an error message that is cached?

Is there a way to avoid caching the result if it's an error message?


source to share

1 answer

Instead of relying on the CacheDuration property, you can do the caching yourself in the webmethod. In this case, you have complete control.

You can create a unique cache key based on the input parameter and store the result in the cache (for a specific duration) if there is no error.

So when there is an error it means there is no cache, so you check again if the account is valid. And put the result in the cache if the account is valid.



All Articles