Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebMethod CacheDuration - Avoid Caching Bad Results

How can you avoid caching an error result with ASP/ASMX WebMethod with a 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 say the account ID is an expired account, the answer to those 3 parameters would be "Error: Expired Account". If you have a cache duration of 10 minutes, if the account gets fixed in 1 minute, and that method is called again within 10 minute expiration period (with same 3 parameters), doesn't it return the error message that is cached?

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

like image 881
John Cruz Avatar asked Aug 06 '26 05:08

John Cruz


1 Answers

Instead of relying on CacheDuration property you can do caching by yourself in webmethod. In that case you will have full control.

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

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

like image 72
Prakash Avatar answered Aug 07 '26 19:08

Prakash



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!