.ashx 处理程序中的视图状态?视图、状态、程序、ashx

2023-09-07 08:40:42 作者:貓巷.

我有一个处理程序(例如 list.ashx),它有一个检索大型数据集的方法,然后只抓取将显示在任何给定数据页面"上的记录.我们允许用户对这些结果进行排序.因此,在任何给定的页面运行中,我将检索几秒钟/几分钟前刚刚获得的数据集,但对它们进行重新排序,或显示下一页数据等.

I've got a handler (list.ashx for example) that has a method that retrieves a large dataset, then grabs only the records that will be shown on any given "page" of data. We are allowing the users to do sorting on these results. So, on any given page run, I will be retrieving a dataset that I just got a few seconds/minutes ago, but reordering them, or showing the next page of data, etc.

我的意思是我的数据集确实没有改变.通常,数据集会卡在页面的视图状态中,但由于我使用的是处理程序,所以我没有那种便利.至少我不这么认为.

My point is that my dataset really hasn't changed. Normally, the dataset would be stuck into the viewstate of a page, but since I'm using a handler, I don't have that convenience. At least I don't think so.

那么,在使用处理程序时,存储与当前用户给定页面关联的视图状态的常用方法是什么?有没有办法获取数据集,以某种方式对其进行编码并将其发送回用户,然后在下一次调用时将其传回,然后从这些位中重新水化数据集?

So, what is a common way to store the viewstate associated with a current user's given page when using a handler? Is there a way to take the dataset, encode it somehow and send that back to the user, and then on the next call, pass it back and then rehydrate a dataset from those bits?

我认为 Session 不是存储它的好地方,因为我们可能有 1000 个用户都查看不同数据的不同数据集,这可能会使服务器瘫痪.至少我是这么认为的.

I don't think Session would be a good place to store it since we might have 1000 users all viewing different datasets of different data, and that could bring the server to its knees. At least I think so.

有没有人遇到过这种情况,能给我一些建议吗?

Does anyone have any experience with this kind of situation, and can you give me any advice?

推荐答案

在这种情况下,我会使用具有某种类型的用户和查询信息作为键的缓存.原因是您说它是一个大型数据集.没错,有些东西你不想不断地上下推动.请记住,如果数据处于 ViewState 中,您的服务器仍然必须接收数据并进行处理.我会做这样的事情,它会为特定用户缓存它并且有效期很短:

In this situation I would use a cache with some type of user and query info as the key. The reason being is you say it is a large dataset. Right there is something you don't want to be pushing up and down the pipe constantly. Remember your server still has to received the data if it is in ViewState and handle it. I would do something like this which would cache it for a specific user and have a short expiry:

public DataSet GetSomeData(string user, string query, string sort)
{
    // You could make the key just based on the query params but figured
    // you would want the user in there as well.
    // You could user just the user if you want to limit it to one cached item
    // per user too.
    string key = string.Format("{0}:{1}", user, query);

    DataSet ds = HttpContext.Current.Cache[key] as DataSet;
    if (ds == null)
    {
        // Need to reload or get the data
        ds = LoadMyData(query);

        // Now store it and make the expiry short so it doesn't bog up your server
        // needlessly... worst case you have to retrieve it again because the data
        // has expired.
        HttpContext.Current.Cache.Insert(key, ds, null,
            DateTime.UtcNow.AddMinutes(yourTimeout), 
            System.Web.Caching.Cache.NoSlidingExpiration);
    }

    // Perform the sort or leave as default sorting and return
    return (string.IsNullOrEmpty(sort) ? ds : sortSortMyDataSet(ds, sort));
}

当您说 1000 个用户时,是指并发用户吗?如果您的到期时间是 1 分钟,那么有多少并发用户会在一分钟内进行该调用并需要排序.我认为将数据卸载到类似于 ViewState 的东西只是用一些缓存来换取带宽和来回处理大型请求的负载.在我看来,来回传输的次数越少越好.

When you say 1000's of users, does that mean concurrent users? If your expiration time was 1 minute how many concurrent users would make that call in a minute and require sorting. I think offloading the data to something like similar to ViewState is just trading some cache memory for bandwidth and processing load of larget requests back and forth. The less you have to transmit back and forth the better in my opinion.