5

I have a site that is being polled rather hard for the JSON representation of the same resources from multiple clients (browsers, other applications, unix shell scripts, python scripts, etc).

I would like to add some caching such that some of the resources are cached within the server for a configurable amount of time, to avoid the CPU hit of handling the request and serialising the resource to JSON. I could of course cache them myself within the handlers, but would then take the serialisation hit on every request, and would have to modify loads of handlers too.

I have looked at the openrasta-caching module but think this is only for controlling the browser cache?
So any suggestions for how I can get openrasta to cache the rendered representation of the resource, after the codec has generated it?

Thanks

Neil Mosafi
  • 460
  • 1
  • 4
  • 14
  • Should add that it would be nice to able to invalidate the cache programmatically, as almost all the updates to the resources are coming in via handlers – Neil Mosafi Oct 09 '12 at 15:07
  • Which version of .net are you working with? – JPReddy Oct 10 '12 at 14:20
  • I am working with .net 4.0.. why? – Neil Mosafi Oct 10 '12 at 18:15
  • May be you should use .net 4.0 caching module. http://msdn.microsoft.com/en-us/library/dd997357.aspx – JPReddy Oct 10 '12 at 21:07
  • Yes that could form part of the solution. The question is more how to actually integrate this into OpenRasta, or seeing if there is anything already out there – Neil Mosafi Oct 10 '12 at 21:16
  • We tried finding if there are any and no success, that's the reason we used in built .net library. If you find any that would also help us too. :) – JPReddy Oct 11 '12 at 01:20

1 Answers1

1

openrasta-caching does have preliminary support for server-side caching, with an API you can map to the asp.net server-side cache, using the ServerCaching attribute. That said it's not complete, neither is openrasta-caching for that matter. It's a 0.2 that would need a couple of days of work to make it to a good v1 that completely support all the scenarios I wanted to support that the asp.net caching infrastructure doesn't currently support (mainly, make the caching in OpenRasta work exactly like an http intermediary rather than an object and .net centric one as exists in asp.net land, including client control of the server cache for those times you want the client to be allowed to force the server to redo the query). As I currently have no client project working on caching it's difficult to justify any further work on that plugin, so I'm not coding anything for it right now. I do have 4 days free coming up though, so DM me if you want openrasta-caching to be bumped to 0.3 with whatever requirements you have in that that would fit 4 days of work.

You can implement something simpler using an IOperationInterceptor and plugging in the asp.net pipeline using that, or be more web-friendly and implement your caching out of process using squid and rely on openrasta-caching for generating the correct http caching instructions.

That said, for your problem, you may not even need server caching if the cost is in json. If you map a last modified or an Etag to what the handler returns, it'll generate correctly a 304 where appropriate, and bypass json rendering alltogether, provided your client does conditional requests (and it should).

There's also a proposed API to allow you to further optimize your API by doing a first query on last modified/etag to return the 304 without retrieving any data.

SerialSeb
  • 6,701
  • 24
  • 28