<?xml version="1.0" encoding="UTF-8"?>
<codes type="array">
  <code>
    <code>Imports System.Web

''' ---------------------------------------------------
''' Interface para as classes de cache.
''' ---------------------------------------------------
Public Interface ICache

    Function GetObject(Of T)(ByVal key As String) As T

    Sub Insert(Of T)(ByVal key As String, ByVal value As T)

    Sub Insert(ByVal key As String, ByVal value As Object)

    Sub Update(Of T)(ByVal key As String, ByVal value As T)

    Sub Update(ByVal key As String, ByVal value As Object)

    Sub Remove(ByVal key As String)

    Sub RemoveAll()

End Interface


''' ---------------------------------------------------
''' AspNetCache - Encapsula as rotinas de cache do ASP.Net
''' ---------------------------------------------------
Public Class AspNetCache
    Implements ICache

    'Objeto System.Web.Caching.Cache
    Private _cache As System.Web.Caching.Cache

    Private dias As Integer

    'N&#250;mero de dias que o objeto ficar&#225; no cache
    Public Property Expire() As Integer
        Get
            Return dias
        End Get
        Set(ByVal value As Integer)
            dias = value
        End Set
    End Property

    Protected Property Cache() As System.Web.Caching.Cache
        Get
            Return _cache
        End Get
        Set(ByVal value As System.Web.Caching.Cache)
            _cache = value
        End Set
    End Property

    'Retorna o n&#250;mero de objetos no cache
    Public ReadOnly Property Count() As Integer
        Get
            Return Me.Cache.Count
        End Get
    End Property

    'Construtor, obt&#233;m o gerenciador de cache do contexto web atual.
    Public Sub New()
        Me.Cache = HttpContext.Current.Cache
    End Sub

    'Retorna do cache o objeto do tipo T
    Public Function GetObject(Of T)(ByVal key As String) As T Implements ICache.GetObject
        Dim obj As T
        Try
            obj = DirectCast(Me.Cache(key), T)
        Catch ex As Exception
            Throw
        End Try
        Return obj
    End Function

    'Inser&#231;&#227;o com generics (tipada)
    Public Sub Insert(Of T)(ByVal key As String, ByVal value As T) Implements ICache.Insert
        Try
            Me.Cache.Insert(key, value, Nothing, Now.AddDays(Me.dias), Caching.Cache.NoSlidingExpiration)
        Catch ex As Exception
            Throw
        End Try
    End Sub

    'Inser&#231;&#227;o n&#227;o tipada
    Public Sub Insert(ByVal key As String, ByVal value As Object) Implements ICache.Insert
        Try
            Me.Cache.Insert(key, value, Nothing, Now.AddDays(Me.dias), Caching.Cache.NoSlidingExpiration)
        Catch ex As Exception
            Throw
        End Try
    End Sub

    'Atualiza&#231;&#227;o - Remove o antigo e insere o objeto novo (tipada - generics)
    Public Sub Update(Of T)(ByVal key As String, ByVal value As T) Implements ICache.Update
        Try
            'Remove o objeto existente...
            Me.Remove(key)
            '... e insere a nova vers&#227;o
            Me.Cache.Insert(key, value, Nothing, Now.AddDays(Me.dias), Caching.Cache.NoSlidingExpiration)
        Catch ex As Exception
            Throw
        End Try
    End Sub

    'Atualiza&#231;&#227;o - Remove o antigo e insere o objeto novo (n&#227;o tipada)
    Public Sub Update(ByVal key As String, ByVal value As Object) Implements ICache.Update
        Try
            'Remove o objeto existente...
            Me.Remove(key)
            '... e insere a nova vers&#227;o
            Me.Cache.Insert(key, value, Nothing, Now.AddDays(Me.dias), Caching.Cache.NoSlidingExpiration)
        Catch ex As Exception
            Throw
        End Try
    End Sub

    'Remove um objeto do cache
    Public Sub Remove(ByVal key As String) Implements ICache.Remove
        Try
            Me.Cache.Remove(key)
        Catch ex As Exception
            Throw
        End Try
    End Sub

    'Remove todos os objetos do cache
    Public Sub RemoveAll() Implements ICache.RemoveAll
        Try
            Dim iterator As IDictionaryEnumerator = Me.Cache.GetEnumerator
            While iterator.MoveNext
                Me.Remove(iterator.Key.ToString)
            End While
        Catch ex As Exception
            Throw
        End Try
    End Sub
End Class


''' ---------------------------------------------------
''' Factory que instancia o objeto de cache de acordo com o argumento passado.
''' ---------------------------------------------------
Public Class CacheFactory

    ''' Tipos de cache
    '''     AspNet: cache do Asp.Net (System.Web.Caching.Cache)
    '''        OBS: outros objetos encapsuladores de cache podem ser criados,
    '''        bastando para isso implementar a extens&#227;o ICache. Uma sugest&#227;o
    '''        seria criar uma classe para trabalhar com o Memcache usando a
    '''        biblioteca Enyim.Caching.MemcachedClient, dispon&#237;vel em 
    '''        http://www.codeplex.com/EnyimMemcached.
    Enum CacheType
        AspNet
    End Enum

    Public Shared Function GetCache(ByVal type As CacheType) As ICache
        Dim cache As ICache = Nothing
        Select Case type
            Case CacheType.AspNet
                cache = New AspNetCache()
            ' No caso de adicionar mais gerenciadores de cache, inserir 
            ' aqui o c&#243;digo de cria&#231;&#227;o dos objetos.
        End Select
        Return cache
    End Function

End Class</code>
    <created-at type="datetime">2008-08-08T17:58:54Z</created-at>
    <description>Conjunto de classes para encapsular as opera&#231;&#245;es de inser&#231;&#227;o, atualiza&#231;&#227;o, remo&#231;&#227;o e busca no cache do Asp.Net, podendo ser adicionados novos gerenciadores de cache, como o Memcache, por exemplo.</description>
    <id type="integer">49</id>
    <language-id type="integer">6</language-id>
    <privated type="boolean">false</privated>
    <title>Cache em VB.Net</title>
    <updated-at type="datetime">2008-08-08T18:04:28Z</updated-at>
    <user-id type="integer">49</user-id>
  </code>
</codes>
