简体   繁体   English

在asp.net中实现缓存的合适方法

[英]Suitable method to implement caching in asp.net

I need to implement caching in Asp.net web application My need to store data with different ID's. 我需要在Asp.net Web应用程序中实现缓存我需要存储具有不同ID的数据。 So which method is better ? 那么哪种方法更好?

  1. Use a dictionary variable. 使用字典变量。 Insert the data (key as ID and data as value). 插入数据(键作为ID,数据作为值)。

      Dim mDict As New Dictionary(Of String, String) mDict .Add(bID, uwtTree.WriteXmlString(True, True)) Cache.Insert("mTree", mDict) 

    Add it to a cache variable. 将其添加到缓存变量。 Access the cache variable 访问缓存变量

      If Not Cache("mTree") is Nothing Then 'cast to dictionary and check ID exists , if exsitis get the data End iF 
  2. Use cache variable for different IDs 将缓存变量用于不同的ID

      Cache.Insert(ID,data) ' each insertion for each ID If Not Cache(ID) is Nothing Then ' get the data. ' End IF 

Which method is the best way ? 哪种方法最好? Or is there any other method exists ? 或者是否存在其他方法? I am using .Net 3.5 /IIS 7 (VB.Net). 我使用.Net 3.5 / IIS 7(VB.Net)。 Thanks in advance 提前致谢

Way to Improve Performance and Memory Optimization 提高性能和内存优化的方法

Without context it's not possible to say which is "better". 没有背景,就不可能说哪个是“更好”。

But if you put a dictionary in the cache (option 1), better make sure it's a thread-safe dictionary (such as the .NET 4 ConcurrentDictionary). 但是如果你把一个字典放在缓存中(选项1),最好确保它是一个线程安全的字典(比如.NET 4 ConcurrentDictionary)。

The most obvious difference between the two approaches is that with option 1, cache item expiry will result in the dictionary with all items being removed at once. 两种方法之间最明显的区别是,对于选项1,缓存项目到期将导致字典中一次删除所有项目。 With option 2, individual items will expire independently. 使用选项2,单个项目将独立到期。

In response to the comment: 回应评论:

i am having xml data and i will store in cache (data caching) as string. 我有xml数据,我将作为字符串存储在缓存(数据缓存)中。 Is there any difference if i store it as XmlObject ? 如果我将它存储为XmlObject有什么区别吗?

The main differences I see are: 我看到的主要区别是:

  • String is immutable, so you won't need to worry about thread-safety when accessing it. 字符串是不可变的,因此访问它时无需担心线程安全。 XML objects are not immutable - so you need to make sure you don't modify the object retrieved from the cache (or use locks to make sure any such modification is thread-safe). XML对象不是不可变的 - 因此您需要确保不修改从缓存中检索的对象(或使用锁来确保任何此类修改是线程安全的)。

  • If you store a string, you will presumably parse it into an XML object each time you retrieve it from the cache, which will result in a potential performance penalty. 如果存储字符串,则每次从缓存中检索字符串时,可能会将其解析为XML对象,这将导致潜在的性能损失。

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

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