Brian Masters
Brian Masters

Reputation: 67

Can I have more than one localstorage instance?

I have some code to store cued Ajax requests in localstorage that works well with one key per request. I'm also storing other persistent data in the same place - I've separated this by using a unique key prefix so that this doesn't interfere with my Ajax cue.

I now have need to store yet further data and I think it's getting messy.

Is there some way of having multiple localstorage instances?

Upvotes: 1

Views: 1493

Answers (1)

SeanLi
SeanLi

Reputation: 194

You can't have multiple localStorage instances, you can't instantiate it. You don't have to separate out every single bit of data with unique key prefix if they can be logically grouped together. As long as those data can be serialized you can do something like this:

var requests = {reqId2 : reqObj, reqId2 : reqObj};
localStorage.setItem("ajax_requests", JSON.stringify(requests));

// then when you want to add additional requests into localStorage
var requests = JSON.parse(localStorage.getItem("ajax_requests"));
requests[reqId] = reqObj;

Just remember to store all data that can be logically grouped together, it should help you with the organization.

Upvotes: 2

Related Questions