简体   繁体   English

在asp.net中缓存?

[英]Caching in asp.net?

Recently a question raised in my group. 最近我的小组提出了一个问题。 I have a sections list in database.it is nothing but names of sections like Dubai section, NA section, Canada Section. 我在数据库中有一个部分列表。它只是迪拜部分,NA部分,加拿大部分的名称。 This list updated very rarely in the database. 此列表在数据库中很少更新。

Now the question, Is there any mechanism in asp.net that we can keep the list in Server Cache memory and refresh the cache every 24 hours? 现在的问题是,在asp.net中是否有任何机制可以将列表保存在服务器缓存内存中并每24小时刷新一次缓存? Why group need is, they dont want call DB all the time. 为什么群体需要,他们不想一直呼叫DB。 Just they want read it from Server memory. 只是他们想从服务器内存中读取它。

Please share me if we have that kind of facility in asp.net. 如果我们在asp.net中拥有这种设施,请与我分享。

examples and blogs will be more helpful. 示例和博客将更有帮助。

I would create a user control just for the purpose of displaying this list of Sections and use the outputcache directive on the control: 我只是为了显示这个Sections列表而创建一个用户控件,并在控件上使用outputcache指令:

On the first line of this user control, have something like: 在此用户控件的第一行,有类似于:

<% @ OutputCache Duration="86400" VaryByParam="none" %> 

*Duration is given on seconds above. *持续时间以秒为单位。

You could use the application cache . 您可以使用应用程序缓存 eg 例如

// Add an object with a cache of 1 day.
Cache.Insert("CacheItem6", "Cached Item 6",
    null, DateTime.Now.AddDays(1), 
    System.Web.Caching.Cache.NoSlidingExpiration);

Straight from MSDN -- Caching . 直接来自MSDN - 缓存

Then to retrieve your value, you could implement the following: 然后,要检索您的值,您可以实现以下内容:

string cachedString;
cachedString = (string)Cache["CacheItem6"];

You can also get a notification when the object is removed . 您还可以在删除对象时收到通知。

You can do that with Cache object. 您可以使用Cache对象执行此操作。 More info here . 更多信息在这里

You probably need something like this: 你可能需要这样的东西:

Cache.Insert("DSN", connectionString, null, DateTime.Now.AddHours(24), Cache.NoSlidingExpiration);

This will cache connectionString object under a key named DSN for 24 hours. 这将在名为DSN的密钥下缓存connectionString对象24小时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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