当启用缓存为 true 时,不会为 ObjectDatasource 引发选定的事件

发布于 2024-08-23 03:36:54 字数 1139 浏览 4 评论 0原文

我有一个使用 ODS(ObjectDataSource) 来获取数据的 Gridview 控件。为了获得最佳性能和效率,我关闭了 Gridview 的视图状态(即 EnableViewstate =“false”)。

并且我还在关联的 Objectdatasource 中启用了缓存。这消除了多达 50-60% 的性能优化,因为它消除了数据库往返......礼貌 ODS 缓存

因此,在此之后我陷入了著名的“ODS 排序”问题,但我设法为其发明了一个棘手的解决方案并且工作正常:

优化分页和排序使用 EnableCaching = true 的 ObjectDataSource 进行排序

下分页,它也工作正常。现在,我需要在 Gridview 顶部显示“总记录:X”。好吧,我部署了以下方法:

protected void ods_Selected(object sender, ObjectDataSourceStatusEventArgs e)
        {
            if(e.ReturnValue != null && e.ReturnValue.GetType() == typeof(int))
                base.setTotalLabel(lblTotal, e.ReturnValue);
        }

不要混淆 - base.setTotalLabel 是我自己的方法,用于设置标签文本与计数。这也工作正常,但问题是 -

每当 ODS 从以下位置获取数据时 它的缓存不会触发 ODS_Selecting 或 ODS_Select 事件。 它们只是被“绕过”,因为它 从缓存中获取数据。这就是当我 刷新总记录失败 数数!

我希望我已经很好地解释了我的问题,这很棘手。我准备为此做任何技巧或肮脏的编码,因为我想维护 ODS 缓存,并且我不能仅仅因为一些偶然的“错误更新”而回滚更改。

请帮忙!

I've a Gridview control using an ODS(ObjectDataSource) to fetch data. For the best performance and efficiency, I've turned-off the view state of Gridview (i.e. EnableViewstate = "false".

And I've also enabled caching in the associated Objectdatasource. This eliminates as much as 50-60% performance optimization because it eliminates the DB round-trip .. courtesy ODS Caching.

So, after this I got stuck into the famous "ODS sorting" issue but I managed to invent a tricky solution for it and its working fine:

Optimize Pagination & Sorting with ObjectDataSource having EnableCaching = true

Next pagination, it is also working fine. Now, I need to display "Total records: X" at the top of the Gridview. Well, I deployed the following method:

protected void ods_Selected(object sender, ObjectDataSourceStatusEventArgs e)
        {
            if(e.ReturnValue != null && e.ReturnValue.GetType() == typeof(int))
                base.setTotalLabel(lblTotal, e.ReturnValue);
        }

Don't confuse - base.setTotalLabel is my own method to set the label text with the count. This is also working fine but the issue is that -

Whenever, the ODS fetches data from
its Cache it won't trigger the
ODS_Selecting or ODS_Select events.
They are simply "by-passed" because it
takes data from cache. This is when I
fail to refresh the Total records
count!

I hope I've explained my problem good, this is tricky. I'm ready to do any trick or dirty coding for this because I want to maintain the ODS-caching and I can't rollback changes just because of a few incidental "mis-updates".

Pls help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

○闲身 2024-08-30 03:36:54

这对我有用,我设置了 CacheKeyDependency,并且每当我想刷新时我都会将其显式设置为新值。如果我想显式刷新,我会在查询字符串中传递 &refresh=1 。

在 .aspx 中 -

<asp:ObjectDataSource ID="odsOrg" runat="server" SelectMethod="SearchOrg" ... EnableCaching="true" CacheDuration="60" CacheKeyDependency="key1">

在代码隐藏中 -

public const string argRefresh = "refresh=1";
public void CheckExpiredCacheKey(string ckey)
{
 if (Cache[ckey] == null) 
  Cache[ckey] = new object(); //Set Cache key which will b used to manually expire ODS cache     

if (!IsPostBack && Request.QueryString["refresh"] == "1")//check refresh flag
 {
    //Cache.Remove(ckey);//NOT NEEDED: use the following instead
    Cache[ckey] = new object();// Needed otherwise it'll call Grid-populate twice
 } 
}

This worked for me I set the CacheKeyDependency and I explicitly set it to a new value whenever I want to refresh. I pass &refresh=1 in querystring if I want to explicitly refresh.

In .aspx -

<asp:ObjectDataSource ID="odsOrg" runat="server" SelectMethod="SearchOrg" ... EnableCaching="true" CacheDuration="60" CacheKeyDependency="key1">

In code behind -

public const string argRefresh = "refresh=1";
public void CheckExpiredCacheKey(string ckey)
{
 if (Cache[ckey] == null) 
  Cache[ckey] = new object(); //Set Cache key which will b used to manually expire ODS cache     

if (!IsPostBack && Request.QueryString["refresh"] == "1")//check refresh flag
 {
    //Cache.Remove(ckey);//NOT NEEDED: use the following instead
    Cache[ckey] = new object();// Needed otherwise it'll call Grid-populate twice
 } 
}
沫离伤花 2024-08-30 03:36:54

我有完全相同的问题吗?

我一直在做的一个解决方案是在 gridview 数据绑定方法中再次调用 select count 方法(通过 BLL)并将其存储在 viewstate var 中,然后使用它来显示记录数。但这会导致两件事:

  • 对数据库计数 sp 的另一次调用(违背缓存目的)
  • 呈现冲突,即显示的缓存记录数可能与实际数量(最新的)不匹配

I have the exact same problem?

A solution I have been doing is to call the select count method again (via BLL) in the gridview databinding method and storing this in a viewstate var then using it to display the number of records. But this leads to two things:

  • another call to the DB count sp (defeats caching purpose)
  • conflict of presentation i.e. number of cached records displayed may not match the actual number(which is up-to-date)
冷心人i 2024-08-30 03:36:54

我通过在选定事件期间将记录计数存储在会话变量中解决了这个问题:

protected void odsSearchResults_Selected(object sender, ObjectDataSourceStatusEventArgs e)
    {
        DataTable resultsTable = e.ReturnValue as DataTable;
        if (resultsTable == null)
        {
            return;
        }
        Session["ResultCount"] = resultsTable.Rows.Count;
        lblRecordCount.Text = resultsTable.Rows.Count.ToString();
    }

然后在 Page_Load 中,如果会话变量不为空,我将显示记录计数:

if(Session["ResultCount"] != null)
{
    lblRecordCount.Text = Session["ResultCount"].ToString();
}

现在,如果数据来自缓存,则 lblRecordCount 将显示记录计数

I solved this by storing the record count in a session variable during the selected event:

protected void odsSearchResults_Selected(object sender, ObjectDataSourceStatusEventArgs e)
    {
        DataTable resultsTable = e.ReturnValue as DataTable;
        if (resultsTable == null)
        {
            return;
        }
        Session["ResultCount"] = resultsTable.Rows.Count;
        lblRecordCount.Text = resultsTable.Rows.Count.ToString();
    }

Then in Page_Load I display the record count if the session var is not null:

if(Session["ResultCount"] != null)
{
    lblRecordCount.Text = Session["ResultCount"].ToString();
}

Now lblRecordCount will show the record count if the data comes from the cache or not

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文