Using Web Cache in Non-web Applications

All this time, I've mistakenly thought that System.Web.Caching.Cache can only be used in web applications.  It turns out that is not true.  You can use the web cache in Console or WinForm applications.  Here's some sample code showing how that's done. 

Note: Documentation for the Cache class explitcitly states that it was designed and tested for use in ASP.NET. Although I've shown that it can be used in console and WinForm applications, using the Cache class outside of ASP.NET is not supported by Microsoft.  

Using Web Cache in a Console Application:

Module Module1

    Private Const CACHE_KEY As String = "testcache"

    Private Const CACHE_DURATION_SECONDS As Double = 10

    Sub Main()

        Do

            PrintInstructions()

            Dim ki As ConsoleKeyInfo = Console.ReadKey(True)

            Dim command As String = ki.Key.ToString().ToLower()

            Select Case command

                Case "q"

                    Exit Sub

                Case "i"

                    Console.WriteLine("Please enter value you would like to insert into the cache")

                    Dim cacheValue As String = Console.ReadLine()

                    Dim cacheExpiration As DateTime = DateTime.Now.AddSeconds(CACHE_DURATION_SECONDS)

                    System.Web.HttpRuntime.Cache.Insert(CACHE_KEY, cacheValue, _

                                                Nothing, _

                                                cacheExpiration, _

                                                System.Web.Caching.Cache.NoSlidingExpiration)

                    Console.WriteLine(cacheValue & " has been inserted into the cache. It will expire at " & cacheExpiration.ToLongTimeString())

                Case "r"

                    Dim cacheValue As Object = System.Web.HttpRuntime.Cache.Get(CACHE_KEY)

                    If (cacheValue Is Nothing) Then

                        Console.WriteLine("Nothing in the cache")

                    Else

                        Console.WriteLine(cacheValue.ToString() & " - current time: " & Now.ToLongTimeString())

  End If

                Case Else

                    PrintInstructions()

            End Select

        Loop While True

    End Sub

    Sub PrintInstructions()

        Console.WriteLine("===========================================")

  Console.WriteLine("Please enter any of the following commands:")

        Console.WriteLine(" i - insert a value into the cache")

        Console.WriteLine(" r - display whatever is in the cache")

        Console.WriteLine(" q - quit")

     Console.WriteLine("===========================================")

    End Sub

End Module

 

Using Web Cache in a WinForm Application:

Public Class Form1

Private Const CACHE_KEY As String = "testcache"

Private Const CACHE_DURATION_SECONDS As Double = 10

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Dim cacheValue As Object = System.Web.HttpRuntime.Cache.Get(CACHE_KEY)

If (cacheValue Is Nothing) Then

MessageBox.Show("Nothing in the cache")

Else

MessageBox.Show(cacheValue.ToString() & " - current time: " & Now.ToLongTimeString())

End If

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim cacheExpiration As DateTime = DateTime.Now.AddSeconds(CACHE_DURATION_SECONDS)

System.Web.HttpRuntime.Cache.Insert(CACHE_KEY, TextBox1.Text, _

Nothing, _

cacheExpiration, _

System.Web.Caching.Cache.NoSlidingExpiration)

MessageBox.Show(TextBox1.Text & " has been inserted into the cache. It will expire at " & cacheExpiration.ToLongTimeString())

End Sub

End Class