How do you pass “expensive” data from page to page

2019-07-17 17:57发布

问题:

I am doing my first ASP.NET MVC project. (In fact, for the record, this is my first production website of any kind).

This is a mobile web page targeting HTML 5.

This page looks up some "expensive" information. First it uses the html 5 geocoding feature to get the customers latlong from their browser.

I pass that information to a controller. That controller fills in the City and State (into a location model) using the Google Maps API and then uses it in the view.

So this data is expensive in two ways, first it takes time to look it up and second, in the case of the Google API, it is literally not free in that Google limits the number of calls that can be made per day.

I understand that MVC is designed to "embrace" the web including the fact that http is a stateless protocol.

Nevertheless, I would like to avoid having to fetch this expensive data on every page load for every endpoint that needs it. Furthermore, one other piece of state that I would like to keep track is the fact that the data has already been fetched.

What is the best way / best practice for achieving this on an MVC 3 web application? Since my model for location has 4 data point (lat long city state) and there is a fifth data point (data retrieved) I would really like to avoid using querystrings to do this or a route for all of those data points?

I am sure this is a common need but I honestly don't know how to tackle it. Any help will be appreciated.

Seth

回答1:

It Seems to me that you would like to cache the API call to google.

http://msdn.microsoft.com/en-us/library/18c1wd61(v=vs.71).aspx

You can store the object you got from google in cache and call it on the new controller event. you could also create another object that has the object from google and a bool that indicates if you have fetched the data or not.

It seem to me that the Cache would be your best bet.



回答2:

You can store it in session state if it is available in your case instead of passing between pages.



回答3:

Since this is "expensive" data, but still not to be persisted for a long time, you may:

  • use Session state
  • put the data in the Cache and either
    • set a cookie to enable the retrieval of the "expensive" data from cache
    • use a cache key which is unique to each query (lat long city state ?)
  • store the data ("data retrieved") on the client (since you do not seem to persist it on the server side)

My personal preference would be server side cache with a unique key.



回答4:

Store expensive data to the cache, and build cache ID by parameters you send to google, cache id should be unique for every distinct place



回答5:

Another option would be html5 storage. You will want to check to see if your target browsers support it though. The advantage to this approach is that the server does not have keep track of this data in session or a database - in fact the server doesn't know about client storage at all.



回答6:

try

Session[xxx]=xxx;

or

Application[xxx]=xxx;

instead