/    Sign up×
Community /Pin to ProfileBookmark

Best practice for storing session values?

Hi everyone, first time posting here.

[SIZE=4]The problem:[/SIZE]
I’ve got a messy site that I’m supposed to make faster and a big culprit seems to be the way it’s handling session values. Right now it has a bunch of functions like GetDeliveryType() and SetDeliveryType() and instead of retrieving the values once, it will do GetDeliveryType() every single time that value is needed in the code. The problem is these are doing [B]synchronous[/B] ajax calls (because most need to return the value to the code that is calling it) like so:

[CODE]
function SetDeliveryType(p_deliverytype) {
$.ajax({
type: “GET”,
url: “/cart/setCartDeliveryType”,
data: { deliverytype: p_deliverytype },
async: false,
success: function (response) {
//stores = JSON.parse(response);
//alert(response)
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(“Exception in /order/SetDeliveryType : ” + errorThrown);
}
});
}[/CODE]

So I have blocking ajax calls everywhere firing potentially dozens of times per page load. As you might guess, this is horribly slow.

[SIZE=4]
The solution?[/SIZE]

I’m a junior developer and I’m having a hard time finding info on the best way to solve this. Ideally I would like to

  • 1. Do away with all the ajax calls, just set the variables as global or near global js variables and access everything in native code. Is that possible? Or will I get stuck with no way to access them when the user goes to the next page or some other caveat?
  • failing that I would like to
    2. Get values only once and then store them as variables and access them in native code. Is that viable?

    or
    3. Session storage or local storage? How would that work?

    Whatever the right way to approach this is, could I also get a few pointers on how to set it up and get started? It would be much appreciated.

    to post a comment
    JavaScript

    8 Comments(s)

    Copy linkTweet thisAlerts:
    @davejjjSep 18.2015 — Obviously the primary goal should be to reduce the number of Ajax calls to a small number. You are already using JSON to transfer collections of variables, so why are they making multiple calls?
    Copy linkTweet thisAlerts:
    @dlssoauthorSep 18.2015 — As a general rule each ajax call only retrieves one variable. As far as I understand they are making multiple calls because the person who built it didn't understand the consequences.

    You're suggesting then that 1. and 3. aren't viable options?
    Copy linkTweet thisAlerts:
    @SempervivumSep 18.2015 — When Ajax is used, the variables shurely are present on the server only. That means, that options 1. and 3. are not possible. Option 2. seems to be the best choice to me. And as davejjj already stated, it seems to be recommendable to get all variables by one Ajax call.
    Copy linkTweet thisAlerts:
    @wbportSep 18.2015 — Save variables as cookies?
    Copy linkTweet thisAlerts:
    @dlssoauthorSep 18.2015 — Ok so 2 is most popular so far: Try to limit to one ajax call and just pass the variables around in javascript to get where they are needed.

    How does it work if I save them s cookies? What are the pros and cons?
    Copy linkTweet thisAlerts:
    @SempervivumSep 18.2015 — The con for cookies is what I wrote before: When Ajax is used to access the variables it is probable that they are available on the server only.
    Copy linkTweet thisAlerts:
    @dlssoauthorSep 18.2015 — Maybe I wasn't clear. These are [B]session[/B] values. They are being [B]set[/B] by ajax calls as well. I believe all of them are set with ajax based on user action (usually information they entered or what stage of the checkout process they are in). They do not exist on the server before the user takes an action that sets them.
    Copy linkTweet thisAlerts:
    @SempervivumSep 19.2015 — They do not exist on the server before the user takes an action that sets them.[/QUOTE]Ah, I see. Then I have to revise my recommendation: The easiest way would be to save them in local storage or session storage as others already posted. In this way they are stored locally and no data transfer to/from the server is needed.
    ×

    Success!

    Help @dlsso spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.14,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...