2011-10-07 65 views
3

我之前使用应用程序对象来缓存永不改变的数据。我正在重写这个项目,并且发现应用程序对象是一个禁止对象,并且只是为了支持来自ASP的传统支持。散列表中的asp.net静态缓存

我知道我也可以使用缓存 - 但我不想因为我使用那些需要失效的数据。

因此,我正在寻找静态数据的静态变量(有意义)。

我的问题是,我没有在每个类中指定静态变量,而是考虑使用散列表来存储所有数据并将其包装到自己的类中 - 就像工厂一样。

事情是这样的:

''' <summary> 
''' Methods and properties related to the access and management of the local static memory cache. 
''' Fastest type of cache but not available across applications, web farm or web garden environments. 
''' Use this cache when data is static or can be stale across application instances. 
''' </summary> 
''' <remarks></remarks> 
Public Class LocalStaticCache 

    'Internal data holder: 
    Private Shared _objCache As Hashtable = Hashtable.Synchronized(New Hashtable) 

    Private Sub New() 
    End Sub 

    ''' <summary> 
    ''' Gets or sets an object in cache. Returns Nothing if the object does not exist. 
    ''' </summary> 
    ''' <param name="key">The name of the object.</param> 
    ''' <returns></returns> 
    ''' <remarks></remarks> 
    Public Shared Property Item(key As String) As Object 
     Get 
      If String.IsNullOrEmpty(key) Then Return Nothing 
      Return _objCache(key) 
     End Get 
     Private Set(value As Object) 
      _objCache(key) = value 
     End Set 
    End Property 

    ''' <summary> 
    ''' Insert an object into the cache. 
    ''' </summary> 
    ''' <param name="key">The unique object key.</param> 
    ''' <param name="value">The object to store in the cache.</param> 
    ''' <remarks></remarks> 
    Public Shared Sub Insert(key As String, 
          value As Object) 
     If Not String.IsNullOrWhiteSpace(key) Then 

      If _objCache.ContainsKey(key) Then 
       'If the key already exists in the Cache it will overwrite only if the objects differ: 
       Interlocked.CompareExchange(Item(key), value, value) 
       Return 
      End If 

      'store the item to the cache: 
      Item(key) = value 
     End If 
    End Sub 

    ''' <summary> 
    ''' Remove an object from the cache. 
    ''' </summary> 
    ''' <param name="key">The key of the object to remove.</param> 
    ''' <remarks></remarks> 
    Public Shared Sub Remove(key As String) 
     If _objCache.ContainsKey(key) Then 
      _objCache.Remove(key) 
     End If 
    End Sub 

End Class 

你想存储所有静态数据装入一个哈希表,这是对性能, 一个好主意,或者这将是更好地为每个类有内部自身的静态数据持有者他们的课?

此线程是否安全?注意:我正在实施Hashtable.Synchronized和Interlocked.CompareExchange以防止竞争条件 - 但锁定和争用怎么办?

请注意,数据在第一次设置后永远不会更改(哈希表中的项目永远不需要更新)。

我有数据集和大块的记录集作为内存流来存储。

任何想法或指针?

谢谢。

+1

嗯,asp.net有它自己的缓存设施,为什么不使用它们?看看这个 - http://msdn.microsoft.com/en-us/library/6hbbsfk6.aspx –

+0

或者如果你不想参考asp.net http://msdn.microsoft.com/en-us/library /dd997357.aspx –

+0

你存储什么“数据”?如果它是相同类型,我会建议使用类型安全字典。在任何地方铸造对象的需求也是将这些静态对象封装在它们所属的类中的强类型的一个很好的理由。这使得代码更易读,并且可以更好地记录对象的用途。但是一般使用静态对象的方法对于使用量大而且从不改变的数据来说是好的,尽管我更喜欢Cache。 –

回答

1

不用编写自己的静态散列表并重新实现应用程序,而是直接使用应用程序存储。

另一种选择是使用ASP.NET缓存对象,因为可以在不使用数据时删除数据。

添加和删除Hashtable中的数据是线程安全的(仅从一个线程写入)。如果您在Application_Start上初始化数据,则不必使用任何锁,因为数据不会更改。

如果数据永远不应该被删除,我会根据数据的上下文将数据存储在不同的类中。为了这个目的,使用几个带有惰性初始化的单例类。

对于新的应用程序,我不再使用数据集(或记录集?)。你最好使用实体框架,你可以基本上生成你的数据访问层。

希望这会有所帮助。

+0

具有延迟初始化的单例类是一个好主意,我没有想到这一点。我想我只是在懒惰,想把每一样东西都包装进一个班级。 –