Home All Groups Group Topic Archive Search About

Cache Classes for Non-ASP.NET Apps??

Author
22 Jun 2006 5:20 PM
Jason Harmon
Does anyone know of a class (third party or otherwise) that implements
caching functionality similar to the "System.Web.Caching.Cache" class for
ASP.NET, but which can be used by non-ASP.NET apps (in my case a Windows
service)?

The System.Web.Caching.Cache class has exactly the functionality I need, but
I can't use it since my application is not a web app.

Any pointers would be helpful.

Thanks.

Jason Harmon

Author
23 Jun 2006 9:08 AM
Markus Kling
You can find a caching implementation within the Microsoft Enterprise
Library beeing available free via
http://www.microsoft.com/downloads/details.aspx?familyid=5A14E870-406B-4F2A-B723-97BA84AE80B5&displaylang=en




Show quote
"Jason Harmon" <jhar***@us.net> schrieb im Newsbeitrag
news:bd6dnYrZmInETAfZnZ2dnUVZ_vqdnZ2d@adelphia.com...
> Does anyone know of a class (third party or otherwise) that implements
> caching functionality similar to the "System.Web.Caching.Cache" class for
> ASP.NET, but which can be used by non-ASP.NET apps (in my case a Windows
> service)?
>
> The System.Web.Caching.Cache class has exactly the functionality I need,
> but I can't use it since my application is not a web app.
>
> Any pointers would be helpful.
>
> Thanks.
>
> Jason Harmon
>
Author
23 Jun 2006 2:06 PM
Jason Harmon
Thanks a bunch, much appreciated.

-Jason Harmon

Show quote
"Markus Kling" <markus.kling@nospam.nospam> wrote in message
news:eDDPhSqlGHA.856@TK2MSFTNGP03.phx.gbl...
> You can find a caching implementation within the Microsoft Enterprise
> Library beeing available free via
> http://www.microsoft.com/downloads/details.aspx?familyid=5A14E870-406B-4F2A-B723-97BA84AE80B5&displaylang=en
>
>
>
>
> "Jason Harmon" <jhar***@us.net> schrieb im Newsbeitrag
> news:bd6dnYrZmInETAfZnZ2dnUVZ_vqdnZ2d@adelphia.com...
>> Does anyone know of a class (third party or otherwise) that implements
>> caching functionality similar to the "System.Web.Caching.Cache" class for
>> ASP.NET, but which can be used by non-ASP.NET apps (in my case a Windows
>> service)?
>>
>> The System.Web.Caching.Cache class has exactly the functionality I need,
>> but I can't use it since my application is not a web app.
>>
>> Any pointers would be helpful.
>>
>> Thanks.
>>
>> Jason Harmon
>>
>
>
Author
23 Jun 2006 4:20 PM
sloan
You can also implement a "home grown" simple solution using the Singleton
Design Pattern.

This is "per machine" (in winforms) .. not "per application".

You can find the syntax usage at my blog:


But here is some code:

SimpleCacheHolder  sch = SimpleCacheHolder.GetInstance();
sch.Add("mykey" , new Exception("use an exception as a demo for any
object");

object o = sch["mykey"];
//o = sch.Remove("mykey"); // Or remove it
if (null!=o)
{
    Exception eeeexxxxx = o as Exception;
    Console.WriteLine (eeeexxxxx.Message);
}

spaces.msn.com/sholliday/  look for "Web Session Object Holder" or something
like that.

public class SimpleCacheHolder //: IDataStore
{
  private static SimpleCacheHolder   singletonInstance  = null;
  private HybridDictionary   m_memoryStore  = null;
  //private Hashtable     m_memoryStore  = null;

  private SimpleCacheHolder(  )
  {
   m_memoryStore = new HybridDictionary(); // Implements IDictionary by
using a ListDictionary while the collection is small, and then switching to
a Hashtable when the collection gets large.
   //m_memoryStore = new Hashtable(); // Or go straight for the HashTable
  }

  /// <summary>
  /// Singleton representing Memory store.
  /// </summary>
  /// <returns></returns>
  public static SimpleCacheHolder GetInstance(  )
  {
   if( null == singletonInstance )
   {
    singletonInstance = new SimpleCacheHolder();
   }
   return singletonInstance;
  }


  public void Clear( )
  {
   m_memoryStore.Clear( );
  }

  public void Add( string key, object value )
  {

   if( m_memoryStore.Contains( key ) )
   {
    m_memoryStore.Remove( key );
   }

   m_memoryStore.Add( key, value );
  }


  public object Remove( string key )
  {

   // see
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Hashtable.html#remove(java.lang.Object)
   // for java method, which returns the object you removed, in case
   // you want to do something with it

   object returnObject = null;
   if (null != this.m_memoryStore)
   {
    if( m_memoryStore.Contains( key ) )
    {
     returnObject = this.m_memoryStore[key];
     m_memoryStore.Remove( key );
    }

   }
   return returnObject;

  }

  public object this[ string key ]
  {
   get
   {
    if( null != m_memoryStore[ key ] )
    {
     return m_memoryStore[ key ];
    }
    return null;
   }
  }

  public int Size
  {
   get
   {
    if( null != m_memoryStore )
    {
     return m_memoryStore.Count ;
    }
    return 0;
   }
  }

}






Show quote
"Jason Harmon" <jhar***@us.net> wrote in message
news:bd6dnYrZmInETAfZnZ2dnUVZ_vqdnZ2d@adelphia.com...
> Does anyone know of a class (third party or otherwise) that implements
> caching functionality similar to the "System.Web.Caching.Cache" class for
> ASP.NET, but which can be used by non-ASP.NET apps (in my case a Windows
> service)?
>
> The System.Web.Caching.Cache class has exactly the functionality I need,
but
> I can't use it since my application is not a web app.
>
> Any pointers would be helpful.
>
> Thanks.
>
> Jason Harmon
>
>

AddThis Social Bookmark Button