views:

17

answers:

2

I have a IHttpHandler that acts as a source to a jQuery Autocomplete input field. In the constructor of the handler I generate an index that remains fairly static between request (needs to be rebuilt maybe once a day).

How can I cache the the index between calls? Debugging indicates that the constructor is called for each request. I've set IsReusable to "false".

+1  A: 

Your handler is passed an HttpContext which means it has access to the application cache (e.g. context.Cache["Foo"] = myVal) and can save values there. However, you can also save the value in Session (e.g. context.Session["Bar"]) if your handler implements the IRequiresSessionState interface.

Thomas
A: 

HttpHandlers have a special propert IsReusable. You can override that property in your Handler implementation and set it to return True. IsReusable is a value indicating whether another request can use the IHttpHandler instance.

So first time when the handler is accessed the Handler instance is created and upon next request the same instance serves the request back - so your caching trick will work right in this case.

http://msdn.microsoft.com/en-us/library/system.web.ihttphandler.isreusable.aspx

this. __curious_geek