Caching Data for Windows Applications

Only Microsoft knows why their preferred way for caching only works with ASP.NET.
But it’s a fact that the class System.Web.Caching.Cache only works in a web environment.

If you want to cache application data for a windows application you have to write your own caching code.
Due to the fact that I like the Cache class for ASP. NET I re-wrote it in order to use it for Windows Applications.

The benefit of that implementation is that you can specify an expiration date for the cached object.

public void Insert(string key, object current, DateTime absoluteExpiration, TimeSpan slidingExpiration)

Example:

class Program
{
    private static Cache cache = new Cache();
    
    /// <summary>
    /// Caching test
    /// </summary>
    /// <param name="args"></param>
    static void Main(string[] args)
    {
        BigObjectForCaching bof = new BigObjectForCaching();
        bof.Content = "Imagine that this is a very big object :) you want to cache.";
        
        Console.WriteLine("Insert Bof01 with a 5 second sliding expiration into cache.");
        /*
         * Take 5 seconds into cache (use sliding cache expiration)
         */
        cache.Insert("bof01", bof, Cache.NoAbsoluteExpiration, new TimeSpan(0,0,0,5));
        
        /*
         * Loop 10 seconds to show the cache change
         */
        for (int x = 0; x < 10; x++)
        {
            Console.WriteLine(string.Concat("Bof01 is in cache ", cache.Contains("bof01")));
            Thread.Sleep(1000);
        }
        
        
        Console.WriteLine("\nInsert Bof01 with a 5 second static expiration into cache.");
        
        /*
         * Insert object into cache. Use static expiration
         */
        cache.Insert("bof01", bof, DateTime.Now.AddSeconds(5), Cache.NoSlidingExpiration);
        
        /*
         * Loop 10 seconds to show the cache change
         */
        for (int x = 0; x < 10; x++)
        {
            Console.WriteLine(string.Concat("Bof01 is in cache ", cache.Contains("bof01")));
            Thread.Sleep(1000);
        }
        
        /*
         * Insert last time into cache - object never expires
         */
        cache.Insert("bof01", bof, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration);
        bof = cache.Get("bof01") as BigObjectForCaching;
        
        Console.WriteLine(string.Concat("\nContent : ", bof.Content));
        Console.ReadKey();
    }
    
    /*
     * Expected output :
     *
     * Insert Bof01 with a 5 second sliding expiration into cache.
     *
     * Bof01 is in cache True
     * Bof01 is in cache True
     * Bof01 is in cache True
     * Bof01 is in cache True
     * Bof01 is in cache True
     * Bof01 is in cache False
     * Bof01 is in cache False
     * Bof01 is in cache False
     * Bof01 is in cache False
     * Bof01 is in cache False
     *
     * Insert Bof01 with a 5 second static expiration into cache.
     * Bof01 is in cache True
     * Bof01 is in cache True
     * Bof01 is in cache True
     * Bof01 is in cache True
     * Bof01 is in cache True
     * Bof01 is in cache False
     * Bof01 is in cache False
     * Bof01 is in cache False
     * Bof01 is in cache False
     * Bof01 is in cache False
     *
     * Content : Imagine that this is a very big object :) you want to cache.
     */
}


You can download that small example including the re-wroted implementation of the cache class using the following link:
http://www.ad-factum.de/wordpress/CacheTest.zip

Best regards
- Gerhard

8 Responses to “Caching Data for Windows Applications”

  1. Felix Prangishvili Says:

    Good clean code. Wanted to write it myself recently but now don’t see why I should. Thanks.
    The only thing though is that it would be nice to have a method which would compact the cache either explicitly (i.e. through a direct call to the method) or on a scheduled basis at predetermined intervals (with the interval settings passed in the constructor, for instance). Also, a property enabling/disabling the autocompacting would be feasible, I guess. But again, great job, thanks a lot!

  2. ID-10-T Says:

    Ah, you CAN use System.Web.Caching.Cache outside of ASP.NET (e.g., from a Winforms app or a console app). Get your facts straight.

  3. Kumar Says:

    You wrote a cache class? I see you initializing the Cache object ;) Are you saying you wrote [i]that[/i] class?

  4. Gerhard Says:

    Kumar, download the example and you’ll see.

  5. sandip Says:

    Nice!!!

  6. test Says:

    INTRODUCTION
    The System.Web.Caching.Cache object in the Microsoft .NET Framework 1.1 and in the Microsoft .NET Framework 1.0 is not intended for use outside Microsoft ASP.NET. The Cache object was designed and tested for use by ASP.NET to provide ASP.NET caching. If you try to use Cache objects in other types of applications, such as console applications or Windows Forms applications, the Cache objects may not work correctly.

    Note The specific problem that is described in the “More Information” section does not occur in the Microsoft .NET Framework 2.0. Therefore, you can use the Cache object outside ASP.NET in the .NET Framework 2.0. However, we recommend that you consider using the Caching Application Block because it offers you more options for customization to meet application requirements.

    MORE INFORMATION
    You cannot create a System.Web.Caching.Cache object on an operating system that does not have ASP.NET installed. C

  7. Eddmartinez Says:

    Great Work Gerhard! Yes Microsoft preferred ASP.NET cache and it is very good in its performance but only in its single server environment. But now Microsoft also realized and starts working on distributed caching. Because distributed caching is scalable and highly reliable as compared to traditional caching.

  8. Joe Says:

    Nice stuff.

    Some changes I made was to add the indexed default property so I can use Cache["Key"], if trying to insert null then remove the key (if it exists) and I replaced INFINITE with Timeout.Infinite (from System.Threading).

    Thanks!


Leave a Reply