I've created an umbraco site and I'm using it to control the content for an iOS app. The content is added via pages on the site (and created as nodes in Umbraco) and then returned to the app as json via calls to an UmbracoApiController.
I'm was wondering if there is any way to cache the data returned from calls to these methods? For eample the app will make a call to:
/umbraco/api/data/getnews?pageid=1
Is it possible to cache the result of a call to this URL rather than building the data again, as this function does some complex work which is identically for every call (not dependant on the user calling it).
We can then just decorate our method with something like [CacheOutput(ClientTimeSpan = 100, ServerTimeSpan = 100)].
It is also EXTREMELY!!! important that we correctly name the methods inside of the controller.
It seems that Get must be at the front of each method that we want to be accessible through a GET HTTP call. Further more, the name Get for a method is not enough, it should be followed by something else, for example GetData.
For some reason, having my method simply named Get allowed the CacheOutput to cache server side, but not client side.
Using the CacheOutput, I have managed to get calls to be cached with a 200 (cached), but not yet a 304. But this 200 does not contact the server, so as far as I am concerned, it works.
UmbracoApiController caching
Hi,
I've created an umbraco site and I'm using it to control the content for an iOS app. The content is added via pages on the site (and created as nodes in Umbraco) and then returned to the app as json via calls to an UmbracoApiController.
I'm was wondering if there is any way to cache the data returned from calls to these methods? For eample the app will make a call to:
/umbraco/api/data/getnews?pageid=1
Is it possible to cache the result of a call to this URL rather than building the data again, as this function does some complex work which is identically for every call (not dependant on the user calling it).
Thanks,
Adam
Hello,
You could try OutputCache or DonutOutputCache.
Jeroen
Hi Jeroen,
This is exactly what I'm looking for! My only issue now is I can't get either of them working...
I've installed DonutOutputCache using NuGet. Added the annotation as follows:
I've also made the request return a timestamp to check that it was indeed caching but the timestamp changes every time I call it.
Should this work by simply completing these 2 steps or am I missing something obvious?
Thanks,
Adam
Hello,
I'm also using DonutOutputCache in the Hybrid Framework. You can see the steps here:
1. The attribute with a cache profile: https://github.com/jbreuer/Hybrid-Framework-for-Umbraco-v7-Best-Practises/blob/master/Umbraco.Extensions/Controllers/ContactController.cs#L21
2. The cache profile in the web.config. varyByCustom is important: https://github.com/jbreuer/Hybrid-Framework-for-Umbraco-v7-Best-Practises/blob/master/Umbraco.Site/Web.config#L164
3. Register it in the global: https://github.com/jbreuer/Hybrid-Framework-for-Umbraco-v7-Best-Practises/blob/master/Umbraco.Extensions/Utilities/Global.cs#L13
4. Change the default global.asax file to inherit from your global: https://github.com/jbreuer/Hybrid-Framework-for-Umbraco-v7-Best-Practises/blob/master/Umbraco.Site/Global.asax#L1
That should be it.
Jeroen
Hi Jeroen,
I'm trying to do cache an UmrbacoApiController like Adam.
Neither the OutputCache or the DonutOutputCache seem to work for me.
Here is an example of my code:
This is being built in a core project where the DLLs are then being put into my Umbraco bins.
If I go to
/umbraco/api/my/get?id=1111
, I get the JSON result, but it is not cached.Here is the response header that is returned:
Can you help anymore? The example you provided above appears to be for an MVC template, not a surface controller.
Thanks.
Okay, I think I have figured this out.
OutputCache and DonutOutputCache cannot be used with the web API.
http://stackoverflow.com/questions/17287144/how-to-cache-net-web-api-requests-use-w-angularjs-http
Following the link above, we can use something called OutputCache: https://github.com/filipw/AspNetWebApi-OutputCache
We can then just decorate our method with something like
[CacheOutput(ClientTimeSpan = 100, ServerTimeSpan = 100)]
.It is also EXTREMELY!!! important that we correctly name the methods inside of the controller.
It seems that
Get
must be at the front of each method that we want to be accessible through aGET
HTTP call. Further more, the nameGet
for a method is not enough, it should be followed by something else, for exampleGetData
.For some reason, having my method simply named
Get
allowed the CacheOutput to cache server side, but not client side.Using the CacheOutput, I have managed to get calls to be cached with a 200 (cached), but not yet a 304. But this 200 does not contact the server, so as far as I am concerned, it works.
is working on a reply...