gpt4 book ai didi

c# - 如何在 ASP.NET 中跨多个 session 存储(缓存?)数据

转载 作者:行者123 更新时间:2023-11-30 19:57:10 25 4
gpt4 key购买 nike

我正在编写 C# ASP.NET Web 窗体应用程序。为简单起见,此应用程序从服务器检索数据并将其显示给用户。

要访问此服务器,我的应用程序需要生成登录信息。生成登录信息需要几分钟时间。此登录信息每天都会更改,但对于使用我的应用程序的每个人(在特定的一天)来说都是一样的。

我的应用程序每天将被数百人使用。让每个用户等待几分钟来检索登录信息对我来说很愚蠢。

理想情况下,我想每天收集一次登录信息,并将结果存储在我的服务器上的某个位置。

有什么建议吗?我知道 .NET 有 CacheSession,但它们只为单个用户存储数据。您是否有任何技术来存储此登录信息以便我可以从我的代码隐藏中获取它?

最佳答案

你错了,Cache 工作在 Application 层,而不是 User 层,来自 MSDN

An application can often increase performance by storing data in memory that is accessed frequently and that requires significant processing time to create. For example, if your application processes large amounts of data using complex logic and then returns the data as a report accessed frequently by users, it is efficient to avoid re-creating the report every time that a user requests it. Similarly, if your application includes a page that processes complex data but that is updated only infrequently, it is inefficient for the server to re-create that page on every request.

To help you increase application performance in these situations, ASP.NET provides caching using two basic caching mechanisms. The first is application caching, which allows you to cache data you generate, such as a DataSet object or a custom report business object. The second is page output caching, which saves the output of page processing and reuses the output instead of re-processing the page when a user requests the page again.

我认为 Application cache 比另一个更合适

关于过期

有两种控制方式,你可以设置X分钟为限制,然后如果你想设置:

  1. 绝对:自对象创建后 X 分钟后数据过期

    Cache.Insert("key", myTimeSensitiveData, null, DateTime.Now.AddMinutes(1), TimeSpan.Zero); 

Absolute Expiration: This example will cache the time sensitive data for one minute, at which point the cache will expire. Note that absolute expiration and sliding expiration (below) cannot be used together.

  1. 滑动:自上次用户使用缓存对象后 X 分钟后使数据过期

    Cache.Insert("key",myFrequentlyAccessedData, null, System.Web.Caching.Cache.NoAbsoluteExpiration,  TimeSpan.FromMinutes(1));

Sliding Expiration: This example will cache some frequently used data. The data will remain in the cache until one minute passes without anything referencing it. Note that sliding expiration and absolute expiration cannot be used together.

示例来源:MSDN: Caching API, Using the Cache Object

关于c# - 如何在 ASP.NET 中跨多个 session 存储(缓存?)数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31278914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com