Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Adam Hill 7 posts 27 karma points
    Mar 25, 2015 @ 12:46
    Adam Hill
    0

    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

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Mar 25, 2015 @ 14:57
    Jeroen Breuer
    0

    Hello,

    You could try OutputCache or DonutOutputCache.

    Jeroen

  • Adam Hill 7 posts 27 karma points
    Mar 25, 2015 @ 17:41
    Adam Hill
    0

    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:

    [DonutOutputCache(Duration = 3000)]
    public Result GetNews(int pageId = 1)

    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 

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Mar 25, 2015 @ 17:48
  • Harvey 28 posts 122 karma points
    Jan 15, 2016 @ 11:12
    Harvey
    0

    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:

    namespace Project.Core.Controllers.SurfaceController
    {
        public class MyController : UmbracoApiController
        {
            [DonutOutputCache(Duration = 86400, Location = OutputCacheLocation.Client)]
            public List<CustomObject> Get(int id)
            {
                return new CustomObject();
            }
        }
    }
    

    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: enter image description here

    Can you help anymore? The example you provided above appears to be for an MVC template, not a surface controller.

    Thanks.

  • Harvey 28 posts 122 karma points
    Jan 15, 2016 @ 12:46
    Harvey
    0

    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 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.

Please Sign in or register to post replies

Write your reply to:

Draft