9

First off I'd like to mention I'm an intern with little prior experience in the technical world so I am sorry if the answer to my question seems obvious. I have tried to find an answer for a couple days now and have little to show for it.

My project has been to develop a Phone Directory. What I am trying to do now is have a frequently searched box that shows the top ten searched names since the user started using the app. I am planing to make a JSON object with the following information and then client-side cache the result:

var myJSONObject = {"searched": [       {"firstname":firstName,"lastname":lastName,"PK":pk,"phone":phonenumber,"email":emailaddress,"bu":businessunit,"company":company,"building":building, "fax":fax, "timesvisited":"accumulator to keep track of visits to the name"}

] };

my head ache right now is figuring out how to cache the JSON object. I created a manifest file and I know how to cache certain files such as a css file, but how do you cache something such as the code listed above specifically so that the data is stored and can be brought up until the page on a second visit?

2
  • There's no such thing as a... ah, forget it :) Commented Aug 2, 2011 at 22:36
  • haha.. like i said, i'm VERY new to this... but your right. Probably a good thing to let it go ;). Commented Aug 3, 2011 at 20:39

2 Answers 2

9

You could use Local Storage, which is supported in Firefox 3.5, IE 8, and Chrome 4 and up. To save the cache to the local storage:

localStorage['jsoncache'] = JSON.stringify(myJSONObject);

To retrieve it:

myJSONObject = JSON.parse(localStorage['jsoncache']);

Notice how you can only store strings, not any kind of object, in local storage. You can change the jsoncache key to something else if you wish.

Sign up to request clarification or add additional context in comments.

3 Comments

Would jsoncache be a .txt file in the same directory in this case?
It's implementation-defined as to how the local storage is stored physically. I'd assume that most browsers would use a database of some sort as it'd be faster. In any case, it's unlikely that browsers would use one text file, per key, per page.
my json object is of 15 MB , is there any other cache available ?
4

I'm not completely how you create, maintain and retrieve the object, but If you load it via Ajax, you can use HTTP caching.

If you want to store it only locally then you can use localStorage (only newer browsers support that):

// Serialize and store
localeStorage['topten'] = JSON.stringify(myJSONObject)

// Retrieve and deserialize
var myJSONObject = JSON.parse(localeStorage['topten'])

1 Comment

Thank you :). Your answer and the one above clarified this greatly for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.