Generic MemoryCache revisited


Share this

Generic MemoryCache Class

Imports System.Collections.Specialized
Imports System.Runtime.Caching


Namespace Caching.Generic

    Public Class MyMemoryCache
        Private MyCache As MemoryCache
        Public ReadOnly Property Count As Integer
            Get
                Return MyCache.Count
            End Get
        End Property

        Public ReadOnly Property Cache As MemoryCache
            Get
                Return MyCache
            End Get
        End Property

        Public Sub New(Domain As StringOptional CacheMemory As Integer = 0Optional PhysicalMemoryLimit As Long = 0Optional PollingIntervalMinutes As Integer = 2)
            If PhysicalMemoryLimit > 100 Then PhysicalMemoryLimit = 100
            Dim cachesetting As New NameValueCollection
            cachesetting.Add("CacheMemoryLimitMegabytes", CacheMemory)
            cachesetting.Add("physicalMemoryLimitPercentage", PhysicalMemoryLimit)
            cachesetting.Add("pollingInterval"TimeSpan.FromMinutes(PollingIntervalMinutes).ToString())
            cachesetting.Add("Name", Domain)
            MyCache = New MemoryCache(Domain, cachesetting)
        End Sub

        Public Function GetObjectFromCache(Of T)(ByVal cacheItemName As StringByVal cacheTimeInMinutes As IntegerByVal objectSettingFunction As Func(Of StringT), ByRef isCached As BooleanByRef AccessTime As LongAs T
            Dim sw As New Stopwatch
            sw.Start()
            Dim cachedObject = CType(MyCache(cacheItemName), T)
            If cachedObject Is Nothing Then
                Dim policy As CacheItemPolicy = New CacheItemPolicy()
                'policy.AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(cacheTimeInMinutes) ' Fixed Timeout
                policy.SlidingExpiration = TimeSpan.FromMinutes(cacheTimeInMinutes) ' Sliding timeout
                cachedObject = objectSettingFunction(cacheItemName)
                isCached = False
                MyCache.[Set](cacheItemName, cachedObject, policy)
            Else
                isCached = True
            End If
            sw.Stop()
            AccessTime = sw.ElapsedMilliseconds
            Return cachedObject
        End Function
    End Class

End Namespace

Using Generic MemoryCache

Imports TechnicaOne.Caching.Generic
Dim IPAddressCache As New TechnicaOne.Caching.Generic.MyMemoryCache("IPAddressCache")
Dim FetchfromDatabase As Func(Of StringIPAddressLookup= AddressOf dbFetch
Private Function dbFetch(key As StringAs IPAddressLookup
    Dim results As IPAddressLookup
    Using db As New ServerDefenderDBDataContext
        results = (From this In db.IPAddressLookups Where this.IPAddress = key Take 1).SingleOrDefault
    End Using
    Return results
End Function
Dim isCached As Boolean = False
       Dim AccessTime As Long


       Dim IPAddressResult As IPAddressLookup = IPAddressCache.GetObjectFromCache(Of IPAddressLookup)(ComboBox1.SelectedItem, 10, FetchfromDatabase, isCached, AccessTime)

First lookup from database

 

First load from database (Single Record)

First load from database (Single Record)

Subsequent lookup from Memory Cache

 

Load from memory cache (Single Record)

Load from memory cache (Single Record)

Benchmark - DB Load

 

5000 records loaded from database

5000 records loaded from database - 1 m 9s 73ms

Benchmark - From Cache

 

5000 records loaded from memorycache - 26 ms

5000 records loaded from memorycache - 26 ms


Share this
Article Rating:
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...