NET Caching Library Help
Getting Started VB.NET

     

Example of loading a monthly sales report from the cache. It demonstrates attaching a memory cache with a backing file storage provider and loading and saving to the cache. 
 

      Public Class Example

           

            Public Sub ExampleUsage()

                  'Create a Primary Memory Cache with a backing File Cache

                  Dim directory As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "KellermanSoftware", "NetCachingLibrary", "Example")

                  Dim config As New SmartConfig(New MemoryCacheProvider(), New FileCacheProvider(directory))

 

                  'Specify UserName and LicenseKey for Licensed Mode

                  'config.UserName = "my user name";

                  'config.LicenseKey = "my license key";

 

                  'Instantiate the cache

                  Dim cache As New SmartCache(config)

 

                  'Build parameters for a long running report

                  Dim parms As New Dictionary(Of String, Object)()

                  parms.Add("Month", Date.Now.Month)

                  parms.Add("Year", Date.Now.Year)

 

                  'Get the cache key based on the report parameters

                  Dim cacheKey As String = cache.BuildCacheKey("MonthlySalesReport", parms)

 

                  'Attempt to load the report from the cache

                  Dim report As List(Of ReportDetail) = cache.Get(Of List(Of ReportDetail))(cacheKey)

 

                  'Not found in the cache

                  If report Is Nothing Then

                        'Load the report from the database

                        report = ReportLogic.MonthlySalesReport(parms)

                        'Save to the cache in the Default region using the default expiration for the primary and secondary caches

                        cache.Set(Of List(Of ReportDetail))(cacheKey, report)

                  End If

            End Sub

      End Class

 

      Public Module ReportLogic

            'Example long running report

            Public Function MonthlySalesReport(ByVal parms As Dictionary(Of String, Object)) As List(Of ReportDetail)

                  System.Threading.Thread.Sleep(1000)

                  Dim list As New List(Of ReportDetail)()

                  list.Add(New ReportDetail("Midwest", 233457.47D))

                  list.Add(New ReportDetail("Southern", 234789.88D))

                  Return list

            End Function

      End Module