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;
}
}
}