you are in: codestackercodes [RSS] → tag: cache [RSS]

Cache em VB.Net Delicious

Conjunto de classes para encapsular as operações de inserção, atualização, remoção e busca no cache do Asp.Net, podendo ser adicionados novos gerenciadores de cache, como o Memcache, por exemplo.

show/hide lines
   1  Imports System.Web
   2  
   3  ''' ---------------------------------------------------
   4  ''' Interface para as classes de cache.
   5  ''' ---------------------------------------------------
   6  Public Interface ICache
   7  
   8      Function GetObject(Of T)(ByVal key As String) As T
   9  
  10      Sub Insert(Of T)(ByVal key As String, ByVal value As T)
  11  
  12      Sub Insert(ByVal key As String, ByVal value As Object)
  13  
  14      Sub Update(Of T)(ByVal key As String, ByVal value As T)
  15  
  16      Sub Update(ByVal key As String, ByVal value As Object)
  17  
  18      Sub Remove(ByVal key As String)
  19  
  20      Sub RemoveAll()
  21  
  22  End Interface
  23  
  24  
  25  ''' ---------------------------------------------------
  26  ''' AspNetCache - Encapsula as rotinas de cache do ASP.Net
  27  ''' ---------------------------------------------------
  28  Public Class AspNetCache
  29      Implements ICache
  30  
  31      'Objeto System.Web.Caching.Cache
  32      Private _cache As System.Web.Caching.Cache
  33  
  34      Private dias As Integer
  35  
  36      'Número de dias que o objeto ficará no cache
  37      Public Property Expire() As Integer
  38          Get
  39              Return dias
  40          End Get
  41          Set(ByVal value As Integer)
  42              dias = value
  43          End Set
  44      End Property
  45  
  46      Protected Property Cache() As System.Web.Caching.Cache
  47          Get
  48              Return _cache
  49          End Get
  50          Set(ByVal value As System.Web.Caching.Cache)
  51              _cache = value
  52          End Set
  53      End Property
  54  
  55      'Retorna o número de objetos no cache
  56      Public ReadOnly Property Count() As Integer
  57          Get
  58              Return Me.Cache.Count
  59          End Get
  60      End Property
  61  
  62      'Construtor, obtém o gerenciador de cache do contexto web atual.
  63      Public Sub New()
  64          Me.Cache = HttpContext.Current.Cache
  65      End Sub
  66  
  67      'Retorna do cache o objeto do tipo T
  68      Public Function GetObject(Of T)(ByVal key As String) As T Implements ICache.GetObject
  69          Dim obj As T
  70          Try
  71              obj = DirectCast(Me.Cache(key), T)
  72          Catch ex As Exception
  73              Throw
  74          End Try
  75          Return obj
  76      End Function
  77  
  78      'Inserção com generics (tipada)
  79      Public Sub Insert(Of T)(ByVal key As String, ByVal value As T) Implements ICache.Insert
  80          Try
  81              Me.Cache.Insert(key, value, Nothing, Now.AddDays(Me.dias), Caching.Cache.NoSlidingExpiration)
  82          Catch ex As Exception
  83              Throw
  84          End Try
  85      End Sub
  86  
  87      'Inserção não tipada
  88      Public Sub Insert(ByVal key As String, ByVal value As Object) Implements ICache.Insert
  89          Try
  90              Me.Cache.Insert(key, value, Nothing, Now.AddDays(Me.dias), Caching.Cache.NoSlidingExpiration)
  91          Catch ex As Exception
  92              Throw
  93          End Try
  94      End Sub
  95  
  96      'Atualização - Remove o antigo e insere o objeto novo (tipada - generics)
  97      Public Sub Update(Of T)(ByVal key As String, ByVal value As T) Implements ICache.Update
  98          Try
  99              'Remove o objeto existente...
 100              Me.Remove(key)
 101              '... e insere a nova versão
 102              Me.Cache.Insert(key, value, Nothing, Now.AddDays(Me.dias), Caching.Cache.NoSlidingExpiration)
 103          Catch ex As Exception
 104              Throw
 105          End Try
 106      End Sub
 107  
 108      'Atualização - Remove o antigo e insere o objeto novo (não tipada)
 109      Public Sub Update(ByVal key As String, ByVal value As Object) Implements ICache.Update
 110          Try
 111              'Remove o objeto existente...
 112              Me.Remove(key)
 113              '... e insere a nova versão
 114              Me.Cache.Insert(key, value, Nothing, Now.AddDays(Me.dias), Caching.Cache.NoSlidingExpiration)
 115          Catch ex As Exception
 116              Throw
 117          End Try
 118      End Sub
 119  
 120      'Remove um objeto do cache
 121      Public Sub Remove(ByVal key As String) Implements ICache.Remove
 122          Try
 123              Me.Cache.Remove(key)
 124          Catch ex As Exception
 125              Throw
 126          End Try
 127      End Sub
 128  
 129      'Remove todos os objetos do cache
 130      Public Sub RemoveAll() Implements ICache.RemoveAll
 131          Try
 132              Dim iterator As IDictionaryEnumerator = Me.Cache.GetEnumerator
 133              While iterator.MoveNext
 134                  Me.Remove(iterator.Key.ToString)
 135              End While
 136          Catch ex As Exception
 137              Throw
 138          End Try
 139      End Sub
 140  End Class
 141  
 142  
 143  ''' ---------------------------------------------------
 144  ''' Factory que instancia o objeto de cache de acordo com o argumento passado.
 145  ''' ---------------------------------------------------
 146  Public Class CacheFactory
 147  
 148      ''' Tipos de cache
 149      '''     AspNet: cache do Asp.Net (System.Web.Caching.Cache)
 150      '''        OBS: outros objetos encapsuladores de cache podem ser criados,
 151      '''        bastando para isso implementar a extensão ICache. Uma sugestão
 152      '''        seria criar uma classe para trabalhar com o Memcache usando a
 153      '''        biblioteca Enyim.Caching.MemcachedClient, disponível em 
 154      '''        http://www.codeplex.com/EnyimMemcached.
 155      Enum CacheType
 156          AspNet
 157      End Enum
 158  
 159      Public Shared Function GetCache(ByVal type As CacheType) As ICache
 160          Dim cache As ICache = Nothing
 161          Select Case type
 162              Case CacheType.AspNet
 163                  cache = New AspNetCache()
 164              ' No caso de adicionar mais gerenciadores de cache, inserir 
 165              ' aqui o código de criação dos objetos.
 166          End Select
 167          Return cache
 168      End Function
 169  
 170  End Class
created by bragil — 08 August 2008 — get a short url — tags: cache vb.net embed