简体   繁体   中英

How do you pass “expensive” data from page to page using ASP.NET MVC 3?

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

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.

如果在您的情况下可用,则可以将其存储在会话状态中,而不是在页面之间传递。

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.

将昂贵的数据存储到缓存中,并通过发送给google的参数构建缓存ID,缓存ID对于每个不同的位置应该是唯一的

try

Session[xxx]=xxx;

or

Application[xxx]=xxx;

instead

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM