Copied to clipboard

Flag this post as spam?

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


  • Andrew Waegel 126 posts 126 karma points
    Jan 19, 2011 @ 07:58
    Andrew Waegel
    0

    Using cache in /base

    Hello,

    I'm using /base and loving it; makes a lot of sense.

    I'd like to cache some output and am wondering what the best route is - my base method is returning a large JSON string which takes some time to assemble from the database. I'd like to cache the output somehow and avoid building up the JSON each time.

    Is there a more elegant method than just writing the JSON out to a file and checking for its mod date?

  • Berto 105 posts 177 karma points
    Jan 20, 2011 @ 13:34
    Berto
    1

    Hi Andrew,

    You can use ASP.NET cache (Cache API), more info in here

    http://msdn.microsoft.com/en-us/library/aa478965.aspx

    The syntax is like the session or application (Cache scope is global)

    example:

    Cache["MyJsonResponse"] = listOfData.ToJson();

    For your case i think that sould be something like

    public static string TheJsonMethod(){
    if(Cache["MyJsonResponse"] == null || booleanToUpdateCache == true){
    Cache["MyJsonResponse"] = MyData.ToJson();
    }
    return Cache["MyJsonResponse"];
    }

     

     

  • Andrew Waegel 126 posts 126 karma points
    Jan 20, 2011 @ 18:20
    Andrew Waegel
    0

    That should work- thanks!

  • Berto 105 posts 177 karma points
    Jan 20, 2011 @ 18:25
    Berto
    2

    I forgot...

    In /base, you have to use HttpContext.Current.Cache["NameOfTheCache"]

    ;)

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies